WPF后台动态调用样式文件

应用场合:如果您的WPF应用程序设置WPF运行一个实例代码后,App.xaml文件中对样式资源字典文件的引用将失效.

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

详细操作介绍如下:

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);
//启动登陆窗体,

MainWindow myWindow = new MainWindow();

myWindow.Show();
}

}

}

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

<ResourceDictionary xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”>
<!–按钮样式–>
<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、MainWindow.xaml文件内容如下:

<Window x:Class=”WpfUI.MainWindow”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”MainWindow” Height=”350″ Width=”525″>
<Grid>
<Button Content=”Button” Style=”{StaticResource RedButtonStyle}” Height=”23″ HorizontalAlignment=”Left” Margin=”102,66,0,0″ Name=”button1″ VerticalAlignment=”Top” Width=”75″ />
</Grid>
</Window>

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

5、 解决办法如下:

第一步: 新建一个Windows–类库项目WpfThems,将ButtonStyle.xaml拷贝过去, 设置ButtonStyle.xaml文件的属性生成操作为 “Page”,之后生成WpfThems.dll文件

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

第三步:修改App.xaml.cs 文件代码:添加一个函数LoadStyleResource并修改OnStartup函数内容。修改后的App.xaml.cs文件内容如下:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
//添加引用
using System.Diagnostics;
using System.IO;
using System.Reflection;

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

public App()
{
}

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

/// <summary>
/// 重载应用程序启动函数
/// </summary>
/// <param name=”e”></param>
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);
//动态调用样式文件
LoadStyleResource();

//启动窗体
MainWindow myWindow = new MainWindow();
myWindow.Show();
}
private void LoadStyleResource()
{
Assembly assembly = Assembly.LoadFrom(“WpfThemes.dll”);
string packUri = @”/WpfThemes;component/ButtonStyle.xaml”;
ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri(packUri, UriKind.Relative)) as ResourceDictionary;
this.Resources.MergedDictionaries.Add(myResourceDictionary);
}
}
}

 

解决办法2:在每个窗体的xaml文件中添加对指定样式文件的引用

<Window.Resources>

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary Source=”ButtonStyle.xaml”/>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

</Window.Resources>

详细工程项目请到我的下载资源中下载: http://download.csdn.net/detail/xqf222/5582575

标签