【发布时间】:2016-10-29 18:30:38
【问题描述】:
我在 WinForms 应用程序中托管 WPF UserControl(实际上是几个)。
由于 Win7 (Aero)、Win8 (Aero2) 和(我假设)Win10 的默认主题之间的视觉差异,我试图指定最低公分母主题 (Aero) 并从那里定制我的 UI,因此希望避免任何操作系统主题问题。
据我了解,问题有两个方面:1)没有 System.Windows.Application 对象,因为它托管在 WinForms 项目中,所以我必须创建一个和 2)我必须指定我想使用的主题.
第一点thanks to this Dr. Wpf blog post 非常简单,可以使用EnsureWpfApplicationResources() 方法解决(字符串在有助于可读性的地方被拆分):
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
EnsureWpfApplicationResources();
AssignWin7Theme();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myWinForm());
}
static void EnsureWpfApplicationResources()
{
if (Wpf.Application.Current == null)
{
// create the wpf application object
new Wpf.Application(); // autoassigns to Wpf.Application.Current
}
}
static void AssignWin7Theme()
{
Uri uri = new Uri(
"PresentationFramework.Aero;V4.0.0.0;" +
"31bf3856ad364e35;component\\themes/aero.normalcolor.xaml",
UriKind.Relative);
Wpf.Application.Current.Resources.MergedDictionaries.Add(
Wpf.Application.LoadComponent(uri) as Wpf.ResourceDictionary);
}
}
AssignWin7Theme() 是我从 Eli Arbel 的 this 博客文章中派生出来的,这给我带来了麻烦。代码运行良好(不会引发异常),但我的控件的外观在 Win8 上没有改变,以匹配我在 Win7 上看到的。我认为它应该自动选择这个设置;我需要在每个控件的 XAML 中设置一个属性吗?我在这里还有什么问题吗?
【问题讨论】: