WPF custom Window style (2), wpf custom window style
Directory:
WPF custom Window style (1)
WPF custom Window style (2)
1. Introduction
In the previous article, we introduced how to create a custom form. Next, we need to consider putting this custom form base class into the class library. Only by putting it in the class library can we easily reference this base class elsewhere.
2. Create a class library
Next, add a class library project stonemqy. CustomWindow. Helpers. Of course, we need to put files such as VisualStates, TransitioningContentControl, CustomWindow, Themes/Generic. xaml into the class library. In this case, add the following reference:
PresentationCor (4.0.0.0)
PresentationFramework (4.0.0.0)
Microsoft. Expression. Interactions (4.5.0.0)
System. Windows. Interactivity (4.5.0.0)
System. Xaml (4.0.0.0)
WindowBase (4.0.0.0)
At this time, we find that resource files cannot be added. You also need to use NotePad to open the project file stonemqy. customWindow. helpers. csproj: manually add the following code in the first PropertyGroup Section. This line of code allows you to add files such as the WPF form and resource dictionary to the class library.
<PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{07AAB444-6AD3-469D-9E94-4A84BADFB36D}</ProjectGuid> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>stonemqy.CustomWindow.Helpers</RootNamespace> <AssemblyName>stonemqy.CustomWindow.Helpers</AssemblyName> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> </PropertyGroup>
Make sure to modify the local reference in the Generic. xaml file:
xmlns:local="clr-namespace:stonemqy.CustomWindow.Helpers"
So far, we have completed the establishment of the class library project.
3. reference the class library in the main project
Modify MainWindow. xaml of the main item, and modify the mwindow namespace in MainWindow. cs.
<local:CustomWindow x:Class="stonemqy.CustomWindow.Main.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:stonemqy.CustomWindow.Helpers;assembly=stonemqy.CustomWindow.Helpers" Title="MainWindow-In Lib" Height="350" Width="525" Icon="logo.png"> <Grid> </Grid></local:CustomWindow>
public partial class MainWindow : Helpers.CustomWindow { public MainWindow() { InitializeComponent(); } }
You can't wait to see the Running Effect
Why ?!! The program does not enable custom styles. It's also a Copey ......
4. Use the class library to customize the form base class style
After Google, CodeProject, and StackOverflow, we finally found the cause:
I) Add the following line of code to AssemblyInfo. cs in the Helpers project:
[assembly:ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
Ii) Modify ThemeInfo configuration in the Main project:
[Assembly: ThemeInfo (ResourceDictionaryLocation. none, // location of the topic-specific resource dictionary // (used when a resource is not found in the page or application resource dictionary) ResourceDictionaryLocation. externalAssembly // location of the general resource dictionary // (used on a page, application, or any topic-specific resource dictionary // If a resource is not found)]
Note that the second parameter is ResourceDictionaryLocation. SourceAssembly (search for the Generic. xaml topic in this program). You need to change it to ResourceDictionaryLocation. ExternalAssembly (search for the topic in external resources ).
Run the program again, and the familiar custom form is displayed again:
5. Source Code
Source code