MVVMCross Code Snippets for Visual Studio?
Asked Answered
B

4

11

where can I download the snippets for Visual Studio, the likes pf pmvx, cmvx and others? I though those would be available on the github projects, but can't find them...

Bigamy answered 13/8, 2013 at 4:26 Comment(1)
SO is not for seeking download links. Try Google.Busk
M
18

I've already created my own. Please remember that these are ReSharper's template.

mvxcom -> for Command

#region $region$ command
private MvxCommand _$NAME$Command;

public ICommand $PNAME$Command
{
    get
    {
       _$NAME$Command = _$NAME$Command ?? new MvxCommand(Do$PNAME$Command);
       return _$NAME$Command;
    }
}

private void Do$PNAME$Command()
{
    $END$        
}
#endregion

mvxprop -> for Properties

#region $region$
private $TYPE$ _$NAME$;
public $TYPE$ $PNAME$
{ 
get { return _$NAME$; }
set { _$NAME$ = value; RaisePropertyChanged(() => $PNAME$); }
}
#endregion
$END$

mvxset -> Binding Set

var set = this.CreateBindingSet<$VIEW$, $VIEW$Model>();
set.Bind($ELEMENT$).To(vm => vm$END$);
set.Apply();

You can add them to your ReSharper using ReSharper>Template Explorer>Live Templates>New Template.

Please feel free to change them however you desire.

Max answered 15/8, 2013 at 7:6 Comment(4)
Hi, thanks for sharing. I prefer not use underscores, is it possible to set a field name starting with a small letter and a property name starting by a capital name (so it would be only one value $NAME)?Momus
And another issue: in your script's properties setter is of 1 line, but in VS it becomes a 4-lined, is it possible to fix that?Momus
for question #1 you could use $NAME and $PNAME and use small letters for $NAME however, the above is aligned with Resharper's default scheme. #2 may be some configs set in your VS to break the lines longer than a certain length. For me it's always one line.Max
When creating or editing a Resharper template there is an option to Reformat. Uncheck it and it will keep the original format.Idolatrous
E
6

And here's the native VS version of the snippets based on Aboo's answer above.

<?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>Andrew Coates (based on Aboo's SO ReSharper Answer)</Author>
      <HelpUrl>https://mcmap.net/q/964929/-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 { if($backingfield$ == value) return; $backingfield$ = value; RaisePropertyChanged(() => $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>Andrew Coates (based on Aboo's SO ReSharper Answer)</Author>
      <HelpUrl>https://mcmap.net/q/964929/-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 Binding Set</Title>
      <Description>Create a binding set and bind an element</Description>
      <Shortcut>mvxset</Shortcut>
      <Author>Andrew Coates (based on Aboo's SO ReSharper Answer)</Author>
      <HelpUrl>https://mcmap.net/q/964929/-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>
Edlun answered 1/3, 2014 at 5:58 Comment(1)
I found an updated version of this that includes a snippet for async commands as well: artm-blog.blogspot.com/2017/05/…Latt
C
3

If I'm correct in my presumption, you're referring to the shortcuts that Stuart uses in his demos for MvvmCross. These are snippets of code that he has written and assigned to shortcuts using ReSharper and are not publicly available, though if you asked nicely they can be exported and shared. Of course you could always make your own 'Live Template' using this tutorial

Caecum answered 13/8, 2013 at 10:18 Comment(1)
Yes, you are correct, I'm talking about those. I though they were already available, as those are a real productivity buster. MVVMlight exposes this in his setup, I though Cross was doing the same. Hope Stuart reads this and shares one more thing with us. Anyway, thank you.Bigamy
B
1

I modified @Coasty's answer to include an async command adapted from https://mcmap.net/q/491473/-how-can-i-use-async-in-an-mvvmcross-view-model and changed the shortcuts so they no longer collide with the mvx namespaces.

<?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>mvvmcrossprop</Shortcut>
      <Author>Andrew Coates (based on Aboo's SO ReSharper Answer)</Author>
      <HelpUrl>https://mcmap.net/q/964929/-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 { $backingfield$ = value; RaisePropertyChanged(() => $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>mvvmcrosscom</Shortcut>
      <Author>Andrew Coates (based on Aboo's SO ReSharper Answer)</Author>
      <HelpUrl>https://mcmap.net/q/964929/-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$ = $backingfield$ ?? new MvxCommand(Do$Name$);
            return $backingfield$;
          }
        }

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

        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MvvmCross Async Command</Title>
      <Description>Insert an Async Command declaration for an MvvmCross View Model</Description>
      <Shortcut>mvvmcrossacom</Shortcut>
      <Author>Benjamin Hysell (based on Andrew Coates Command Answer)</Author>
      <HelpUrl>https://mcmap.net/q/964929/-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$ = $backingfield$ ?? new MvxCommand(async () => await Do$Name$());
            return $backingfield$;
          }
        }

        private async Task Do$Name$()
        {
          $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>mvvmcrossset</Shortcut>
      <Author>Andrew Coates (based on Aboo's SO ReSharper Answer)</Author>
      <HelpUrl>https://mcmap.net/q/964929/-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>
Budde answered 16/3, 2015 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.