Xamarin: How .NET MAUI will change the life of Xamarin.Forms development

.NET 6 and .NET MAUI is on the sill, but how will it affect the development of Xamarin applications?

For today, I have brought you a video by James Montemagno, a Xamarin developer working at Microsoft.

Follow James on the following platforms:

? https://montemagno.com
? https://mergeconflict.fm, https://blunders.fm, https://nintendodispatch.com
? https://twitter.com/jamesmontemagno
? https://youtube.com/jamesmontemagno

This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

.NET JSON Serialization and deserialization flavoured with some inheritance

Imagine, you have a base class.

    public class BaseClass
    {
        public string BaseProperty { get; set; }
    }

It can have a base property in it. To keep OOP principles in mind, you are creating a derived class by of it called “InheritedClass”

    public class InheritedClass : BaseClass
    {
        public string PlusProperty { get; set; }
    }

And you are defining a container class, which holds an instance of a BaseClass or an InheritedClass. You are defining a property with BaseClass type. It will fit to InheritedClass too.

    public class ContainerClass
    {
        public BaseClass Property { get; set; }
    }

If you are casting your object, the PlusProperty’s value never gets lost, because the framework allocates the memory for an InheritedClass.

But what about, if you are serialize this object, and then deserialize it back? What do you think, what will be the result of this program?

    class Program
    {
        static void Main(string[] args)
        {
            var toBeSerialized = new ContainerClass();
            toBeSerialized.Property = new InheritedClass()
            {
                BaseProperty = "I am base property",
                PlusProperty = "I am an inherited plus property"
            };

            string serializedJSON = JsonConvert.SerializeObject(toBeSerialized);

            ContainerClass deserialized = (ContainerClass)JsonConvert.DeserializeObject(serializedJSON, typeof(ContainerClass));

            Console.WriteLine(deserialized.Property.GetType().AssemblyQualifiedName);
            Console.WriteLine((deserialized.Property as InheritedClass)?.PlusProperty);
            Console.ReadLine();
        }
    }

This code snippet gives the following result:

TestConsoleApp.BaseClass, TestConsoleApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null


That is because JSON does not include type names when serializing and deserializing an object.

You can set TypeNameHandling = All for your JsonSerializerSettings, but it is highly unrecommended by Microsoft, because it can lead you into security issues, allows attackers remote code execution.

        static void Main(string[] args)
        {
            var toBeSerialized = new ContainerClass();
            toBeSerialized.Property = new InheritedClass()
            {
                BaseProperty = "I am base property",
                PlusProperty = "I am an inherited plus property"
            };

            string serializedJSON = JsonConvert.SerializeObject(toBeSerialized, new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.All
            });

            ContainerClass deserialized = (ContainerClass)JsonConvert.DeserializeObject(serializedJSON, typeof(ContainerClass), new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.All
            });

            Console.WriteLine(deserialized.Property.GetType().AssemblyQualifiedName);
            Console.WriteLine((deserialized.Property as InheritedClass)?.PlusProperty);
            Console.ReadLine();
        }

Output:

TestConsoleApp.InheritedClass, TestConsoleApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
I am an inherited plus property

” Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. An attack against an insecure deserializer could, for example, execute commands on the underlying operating system, communicate over the network, or delete files. ” More details at: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2326

This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin.Forms: Lazy loaded Command snippet for Visual Studio

Use xamcomm tab tab to generate commands in ViewModels with this snippet

IntelliSense recommendation

Snippet in work

Save the snippet file as {name}.snippet in Visual Studio’s folder: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC#\Snippets\1033\Visual C#

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>Xamarin Lazy Command</Title>
			<Shortcut>xamcomm</Shortcut>
			<Description>Xamarin Command declaration code snippet for MVVM design pattern</Description>
			<Author>banditoth.hu</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>BackFieldName</ID>
					<ToolTip>Backfield Name</ToolTip>
					<Default>_backfieldname</Default>
				</Literal>
				<Literal>
					<ID>CommandName</ID>
					<ToolTip>Command name</ToolTip>
					<Default>CommandName</Default>
				</Literal>
				<Literal>
					<ID>ActionToExecute</ID>
					<ToolTip>Action to execute</ToolTip>
					<Default>() => { return; /*TODO: Implement logic for this Command*/ }</Default>
				</Literal>
        <Literal>
          <ID>ActionCanExecute</ID>
          <ToolTip>Action to determine can execute</ToolTip>
          <Default>() => true</Default>
        </Literal>
			</Declarations>
			<Code Language="csharp">
			<![CDATA[
	private Command $BackFieldName$Command;

	public Command $CommandName$Command
	{
		get { return $BackFieldName$Command ?? ($BackFieldName$Command = new Command($ActionToExecute$,$ActionCanExecute$)); }
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>
This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin Forms Maps: Nagyításra szánt gombok eltűntetése

Androidon default megjelennek a Xamarin.Forms.Maps.Map használata esetén a nagyításra szánt + és – gombok, illetve a felhasználó aktuális pozíciójára mozgató gomb.
Ezeknek eltűntetéséhez egy CustomMapRenderer-t kell létrehozni, és az Androidos rendererben be kell állítani a térképen, hogy ne jelenlítse meg ezeket a plusz gombokat.

Xamarin Forms kód:

public class CustomMap : Map
    {
        public CustomMap(MapSpan region) : base(region)
        {
        }
    }

Xamarin.Android kód:

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]

namespace SampleApp.Droid.Renderers
{
    public class CustomMapRenderer : MapRenderer
    {
		protected override void OnMapReady(GoogleMap map)
		{
			base.OnMapReady(map);
			// Nagyításhoz gombok 
			map.UiSettings.ZoomControlsEnabled = false;
			// Saját helyzet gombok
			map.UiSettings.MyLocationButtonEnabled = false;
		}
    }
}
This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Word Capitalisation Xamarin Forms Entryben

Amennyiben azt szeretnénk, hogy minden szóköz leütése után automatikusan nagybetűvel kezdje a szavakat a készülék-billentyűzet abban az esetben el kell nevezni az Entry-t, jelen esetben az Entryből származtatott UserControlt.

<blackandyellowdesign:UnderlinedEntryWithLabelOnRight
                    x:Name="FullNameEntry"
                    Placeholder="{markupextensions:Multilanguage TranslationKey=PlaceholderRegisterViewName}"
                    Text="{Binding Path=FullName}" />

Így hivatkozható CodeBehindban FullNameEntry-ként.

A konstruktorban (vagy tetszőleges helyre, valamilyen trigger esetén) pedig be kell állítani a Keyboard tulajdonságát, a Keyboard osztály statikus “Create” metódusának meghívásával.

        FullNameEntry.Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeWord);
This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.