Posts

Donate

How To Iterate Or Loop Through A C# Dictionary And Update It's Value

Hello, Most of us C# developers are working on Dictionaries to store multiple sets of data with unique keys. And in times that we need to modify our logic with the Dictionary object involved, we normally do that using looping construct. Given a simple Dictionary object below with int as key and string as value. Dictionary< int , string > styledLines = new Dictionary< int , string >(); We may thought that looping through this object with the code presented below may work. However, when running your app, this will cause a "Collection was modified; enumeration operation may not execute" issue. It's because we are using the collection's key directly. foreach ( var kvp in styledLines) { styledLines[kvp.key] = $ "Series# {seriesNum} "; } To fix that we have a couple of options. Given that our key is an integer type with values in sequential order such as 0...1000, we could use for loop to change the values such as below. var startI

BinaryFormatter Custom Binder Alternative Using Newtonsoft.Json.JsonSerializer In C#

Hello Everyone! In relation to the previous post on updating the serialization and deserialization using BinaryFormatter class to NewtonSoft's JsonSerializer, we also need to change the Binder functionality using NewtonSoft. The class below inherits the SerializationBinder and overrides the BindToType() function. sealed class HELPERSCustomBinder : SerializationBinder { public override Type BindToType( string assemblyName, string typeName) { if (!typeName.ToUpper().Contains( "SYSTEM.STRING" )) { throw new SerializationException( "Only List<string> is allowed" ); } return Assembly.Load(assemblyName).GetType(typeName); } } The class above will then be assigned to the Binder property of BinaryFormatter as shown from the code snippet below. public static T BuildFromSerializedBuffer<T>( byte [] buffer) where T : new () { MemoryStream MS; BinaryFormatter BF; T rtn; rtn = default (T); try { using (M

BinaryFormatter Serialize() And Deserialize() Alternative Using Newtonsoft.Json.JsonSerializer In C#

Good evening! Part of the migration of our parts cataloging WPF application to .NET 7 was to investigate obsolete code and refactor that to something acceptable and maintainable in the future. One of which that needs to be addressed is the BinaryFormatter class used for serialization and deserialization. Searching thru the internet presents tons of options but since the application has already a Newtonsoft.Json package, we decided to use it's serialization instead. So to proceed with, below are the sample serialization and deserialization functions that incorporates the BinaryFormatter class and converted to NewtonSoft.Json.JsonSerializer. Below is a function using BinaryFormatter serialization. public static byte [] GetSerializedBuffer<T>(T itemToSerialize) { byte [] rtn; MemoryStream MS; BinaryFormatter BF; if (! typeof (T).IsSerializable && !( typeof (ISerializable).IsAssignableFrom( typeof (T)))) throw new InvalidOperationException( "A ser

Xamarin Forms Error - adb is not recognized as an internal or external command

Image
Good day! Following a tutorial on Xamarin.Forms, I encountered this issue namely " adb is not recognized as an internal or external command " when running adb commands in command prompt. After doing some research for answers to this problem, I came up with the steps to make it work. First is to locate the path of the adb application which can be found in android-sdk->platform-tools folder Next is to go to System Properties and open Environment Variables form. Under System variables, locate the Path entry and click the Edit button. Copy the folder path of the adb.exe application and add it as a new entry to the System variable Path. Click Ok and restart your computer. The adb command should work as expected.

How To Disable WPF UI Debugging In Visual Studio 2022

Image
Hello, When running your WPF application thru Visual Studio, you may find a UI toolbar that is useful for checking the Adorner or Visual Tree of the control such as below. However, if you want to remove that toolbar, go to Tools menu -> Options -> Debugging. Then uncheck " Enable in-app toolbar ". Click Ok. Run the application again and you will notice that the UI toolbar disappear.

Process.Start() Error - An error occurred trying to start process. The specified executable is not a valid application for this OS platform.

Image
Good evening! I had an issue when opening PDF files using Process.Start() which is called in a ShowFile() method of the project. The code which isn't working is shown below. System.Diagnostics.Process.Start(Path.Combine(myDocumentsPath, fileName)); The solution for that is to pass a ProcessStartInfo() object in the Start() parameter. And then set UseShellExecute property of the ProcessStartInfo object to true. The modified code that is working is shown below. System.Diagnostics.Process.Start( new ProcessStartInfo(Path.Combine(myDocumentsPath, fileName)) { UseShellExecute = true }); Output

WPF MVVM - How To Pass ViewModel Or Element As ConverterParameter Value In XAML Code

Image
Good evening! In this post, I will demonstrate on how to pass a view model or an element as ConverterParameter value in XAML. This methodology is widely used by developers if you need to retrieve certain values or properties from a view model object or element. To send the view model data context as ConverterParameter value, reference the view model class in the data context node of your form or user control. <UserControl d:DesignHeight= "450" d:DesignWidth= "800" > <UserControl.DataContext> <localmain:ConfigurationResourceManagerViewModel x:Name= "vmResourceManager" /> </UserControl.DataContext> </UserControl> In your element as to where the ConverterParameter is, set it's value with the view model name. In the example snippet below, the ConverterParameter is used in the ComboBox SelectedItem property with a reference to the view model object. <DataGridTemplateColumn Header= "Resource

Xamarin Forms Project - Project Not Selected To Build For This Solution Configuration

Image
Hello and Happy Easter! A while ago I was downloading some Xamarin Forms projects from Github for testing and research purposes. Upon running the projects, it won't show on the Android emulator and in the Visual Studio IDE, a notification shows that the project was skipped in deployment with a detailed statement like "Project Not Selected To Build For This Solution Configuration" . I found the solution which is to right click on the Solution of the project -> Select Configuration Manager. If you notice, the Deploy checkbox is unchecked. Changing the state to checked solved the issue. I can now run the projects using the Android emulator. Cheers!

How To Use Samsung Galaxy Mobile Phones Pop-up View Feature

Image
Good day, A cool feature of Samsung Galaxy mobile phone series is the pop-up view of applications. This enables the user to open multiple applications and set an application as either background or in pop-up view mode. An example scenario is you're currently doing light exercises at home. You may open a Youtube application playing your favorite zumba or aerobics music compilations and another app such as StopWatch set as the pop-up view in order to monitor the timeframe of a particular exercise. To use the pop-up view feature,open at least two applications like Youtube or Clock. Switch to the Youtube app and make it as the current application running. Press the Recent Apps button of your phone encircled in Red for you to select the app as the pop-up. Select and press the Clock app and drag it to the Youtube app. A Drop here for pop-up view empty container appears as to where you will drop the program. Drop the Clock application into that empty container. The layout of

XAML Hot Reload Is Disabled Because It Requires Xamarin.Forms 5.0.0.2012 Or Newer -In Visual Studio 2019

Image
Hello, I downloaded a couple of Xamarin Forms applications written in Visual Studio 2017 and debugged them using Visual Studio 2019. When running these projects, a notification appears below the toolbar that says "XAML Hot Reload Is Disabled Because It Requires Xamarin.Forms 5.0.0.2012 Or Newer". I then proceed with Updating the Xamarin.Forms Packages for these projects and another issue came up which is "The $(TargetFrameworkVersion) for Project_name.Android (v8.1) is less than the minimum required $(TargetFrameworkVersion) for Xamarin.Forms (10.0)." . The solution to this error led me to StackOverflow that is to change a couple of Application and Application Manifest settings for the Android project. Changing the Compiled using Android version and Target Android version made the error go away. Application Tab. Change the Android version target from Android 8.1 to Android 11. Application Manifest Tab. Change the Android version target from Android 8.1

Donate