SourceSurfer
SourceSurfer

Reputation: 185

Resharper. How to add xaml template

How to add in the Resharper template with .xaml extension? For example, I am trying to add a Window.xaml and the problem is in this line <Window x:Class="ADONET_Samples.UserControls.Window1" how to put in "Window1" the name of your XAML form.

<Window x:Class="ADONET_Samples.UserControls.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ADONET_Samples.UserControls"
    mc:Ignorable="d"
    Title="Window1" Height="450" Width="800">
<Grid>

</Grid>

Upvotes: 0

Views: 294

Answers (1)

mu88
mu88

Reputation: 5384

For this, you have to use a multifile template. Do it like this:

  1. Copy the code at the end of this post into a text file called WindowTemplate.DotSettings.
  2. Open the ReSharper Templates Explorer via Extensions → ReSharper → Tools → Templates Explorer....
  3. Open the tab File Templates and import the file WindowTemplate.DotSettings.

You should see a template called Custom Window. If you're interested in the code, you can open it.You can also add the template to the quicklist when setting the layer within Templates Explorer to Smart.

When using Alt + Insert in Solution Explorer and opening More..., there will be the template. Choose it an enter a name for the file. Now the code generation is done.

However, you won't be able to compile it with the following error: CS0103: The name 'InitializeComponent' does not exist in the current context. You have to solve this manually for every file by opening the *.csproj file and looking for the following code:

<AdditionalFiles Include="Demo3.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</AdditionalFiles>

Replace it with (notice Page instead of AdditionalFile):

<Page Include="Demo3.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Page>

Last but not least, the necessary template code:

<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Description/@EntryValue">Custom Window</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Text/@EntryValue">&lt;Window x:Class="$Namespace$.$Class$"&#xD;
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&#xD;
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&#xD;
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&#xD;
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&#xD;
    xmlns:local="clr-namespace:$Namespace$"&#xD;
    mc:Ignorable="d"&#xD;
    Title="$Class$" Height="450" Width="800"&gt;&#xD;
&lt;Grid&gt;&#xD;
&#xD;
&lt;/Grid&gt;&#xD;
&lt;/Window&gt;</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Reformat/@EntryValue">True</s:Boolean>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/ShortenQualifiedReferences/@EntryValue">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/CustomProperties/=FileName/@EntryIndexedValue">Window</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/CustomProperties/=Extension/@EntryIndexedValue">xaml</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/CustomProperties/=ValidateFileName/@EntryIndexedValue">False</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Applicability/=File/@EntryIndexedValue">True</s:Boolean>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Scope/=55C906A5BD591341AE313A07A8F303DE/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Scope/=55C906A5BD591341AE313A07A8F303DE/Type/@EntryValue">InAnyXamlProject</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Field/=Namespace/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Field/=Namespace/Expression/@EntryValue">fileDefaultNamespace()</s:String>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Field/=Namespace/InitialRange/@EntryValue">-1</s:Int64>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Field/=Namespace/Order/@EntryValue">0</s:Int64>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Field/=Class/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Field/=Class/Expression/@EntryValue">getAlphaNumericMainFileNameWithoutExtension()</s:String>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Field/=Class/InitialRange/@EntryValue">-1</s:Int64>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Field/=Class/Order/@EntryValue">1</s:Int64>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Section/=60769A869101874889D7DC3F51C8EEA7/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Section/=60769A869101874889D7DC3F51C8EEA7/LocationSelectorName/@EntryValue">manual</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Section/=60769A869101874889D7DC3F51C8EEA7/LocationSelectorConfig/@EntryValue">&lt;RelativeConfig File="$NAME$.xaml.cs" /&gt;</s:String>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Section/=60769A869101874889D7DC3F51C8EEA7/Order/@EntryValue">0</s:Int64>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=0CB2F5C22C4A274F8D1425E1F1DA05CF/Section/=60769A869101874889D7DC3F51C8EEA7/SectionPlain/Text/@EntryValue">using System;&#xD;
using System.Collections.Generic;&#xD;
using System.Linq;&#xD;
using System.Text;&#xD;
using System.Threading.Tasks;&#xD;
using System.Windows;&#xD;
using System.Windows.Controls;&#xD;
using System.Windows.Data;&#xD;
using System.Windows.Documents;&#xD;
using System.Windows.Input;&#xD;
using System.Windows.Media;&#xD;
using System.Windows.Media.Imaging;&#xD;
using System.Windows.Navigation;&#xD;
using System.Windows.Shapes;&#xD;
&#xD;
namespace $Namespace$&#xD;
{&#xD;
    /// &lt;summary&gt;&#xD;
    /// Interaction logic for $Class$.xaml&#xD;
    /// &lt;/summary&gt;&#xD;
    public partial class $Class$ : Window&#xD;
    {&#xD;
        public $Class$()&#xD;
        {&#xD;
            InitializeComponent();&#xD;
        }&#xD;
    }&#xD;
}&#xD;
</s:String></wpf:ResourceDictionary>

Upvotes: 2

Related Questions