【问题标题】:Resharper. How to add xaml template锐器。如何添加 xaml 模板
【发布时间】:2020-04-20 09:13:10
【问题描述】:

如何添加扩展名为 .xaml 的 Resharper 模板? 例如,我正在尝试添加一个 Window.xaml,问题出在这一行 <Window x:Class="ADONET_Samples.UserControls.Window1" 如何在“Window1”中输入 XAML 表单的名称。

<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>

【问题讨论】:

  • 你能更详细地解释一下你的问题吗?你想简单地输入一个字符串吗?你可以用这样的变量来做到这一点:&lt;Window x:Class="$Class$"
  • 我喜欢使用热键,在 ReSharper - ctrl+Insert 让我可以使用各种模板来自动创建类、接口等,但没有任何用于创建 Windows1.xaml 表单的各种模板。你的答案 -

标签: c# wpf visual-studio xaml


【解决方案1】:

为此,您必须使用多文件模板。这样做:

  1. 将本文末尾的代码复制到名为WindowTemplate.DotSettings 的文本文件中。
  2. 通过 Extensions → ReSharper → Tools → Templates Explorer...打开 ReSharper Templates Explorer...
  3. 打开标签文件模板并导入文件WindowTemplate.DotSettings

您应该会看到一个名为自定义窗口的模板。如果您对代码感兴趣,可以打开它。您也可以在 Templates Explorer 中将图层设置为 Smart 时将模板添加到快速列表中。

在解决方案资源管理器中使用Alt + Insert 并打开更多... 时,将出现模板。选择它并输入文件的名称。现在代码生成完成了。

但是,您将无法编译它并出现以下错误:CS0103: The name 'InitializeComponent' does not exist in the current context。您必须通过打开 *.csproj 文件并查找以下代码为每个文件手动解决此问题:

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

将其替换为(注意Page 而不是AdditionalFile):

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

最后但同样重要的是,必要的模板代码:

<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>

【讨论】:

  • 谢谢,只为 $Class$ 设置宏“当前文件名不带扩展名”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 2021-10-31
  • 2019-02-20
  • 2020-04-07
  • 2015-12-16
  • 1970-01-01
相关资源
最近更新 更多