湘野渔人 发表于 2023-6-16 18:24:35

【.NET深呼吸】将XAML放到WPF程序之外

上一篇水文中,老周说了一下纯代码编写 WPF 的大概过程。不过,还是不够的,本篇水文中咱们还要更进一步。
XAML 文件默认是作为资源打包进程序中的,而纯代码编写又导致一些常改动的东西变成硬编码了。为了取得二者平衡,咱们还要把一些经常修改的东西放到 XAML 文件中,不过 XAML 文件不编译进程序里,而是放到外部,运行阶段加载。比如一些对象属性、画刷、样式、字体之类的,直接改文件保存就行,修改之后不用重新编译项目。
要在运行阶段加载 XAML,咱们只需认识一个类就OK—— XamlReader,调用它的 Load 方法就能从 XAML 文件加载对象了。
下面老周就边演示边唠叨一下相关的问题。
一、新建项目。可以参照上一篇中的做法,用控制台应用程序项目,然后修改项目文件。也可以直接建 WPF 项目。都可以。
 
 
二、自定义窗口类,从 Window 派生。当然,你直接用 Window 类也可以的。
public class MyWindow : Window
{
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>const string XAML_FILE = "MyWindow.xaml";

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>public MyWindow()
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>{
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>Title = "加载外部XAML";
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>Height = 150;
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>Width = 225;
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>// 从XAML文件加载
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>using FileStream fsIn = new(XAML_FILE, FileMode.Open, FileAccess.Read);
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>FrameworkElement layout = (FrameworkElement)XamlReader.Load(fsIn);
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>// 两个按钮要处理事件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>Button btn1 = (Button)layout.FindName("btn1");
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>Button btn2 = (Button)layout.FindName("btn2");
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>btn1.Click += OnClick1;
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>btn2.Click += OnClick2;
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>Content = layout;
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>}

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>private void OnClick2(object sender, RoutedEventArgs e)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>{
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>MessageBox.Show("第二个按钮");
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>}

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>private void OnClick1(object sender, RoutedEventArgs e)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>{
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>MessageBox.Show("第一个按钮");
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>}
}这个不复杂,咱们关注加载 XAML 部分。通过文件流 FileStream 读取文件,而后在 XamlReader.Load 中加载。Load 方法返回的是 object 类型的对象,咱们要适当地进行类型转换。这个例子里面其实加载上来的是 Grid 类,但这里我只转换为 FrameworkElement 就可以了,毕竟我后面只用到了 FindName 方法。Find 出来的是两个 Button 对象,最后处理一下 Click 事件。
 
三、在项目中添加 MyWindow.xaml 文件。
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>Margin="12">
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><Grid.RowDefinitions>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><RowDefinition Height="auto"/>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><RowDefinition Height="auto"/>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary></Grid.RowDefinitions>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><Button Name="btn1" Grid.Row="0" Margin="5,8">按钮A</Button>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><Button Name="btn2" Grid.Row="1" Margin="5,8">按钮B</Button>
</Grid>这里顺便说一下,保存 XAML 文件时最好用 UTF-8 编码,不然可能会报错。方法是在 VS 里,【文件】-【XXX 另存为】。在保存文件对话框中,点“保存”按钮右边的箭头,选择“编码保存”。

编码选 UTF-8 无签名(或带签名的也行)。

另一种方法是用记事本打开,再以 UTF-8 保存。
 
四、在项目中添加 styles.xaml。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>里面包含一个 Button 控件模板。
 
五、在 Main 方法中,初始化 Application 类,并且从外部 XAML 中加载资源字典。
static void Main(string[] args){<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>Application app = new();<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>using FileStream extFile = new FileStream("styles.xaml", FileMode.Open, FileAccess.Read);<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(extFile);<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>app.Resources = dic;<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>app.Run(new MyWindow());}由于是在 app 处加载的资源,所以按钮样式会应用到整个程序。
 
六、打开项目文件(*.csproj),我们要做点手脚。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   
</ResourceDictionary>老周解释一下加了颜色的部分。
1、Page 表示XAML文件最终生成二进制文件,且塞进目标程序集中。加了 Remove 表示排除这一行为。说人话就是:本项目不编译 XAML 文件。
2、None 表示该项目中 XAML 文件“啥也不是”,编译时不做任何处理。
3、PostBuild 任务指定一条命令,在生成项目之后执行。此处是把项目目录下的 XAML 文件复制到输出目录。$(OutDir) 在 VS 中表示宏(也是 MSBuild 的属性)。在命令实际执行时,替换为实际目录路径,如 bin\Debug\net7.0-windows。
也可以用 $(TargetDir),不过 TargetDir 替换的是完整路径,OutDir 是用相对路径的。
 
现在生成一下项目,若没有问题,在输出目录下除了程序文件,还有那两个 XAML 文件。运行一下。

关闭程序,用记事本打开 styles.xaml 文件,把按钮的背景色改成橙色。

保存并关闭文件,重新运行程序。

咱们并没有重新编译程序。接下来用记事本打开 MyWindow.xaml 文件,改一下按钮上的文本。

保存并关闭文件,不用编译代码,再次运行程序。

 
这样就很方便修改了,不必每次都重新编译。 
下一篇老周还会说说纯代码写 WPF 的模板问题。三维图形就看心情了。因为 3D 图形的构造和一般控件应用差不多,就是用代码建立 WPF 对象树。

来源:https://www.cnblogs.com/tcjiaan/archive/2023/06/16/17486037.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【.NET深呼吸】将XAML放到WPF程序之外