Resharper. How to add xaml template - c#

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>

For this, you have to use a multifile template. Do it like this:
Copy the code at the end of this post into a text file called WindowTemplate.DotSettings.
Open the ReSharper Templates Explorer via Extensions → ReSharper → Tools → Templates Explorer....
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"><Window x:Class="$Namespace$.$Class$"
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:$Namespace$"
mc:Ignorable="d"
Title="$Class$" Height="450" Width="800">
<Grid>
</Grid>
</Window></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"><RelativeConfig File="$NAME$.xaml.cs" /></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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace $Namespace$
{
/// <summary>
/// Interaction logic for $Class$.xaml
/// </summary>
public partial class $Class$ : Window
{
public $Class$()
{
InitializeComponent();
}
}
}
</s:String></wpf:ResourceDictionary>

Related

WPF MainWindow differs at runtime from it's expected outcome

I am relatively new to this platform, so forgive me for my lack of professionalism.
I encountered a problem while creating one of my first WPF applications.
I managed to include some buttons in MainWindow and run it, but I spotted some bugs, so I tried to start from the scratch.
I decided to run an empty window, but then i saw the previous "buggy" window at runtime. Since then, no matter what i change in MainWindow.xaml file i get the same old result.
I tried to chceck whether my program compiles, and for what I know it does.
Unfortunately I erased old xaml code responsible for "buggy" window, but I can recall it was something like this.
<Window x:Class="ProjektPO.MainWindow"
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:ProjektPO"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="A" Content="B" Click="A_Click"/>
</Grid>
</StackPanel>
</Grid>
</Window>
And the .cs file (almost default)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ProjektPO
{
/// <summary>
/// Logika interakcji dla klasy MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void A_Click(object sender, RoutedEventArgs e)
{
}
}
}
Program compiles and dispalys old "buggy" Window.

WPF Custom Control in separate namesapce

I try to make Visual C# app, which is using UserControl (WPF).
By default, VS Community 2015 C# generates UserControl in the same namespace as main program. It's built correctly.
I'd like to separate it in it's own namespace for possible future reuse.
I change auto-generated code to have UserControl in separate namespace.
Here is my code
UserControl
<UserControl x:Class="nsTabI2CMemRW.TabI2CMemRW"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:nsTabI2CMemRW"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500">
<Grid>
<TextBox x:Name="AddrH"/>
<Label x:Name="AddrH_Label"/>
</Grid>
C# class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication1; // This is my main app namespace
namespace nsTabI2CMemRW
{
/// <summary>
/// Interaction logic for TabI2CMemRW.xaml
/// </summary>
public partial class TabI2CMemRW : UserControl
{
public TabI2CMemRW()
{
//InitializeComponent();
((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent(); //InitializeComponent();
}
}
}
Main Windows XAML
<Window x:Class="WpfApplication1.MainWindow"
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:WpfApplication1"
xmlns:UserControl="clr-namespace:nsTabI2CMemRW"
mc:Ignorable="d"
Title="MainWindow" Height="649.005" Width="620.667" ScrollViewer.CanContentScroll="True">
<Grid>
<TabControl x:Name="tabControl" Margin="0,0,0.4,0.2">
<TabControl.BindingGroup>
<BindingGroup/>
</TabControl.BindingGroup>
<TabItem x:Name="I2CMemReadWrite" Header="I2C Mem Read/Write">
<UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>
</TabItem>
<TabItem Header="TabItem">
<Button x:Name="button" Content="Button" Height="100" Width="75"/>
</TabItem>
</TabControl>
</Grid>
Designer shows error "Object reference is not set to an instance of an object" for line
<UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>
Program compiled fine but first tab (where my UserControl should be) is blank.
There are many things wrong here, cant answer them all. try comenting
((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent();
and leaving
InitializeComponent();
instead in your C# class
Yes, this ((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent(); was wrong! Error "Object reference is not set to an instance of an object" relates to this code (Found using VS debugger as in this post WPF, 'Object reference not set to an instance of an object' in Designer. I think it's useful for similar cases with WPF).
After commenting-out line (see above) and leaving only InitializeComponent() and recompiling code twice - it works!
Also I've checked that right code-behind file bound to XAML file (Accidentally deleted the code behind for an Xaml file. How do I add the code behind again?).
And also this lines are important xmlns:UserControl="clr-namespace:nsTabI2CMemRW"
and <UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>. I think I can missed them first time. (After xmlns: can be any name, not only UserControl)

Could not load file or assembly WPFToolkit

I am trying to use WPFToolkit in my user control dll. I inserted the WPFToolkit in its reference, and my user control dll builds with no error.
Then I insert my user control dll into my application, but when my application new an object of my user control dll
MultiROIStats mroi = new MultiROIStats();
the exception occured, saying:
Additional information: Could not load file or assembly 'WPFToolkit, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Here is my user control dll code, the constructor where the error occurs.
View xaml code:
<Window x:Class="MultiROIStats.MultiROIStats"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<dg:DataGrid ItemsSource="{Binding Path=FileData}" Margin="0,30,0,0" />
<Button Height="22" HorizontalAlignment="Left" Margin="8,4,0,0"
Name="button1" VerticalAlignment="Top" Width="48"
Command="{Binding Path=GetDataCommand}">Button
</Button>
</Grid>
</Window>
View C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MultiROIStats
{
using System.Windows;
using ViewModel;
//xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class MultiROIStats : Window
{
public MultiROIStats()
{
InitializeComponent(); // exception occurs here!
DataContext = new MultiROIStatsViewModel();
}
}
}
I also checked the binary folder of my user control dll, the WPFToolkei.dll is there. So I am confused, and I am wondering how can I correct this error? Thanks.
Have you maybe tried the NuGet package for the toolkit to see if that works instead?

wpf custom title bar does not work but have no exceptions

I downloaded the sample code from
http://www.cnblogs.com/Files/sheva/RibbonStyle2.zip
I added three key files into a new project without changing the files. They are NativeMethods.cs, OfficeWindow.cs, and Generic.xaml.
I then use my new WFP form (MainWindow.xaml) to inherit from OfficeWindow.
<cc:OfficeWindow
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:RibbonStyle"
ResizeMode="CanResizeWithGrip"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfApplication1;component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</cc:OfficeWindow>
and code behind MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RibbonStyle;
namespace WpfApplication1
{
public partial class MainWindow : OfficeWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
The design view looks good. It shows ribbon style title bar as expected. However when I run it in the debugger it shows a classic title bar without and exceptions.
Note that if you download the original sample code from
http://www.cnblogs.com/Files/sheva/RibbonStyle2.zip
and run it directly it works.
Can anyone try my way and tell me what I have missed?
Thanks,
i have checked this example ..i think it is working fine here is the screeschot..
if thr is problem in its styling comment below..

Partial declarations, must not specify different base classes

I know there is information about this on the internet and I've searched for it. But I'm still getting the error, can anyone point out to me what I'm doing wrong?
Base class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
namespace ProgramManagementV2.screens
{
public abstract class AScreenUserControl : UserControl
{
public string GetScreenDescriptionName()
{
return "No name yet!";
}
}
}
MainUserControl.xaml
<UserControl x:Class="ProgramManagementV2.screens.MainUserControl"
xmlns:we="clr-namespace:ProgramManagementV2.screens"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Text="asdfasdf" VerticalAlignment="Center" />
</Grid>
</UserControl>
MainUserControl.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace ProgramManagementV2.screens
{
/// <summary>
/// Interaction logic for MainUserControl.xaml
/// </summary>
public partial class MainUserControl : AScreenUserControl
{
public MainUserControl()
{
InitializeComponent();
}
}
}
As you can see I'm adding
xmlns:we="clr-namespace:ProgramManagementV2.screens"
to the user control xml but I'm still getting the error:
Partial declarations of 'ProgramManagementV2.screens.MainUserControl' must not specify different base classes
Can anyone explain to me what I'm doing wrong?
By using <UserControl ... you claim the base-class to be UserControl, you would need to change it to
<we:AScreenUserControl x:Class="ProgramManagementV2.screens.MainUserControl" ...
However UserControls only allow one level of inheritance i think, at least if the AScreenUserControl has a XAML this surely will not work.
Just do this:
<we:AScreenUserControl x:Class="ProgramManagementV2.screens.MainUserControl"
xmlns:we="clr-namespace:ProgramManagementV2.screens"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Text="asdfasdf" VerticalAlignment="Center" />
</Grid>
</we:AScreenUserControl>

Categories