The programming language WPF sets the resource dictionary used in design for the class library

The programming language WPF sets the resource dictionary used in design for the class library ...

The programming language WPF sets the resource dictionary used in design for the class library

When developing WPF class library, because there is no App.xaml.cs file in the class library, when developing a single XAML, the designer will not get the resources because the resource file cannot be found. This article tells you a simple way to set the designer with resources that are only referenced at design time

In the XAML of WPF, if you reference the same resource for each XAML control, you can run happily at design time. However, you will repeatedly create resources at run time, which will affect performance. When developing WPF applications, because there is an App.xaml file in the entry project, the references of various projects are added to this file. At this time, the designer can know the XAML resource dictionary referenced by the current project, so the designer can work

However, when developing the class library, the class library does not know what the final entry project is, so it does not know that the current program will be referenced when running

The best way is to let the designer reference some resources at design time, just like Blend, so that the designer can work

The implementation method is to add a special file in the class library. This special file has folder and naming requirements. This is a constant fixed path written in the visual studio designer

Create a new project   Properties   Folder, in   Properties   Create a new DesignTimeResources.xaml resource dictionary file in the folder, as follows:

The naming rules of this file are agreed, and it is not recommended to modify it yourself. Add the following code on csproj

<ItemGroup> <Page Update="Properties\DesignTimeResources.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> <ContainsDesignTimeResources>true</ContainsDesignTimeResources> </Page> </ItemGroup>

The core of the above code is the property ContainsDesignTimeResources. Theoretically, this property can be set for any XAML file, but XAML designer can read only the file in this path on many VS versions

By adding references to other resource dictionaries in DesignTimeResources.xaml resource dictionary, the designer of the class library can find resources, but the resources will not be loaded into memory at runtime

For example, I created a new class library project jeenaleneearwerjilakaw project. I added the resource dictionary ColorBrushResourcesDictionary.xaml in the jeenaleneearwerjilakaw project, and stored the color brush in it. The code is as follows

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:JeenalerenenearWerjilakaw"> <SolidColorBrush x:Key="Brush.ColorBrush.ThemeColorBrush">#FF565656</SolidColorBrush> </ResourceDictionary>

I expect to use this Brush.ColorBrush.ThemeColorBrush resource on the custom control UserControl1.xaml of the jeenaleneearwerjilakaw project, as shown in the following code

<Grid> <Border Background="" Margin="10,10,10,10"></Border> </Grid>

The designer and code cannot work at this time, and the resource will not be found at the designer prompt

Next, create a new   Properties\DesignTimeResources.xaml   Resource dictionary file. Add the following code to this resource dictionary file

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="../ColorBrushResourcesDictionary.xaml"></ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>

Add the following code to the csproj of jeenaleneearwerjilakaw

<ItemGroup> <Page Update="Properties\DesignTimeResources.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> <ContainsDesignTimeResources>true</ContainsDesignTimeResources> </Page> </ItemGroup>

All the code for the current csproj looks like this

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <UseWPF>true</UseWPF> </PropertyGroup> <ItemGroup> <Page Update="Properties\DesignTimeResources.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> <ContainsDesignTimeResources>true</ContainsDesignTimeResources> </Page> </ItemGroup> </Project>

The above code is used for SDK style csproj files. If the current project file is not SDK style, please refer to migrating from the previous project format to VS2017 new project format

Next, save the code, then close visual studio and clean up the cache file. Open visual studio to see that the current designer and code can work

The code of this article is placed in github and gitee. Welcome to download and execute

WPF Design Time Support (Part 2) · jbe2277/waf Wiki

However, in visual studio with Resharper, this configuration is unknown in some versions of Resharper 2020. At this time, Resharper will not find the resource reference and will not have the function of automatic jump and completion. Fortunately, Resharper's support for project format is not good enough. We can use black technology to solve this problem. This problem is also fixed in the 2021 version of Resharper, so the following code does not need to be added in the new version of Resharper

In Resharper, the application resource file will be read according to the application definition, but the Condition content will be ignored when reading. So we can try to create one called   App_MakeReshaperHappy.xaml   The file used to get Resharper Carson. Add the following code in csproj to reference the application resource, but it will not actually use this resource

<ApplicationDefinition Include="App_MakeReshaperHappy.xaml" Condition="false"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> </ApplicationDefinition>

In the above code, the Condition is false, so that this file is actually ignored during construction. Otherwise, it will be reported that the ApplicationDefinition element cannot be defined in the WPF class library during construction. Why not call the App.xaml file? The reason is that in SDK style csproj, EnableDefaultApplicationDefinition will be added by default, and the default ApplicationDefinition element will be added automatically. As long as there is a file called App.xaml, it will be automatically recognized as an ApplicationDefinition element, which will make the WPF build time report unable to be defined in the class library. If EnableDefaultApplicationDefinition is set to false, Resharper will not recognize this file. After the exploration of lsj tools, it is found that the above method is the most stable

stay   App_MakeReshaperHappy.xaml   The following code is stored in the file to enable Resharper intelligent perception to find resources during design

<Application x:Class="App_MakeReshaperHappy" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!--This file is just for Reshaper Intelligent perception of happiness--> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!--Replace the following with your own path. I recommend referencing the design time resource file--> <ResourceDictionary Source="Properties/DesignTimeResources.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>

The above code must define its own type, then inherit the Application and have   x:Class   Content. The file name and class name of this file must contain the three characters App, and the file name and class name must be the same

The above code is placed in github and gitee. Welcome to download and execute

www.ijzcn.cn www.zuowenge.cnwww.awaedu.com

21 September 2021, 20:38 | Views: 1587

Add new comment

For adding a comment, please log in
or create account

0 comments