locked
Can Snippet and Template run together? RRS feed

  • Question

  • I suspect the answer is "no" but I'll ask anyway.

    I recently discovered the MVVM pattern for WPF applications and I'm trying to implement Commands the way Jason Dolinger explained in this video. Basically you create a new class that implements ICommand, your view model has an ICommand property for this command and the view binds to that command. The ViewModel would implement the functionality that determines if the command is available and how it executes. So basically when you want to add a command you need to 1) create a new class for your command that implements ICommand 2) add an ICommand property to your view-model, instantiate the command, and create methods to implement the command's functionality 3) bind to the command in your view.

    I created an ItemTemplate to do #1. Here's the code, in case anyone is interested:

     

    using System;
    using System.Windows.Input;
    
    namespace $rootnamespace$
    {
     public class $safeitemname$ : ICommand
     {
     private ViewModel p_oVM;
    
     public $safeitemname$(ViewModel oVM)
     {
     p_oVM = oVM;
     }
    
     public event EventHandler CanExecuteChanged
     {
     add { CommandManager.RequerySuggested += value; }
     remove { CommandManager.RequerySuggested -= value; }
     }
    
     public bool CanExecute(object parameter)
     {
     return p_oVM.Can$fileinputname$();
     }
    
     public void Execute(object parameter)
     {
     p_oVM.$fileinputname$();
     }
     }
    }
    

     

    And I created a snippet to handle (most) of #2:

     

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
     <CodeSnippet Format="1.0.0">
     <Header>
     <Title>Implement Command</Title>
     <Shortcut>icomm</Shortcut>
     <Description>Code snippet for implementing a command class in the ViewModel</Description>
     <Author>Nicholas Layton</Author>
     <SnippetTypes>
     <SnippetType>Expansion</SnippetType>
     </SnippetTypes>
     </Header>
     <Snippet>
     <Imports>
     <Import>
     <Namespace>System.Windows.Input</Namespace>
     </Import>
     </Imports>
     <Declarations>
     <Literal>
     <ID>name</ID>
     <ToolTip>Command Name</ToolTip>
     <Default>MyCommand</Default>
     </Literal>
     </Declarations>
     <Code Language="csharp">
     <![CDATA[
    public ICommand $name$Command { get; set; }
    
    public bool Can$name$()
    {
     throw new NotImplementedException();
    }
    
    public void $name$()
    {
     throw new NotImplementedException();
    }
    $end$]]>
     </Code>
     </Snippet>
     </CodeSnippet>
    </CodeSnippets>
    

     

    You still need to manually write the code to instantiate the command in your view model's constructor, but it's good enough.

    Now maybe I'm just lazy, but if you look at the code I could easily change $name$ in the snippet to $fileinputname$ and the string replace dictionaries would be identical between the Snippet and the Item Template. I don't suppose there is a way to get them to run together automagically in 1 step for the developer, is there? And maybe if there was I could even improve the template by programmatically finding the Type of the ViewModel class.



    Tuesday, April 12, 2011 3:16 PM

Answers

  • Hi ColtonGrundy,

     

    Thanks for your post.

    I suppose you expect to trigger insert code snippet into your constructor automatically,

    but why not include those code lines in your template code file?

    I'm looking for APIs to insert code snippet and use them in template wizard(IWizard).

    Looking forward to your reply.


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Victor_Chen Monday, April 25, 2011 5:19 AM
    Thursday, April 14, 2011 6:50 AM

All replies

  • Hi ColtonGrundy,

     

    Thanks for your post.

    I suppose you expect to trigger insert code snippet into your constructor automatically,

    but why not include those code lines in your template code file?

    I'm looking for APIs to insert code snippet and use them in template wizard(IWizard).

    Looking forward to your reply.


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Victor_Chen Monday, April 25, 2011 5:19 AM
    Thursday, April 14, 2011 6:50 AM
  • Hi ColtonGrundy ,

     

    Is it resolved? Any progress?


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 21, 2011 7:46 AM