WPF 后台动态调用样式文件,就像CSS样式表文件一样

本文介绍了在WPF应用中如何在后台通过反射动态调用DLL项目中的样式文件,解决App.xaml文件引用失效的问题。详细步骤包括设置只运行一个实例的代码、加载样式资源字典的方法以及样例代码。
摘要由CSDN通过智能技术生成
应用场合:如果您的WPF应用程序设置WPF运行一个实例代码后,App.xaml文件中对样式资源字典文件的引用将失效.

解决办法1:在App.xaml.cs文件中用反射动态调用另外一个DLL项目中的样式文件即可

解决办法2:在每个窗体的xaml文件中添加对指定样式文件的引用(本文不做介绍)

详细操作介绍如下:

1、WPF设置只运行一个实例代码:

App.xaml文件代码如下:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ButtonStyle.xaml"/>
</ResourceDictionary>
</Application.Resources>
</Application>

App.xaml.cs文件代码如下:

//添加引用
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace WpfUI
{

/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{

public App()
{
}

/// <summary>
/// 要设置App.xaml的文件属性中生成操作=无
/// </summary>
[STAThread]
public static void Main()
{
App myApp = new App();
myApp.ShutdownMode = ShutdownMode.OnExplicitShutdown;
myApp.Run();
}

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
if (e.Exception is InvalidOperationException)
e.Handled = true;
}

protected override void OnStartup(StartupEventArgs e)
{
//获取当前运行WPF程序的进程实例
Process process = Process.GetCurrentProcess();
//遍历WPF程序的同名进程组
foreach (Process p in Process.GetProcessesByName(process.ProcessName))
{
if (p.Id != process.Id && (p.StartTime - process.StartTime).TotalMilliseconds <= 0)
{
p.Kill();//关闭进程
return;
}
}
base.OnStartup(e);
//启动登陆窗体,

TestWindow myWindow = new TestWindow();

myWindow.Show();
}

}

}

2、ButtonStyle.xaml样式文件内容如下:

<ResourceDictionary
xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:ed=" http://schemas.microsoft.com/expression/2010/drawing ">

<!--按钮样式-->
<Style x:Key="RedButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Silver"/>
<Setter Property="Height" Value="23"/>
</Style>
</ResourceDictionary>

3、TestWindow.xaml文件内容如下:

<Grid>

<Button x:Name="MyButton" Content="测试按钮" Style="{StaticResource RedButtonStyle}" Margin="144.988,8,0,0" HorizontalAlignment="Left" Width="57" FontSize="16" VerticalAlignment="Top" Height="24.687"/>

</Grid>

4、运行程序后发现按钮样式RedButtonStyle总提示找不到

5、 解决办法如下:

第一步: 新建一个Windows--类库项目WpfThems,将ButtonStyle.xaml拷贝过去,生成WpfThems.dll文件

第二步:在当前项目WpfUI中添加WpfThems项目引用

第三步:修改App.xaml.cs 文件代码:

添加一个函数
private void LoadStyleResource()
{

Assembly assembly = Assembly.LoadFrom("WpfThems.dll");
string packUri = @"/WpfThems;component/ButtonStyle.xaml");
ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri(packUri, UriKind.Relative)) as ResourceDictionary;
this.Resources.MergedDictionaries.Add(myResourceDictionary);

}

在OnStartup函数中添加上面样式加载函数的调用即可

protected override void OnStartup(StartupEventArgs e)
{

LoadAllStyleResource():

//后面的代码省略,请参考上面代码

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值