开发者

Fastest way to create a Converter in Visual Studio

When working with Binding converters are pretty common.. I always find myself

  • Right-click the correct Folder
  • Click Add
  • Click New Item
  • (Sometimes) Choose Code to the left
  • Choose Class and "Add" (and sometimes when I'm in a hurry a create an AboutBox instead :-( )
  • Copy an old IValueConverter or IMultiValueConverter
  • Change the namespace and the class name
  • Remove the old code

and then I can finally start to implement my new converter.

After this I also have to add this namespace to the xaml file and add it to resources before I can reference it. I've been doing this a million times and this is probablly the slowest w开发者_StackOverflow中文版ay to do it so my question is..

What is the fastest way to create a Converter in Visual Studio?


You can certainly save time on this part:

  • Copy an old IValueConverter or IMultiValueConverter
  • Change the namespace and the class name
  • Remove the old code

Do it this way instead:

  • Create a new class (FooConverter for instance)
  • Make it implement IValueConverter by just adding : IValueConverter
  • With the caret still on IValueConverter, press Ctrl + . to open the smart tag menu
  • Select "Implement interface IValueConverter" (should be the first option) from the smart tag menu

Visual Studio will automatically create the necessary method stubs, you just need to write the implementation.


Probably the best resource for you is the Visual Studio templates. You can create your own and so you could right-click/create new item/Converter that would stub in everything that you're doing manually.

If you do create something like that, it would probably be a great little project to share with the community via codeplex or something like that.

Creating Item Templates in Visual Studio


I like this question and value converters, so i have a little contribution that might not help you, Meleak, but hopefull those who don't have such strict style guidelines and thus can place multiple converters in one file: A snippet that creates the code for a value converter stub.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Value Converter Stub</Title>
            <Shortcut>vc</Shortcut>
            <Description>Code snippet for a value converter stub</Description>
            <Author>H.B.</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>from</ID>
                    <ToolTip>The source type.</ToolTip>
                    <Default>object</Default>
                </Literal>
                <Literal>
                    <ID>to</ID>
                    <ToolTip>The target type.</ToolTip>
                    <Default>object</Default>
                </Literal>
                <Literal>
                    <ID>convertername</ID>
                    <ToolTip>Name of the converter.</ToolTip>
                    <Default>My</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[[ValueConversion(typeof($from$), typeof($to$))]
public class $convertername$Converter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        $from$ input = ($from$)value;
        $to$ output;

        throw new NotImplementedException();

        return output;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        $to$ input = ($to$)value;
        $from$ output;

        throw new NotImplementedException();

        return output;
    }
}]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Save this as vc.snippet (or something else with the extension snippet) in the snippet folder of Visual Studio (for VS2010 on x64 that would be: "%ProgramFiles% (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#"). You can also import the snippet via Tools -> Code Snippet Manager....

To use it just type vc and hit tab twice, then tab through the two class fields to fill them out.

You might need to add two references for the ValueConversion-attribute and IValueConverter.

The above snippet is quite verbose so here would be a lighter version (keyword is vcl) which omitts the attribute and method frames and has just one editable field which directly sets the complete class name:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Value Converter Stub (Light)</Title>
            <Shortcut>vcl</Shortcut>
            <Description>Code snippet for a light value converter stub</Description>
            <Author>H.B.</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
            <Literal>
                <ID>convertername</ID>
                <ToolTip>Name of the converter.</ToolTip>
                <Default>My</Default>
            </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[public class $convertername$Converter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        $end$
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>


Keep all of your converter classes in a single file (ie: Converters.cs), then when you need to add one you can do the same copy/paste that you're already doing but you won't have to add a new item, re-namespace, add a new namespace, etc.

At least, that's what I do...


I have added a Value Converter item template to the Visual Studio 2010: From the Project Context Menu, Add...New Item...Online Templates folder...Silverlight folder...Value Converter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜