Andr谩s T贸th‘s professional blog
banditoth.net

Hey there 馃憢, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

Xamarin.Forms: Lazy loaded Command snippet for Visual Studio

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.

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>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.