onsdag den 10. maj 2017

Visual Studio Xamarin MvvmCross Code Snippets

Visual Studio Xamarin MvvmCross Code Snippets

Introduction

This post is based on this StackOverflow question which contains visual studio code snippets to help with MvvmCross development.

Updated

We have updated the mvxprop snippet as well as added "mvxasynccom" to let you easily add async commands to your viewmodels.

Install

Save the following code in a file called updated-mvx.snippet

In visual studio:
Tools -> Code Snippets Manager -> Click "My Code Snippets" and click the "Import..." button.
Select the file and hit okay. You should now be able to use "mvxprop", "mvxcom", "mvxasynccom" and "mvxset" in your code.

File: updated-mvx.snippet


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MvvMCross Property</Title>
      <Description>Insert a property block with a backing field and property change notification</Description>
      <Shortcut>mvxprop</Shortcut>
      <Author>Mikkel Jensen (based on Andrew Coates and Aboo's SO answers</Author>
      <HelpUrl>http://stackoverflow.com/questions/18200679/mvvmcross-code-snippets-for-visual-studio</HelpUrl>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>backingfield</ID>
          <ToolTip></ToolTip>
          <Default>propertyName</Default>
        </Literal>
        <Literal>
          <ID>property</ID>
          <ToolTip></ToolTip>
          <Default>PropertyName</Default>
        </Literal>
        <Literal>
          <ID>propertyType</ID>
          <ToolTip></ToolTip>
          <Default>int</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[private $propertyType$ $backingfield$;
        public $propertyType$ $property$
        {
          get { return $backingfield$; }
          set { SetProperty(ref $backingfield$, value, "$property$"); }
        }
        $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MvvMCross Command</Title>
      <Description>Insert a Command declaration for an MvvMCross View Model</Description>
      <Shortcut>mvxcom</Shortcut>
      <Author>Mikkel Jensen (based on Andrew Coates and Aboo's SO answers</Author>
      <HelpUrl>http://stackoverflow.com/questions/18200679/mvvmcross-code-snippets-for-visual-studio</HelpUrl>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>backingfield</ID>
          <ToolTip></ToolTip>
          <Default>commandName</Default>
        </Literal>
        <Literal>
          <ID>Name</ID>
          <ToolTip></ToolTip>
          <Default>CommandName</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[private MvxCommand $backingfield$Command;

        public MvxCommand $Name$Command
        {
          get
          {
            $backingfield$Command = $backingfield$Command ?? new MvxCommand(Do$Name$Command);
            return $backingfield$Command;
          }
        }

        private void Do$Name$Command()
        {
          $end$
        }

        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MvvMCross Async Command</Title>
      <Description>Insert a Command declaration for an MvvMCross View Model</Description>
      <Shortcut>mvxasynccom</Shortcut>
      <Author>Mikkel Jensen (based on Andrew Coates and Aboo's SO answers</Author>
      <HelpUrl>http://stackoverflow.com/questions/18200679/mvvmcross-code-snippets-for-visual-studio</HelpUrl>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>backingfield</ID>
          <ToolTip></ToolTip>
          <Default>commandName</Default>
        </Literal>
        <Literal>
          <ID>Name</ID>
          <ToolTip></ToolTip>
          <Default>CommandName</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[private MvxAsyncCommand $backingfield$CommandAsync;

        public MvxAsyncCommand $Name$CommandAsync
        {
          get
          {
            $backingfield$CommandAsync = $backingfield$CommandAsync ?? new MvxAsyncCommand(Do$Name$CommandAsync);
            return $backingfield$CommandAsync;
          }
        }

        private void Do$Name$CommandAsync()
        {
          $end$
        }

        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MvvMCross Binding Set</Title>
      <Description>Create a binding set and bind an element</Description>
      <Shortcut>mvxset</Shortcut>
      <Author>Mikkel Jensen (based on Andrew Coates and Aboo's SO answers</Author>
      <HelpUrl>http://stackoverflow.com/questions/18200679/mvvmcross-code-snippets-for-visual-studio</HelpUrl>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>viewName</ID>
          <ToolTip></ToolTip>
          <Default>viewName</Default>
        </Literal>
        <Literal>
          <ID>element</ID>
          <ToolTip></ToolTip>
          <Default>element</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[var set = this.CreateBindingSet<$viewName$View, $viewName$ViewModel>();
        set.Bind($element$).To(vm => vm$end$);
        set.Apply();
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Ingen kommentarer:

Send en kommentar