我的 WPF 應用程式太亮了,我想為其應用深色主題。
在我的電腦上,Explorer 使用的是深色主題。我可以在我的 WPF 應用程式上使用與 Explorer 相同的主題嗎?如果是這樣,我該怎么做?
我見過一些例子說我可以將它添加到我的App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
這行得通,我的主題現在改變了。但是其他可用的主題是什么?它們長什么樣?
而且我想既然“Aero2”是一個東西,這可能看起來更現代。我嘗試將Source屬性更改為/PresentationFramework.Aero2;component/themes/Aero.NormalColor.xaml(注意“2”),但它引發了錯誤:
IOException: Cannot locate resource 'themes/aero.normalcolor.xaml'.
uj5u.com熱心網友回復:
切換 WPF 內置主題
對于 .NET Core 3.1,您必須將主題字典合并到應用程式資源中。
<Application x:Class="YourApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFThemes"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
對于.NET Framework,您也必須合并主題資源字典,但您還必須在專案中參考相應的主題程式集。在您的專案中,右鍵單擊References,選擇Add Reference...并展開Assemblies樹項。搜索PresentationFramework,然后選中您要參考的主題程式集的復選框并單擊OK。

內置主題
AeroLite - Windows 8 到 10 類似的主題
/PresentationFramework.AeroLite;component/themes/AeroLite.NormalColor.xamlAero2 - Windows 8 到 10 類似的主題
/PresentationFramework.Aero2;component/themes/Aero2.NormalColor.xamlAero - Windows 7 主題
/PresentationFramework.Aero;component/themes/Aero.NormalColor.xamlLuna - Windows XP 主題
- 綠色變種
/PresentationFramework.Luna;component/themes/Luna.NormalColor.xaml - 銀色變體
/PresentationFramework.Luna;component/themes/Luna.Metallic.xaml - 橄欖變種
/PresentationFramework.Luna;component/themes/Luna.Homestead.xaml
- 綠色變種
Royale - Windows XP 媒體中心主題
/PresentationFramework.Royale;component/themes/Royale.NormalColor.xaml經典 - Windows 95/98/2000 類似主題
/PresentationFramework.Classic;component/themes/Classic.xaml
其他主題
On my computer, Explorer is using a dark theme. Can I use the same theme as Explorer on my WPF application? And if so, how do I do that?
No, you cannot. There is no built-in theme for light and dark mode. The most suitable theme for your application will be picked under the current operating system if you did not specify or override this behavior otherwise. For me on Windows 10 that is Aero2.
If you want to have a more modern theme with light and dark mode support, you have to look at thrid party libraries like MahApps.Metro or MaterialDesignInXAML. Those also provided automatic theme switching based on the operating system light or dark setting.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/443425.html
