让你的 WPF 应用程序拥有漂亮的 Fluent 风格(不是 XAML 岛)


步骤

1. 创建一个新的 WPF 项目,或者打开已有的 WPF 项目。

废话不多说,准备好了之后往下走。

2. 打开 NuGet,并安装包 iNKORE.UI.WPF.Modern

下图这个包,现在的最新版本是 0.9.19,选最新的安装就好。

dotnet add package iNKORE.UI.WPF.Modern 

在这里插入图片描述

3. 修改项目文件

  • 如果你是用的是 .NET Framework,那么需要把 Framework 版本改到 4.5.2 之后。

  • 如果是 .NET 6.0,那么目标操作系统版本选择 10.0.18326.0,或者更新的也行。

在这里插入图片描述

4. 修改 App.xaml

首先在根节点添加命名空间引用:

xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"

然后添加资源字典:

 <Application.Resources>
     <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
             <ui:ThemeResources/>
             <ui:XamlControlsResources />
         </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
 </Application.Resources>

这样 App.xaml 就改完了,当然,如果想添加一些自定义内容,可以参考如下 App.xaml 完整示例:

<Application x:Class="WpfModernApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfModernApp1"
             xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
             xmlns:sys="clr-namespace:System;assembly=System.Runtime"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ui:ThemeResources CanBeAccessedAcrossThreads="False">

                    <ui:ThemeResources.ThemeDictionaries>
                        <ResourceDictionary x:Key="Light">
                            <SolidColorBrush x:Key="NavigationViewExpandedPaneBackground" Color="#FFEDEDED" />
                        </ResourceDictionary>
                        <ResourceDictionary x:Key="Dark">
                            <SolidColorBrush x:Key="NavigationViewExpandedPaneBackground" Color="#FF202020" />
                        </ResourceDictionary>
                    </ui:ThemeResources.ThemeDictionaries>

                </ui:ThemeResources>
                <ui:XamlControlsResources />

            </ResourceDictionary.MergedDictionaries>


            <Style x:Key="OptionsPanelStyle" TargetType="ui:SimpleStackPanel">
                <Setter Property="Spacing" Value="12" />
                <Style.Resources>
                    <Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox">
                        <Setter Property="MinWidth" Value="200" />
                    </Style>
                    <Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="TextBox">
                        <Setter Property="MinWidth" Value="200" />
                    </Style>
                    <Style BasedOn="{StaticResource {x:Type DatePicker}}" TargetType="DatePicker">
                        <Setter Property="MinWidth" Value="200" />
                    </Style>
                </Style.Resources>
            </Style>

            <Thickness x:Key="ControlPageContentMargin">24,0,24,20</Thickness>

            <Style x:Key="ControlPageContentPanelStyle" TargetType="ui:SimpleStackPanel">
                <Setter Property="Margin" Value="{StaticResource ControlPageContentMargin}" />
                <Setter Property="Spacing" Value="16" />
            </Style>

            <Style x:Key="ScrollableContentDialogStyle" TargetType="ui:ContentDialog">
                <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
                <Style.Resources>
                    <Thickness x:Key="ContentDialogContentMargin">24,0,24,0</Thickness>
                    <Thickness x:Key="ContentDialogContentScrollViewerMargin">-24,0,-24,0</Thickness>
                    <Thickness x:Key="ContentDialogTitleMargin">24,0,24,12</Thickness>
                </Style.Resources>
            </Style>

            <Style
             x:Key="RichTextBlockStyle"
             BasedOn="{StaticResource DefaultRichTextBoxStyle}"
             TargetType="RichTextBox">
                <Setter Property="Padding" Value="0" />
                <Setter Property="IsReadOnly" Value="True" />
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="RichTextBox">
                            <ui:ScrollViewerEx x:Name="PART_ContentHost" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Resources>
                    <Style TargetType="Paragraph">
                        <Setter Property="Margin" Value="0" />
                    </Style>
                </Style.Resources>
            </Style>

            <CornerRadius x:Key="ControlCornerRadius">4</CornerRadius>

            <LinearGradientBrush x:Key="HeroImageGradientBrush" StartPoint="0.5,0" EndPoint="0.5,1.5">
                <GradientStop Offset="0" Color="Transparent" />
                <GradientStop Offset="0.5" Color="{DynamicResource LayerFillColorDefault}" />
                <GradientStop Offset="1" Color="{DynamicResource LayerFillColorDefault}" />
            </LinearGradientBrush>

            <!--  Breakpoints  -->
            <sys:Double x:Key="Breakpoint640Plus">641</sys:Double>

            <Thickness x:Key="PageHeaderDefaultPadding">0</Thickness>
            <Thickness x:Key="PageHeaderMinimalPadding">-4,0,12,0</Thickness>

            <Thickness x:Key="ControlElementScreenshotModePadding">67</Thickness>

            <!--  L-Pattern Overwriting resources  -->
            <Thickness x:Key="NavigationViewContentMargin">0,48,0,0</Thickness>
            <Thickness x:Key="NavigationViewContentGridBorderThickness">1,1,0,0</Thickness>
            <CornerRadius x:Key="NavigationViewContentGridCornerRadius">8,0,0,0</CornerRadius>
            <Thickness x:Key="NavigationViewHeaderMargin">56,34,0,0</Thickness>

            <SolidColorBrush x:Key="GridViewHeaderItemDividerStroke" Color="Transparent" />

            <sys:String x:Key="ControlsName">All controls</sys:String>
            <sys:String x:Key="AppTitleName">ModernWPF Gallery</sys:String>
            <sys:String x:Key="WinUIVersion">SDK 0.10.0</sys:String>

            <sys:String x:Key="NewControlsName">What's New</sys:String>

            <Style x:Key="ControlPageScrollStyle" TargetType="ScrollViewer">
                <Setter Property="VerticalScrollBarVisibility" Value="Auto" />
            </Style>

            <Style x:Key="GridViewItemStyle" TargetType="ui:GridViewItem">
                <Setter Property="Margin" Value="0,0,12,12" />
            </Style>

            <Style x:Key="IndentedGridViewItemStyle" TargetType="ui:GridViewItem">
                <Setter Property="Margin" Value="12,0,0,12" />
            </Style>

            <Style x:Key="GridViewItemStyleSmall" TargetType="ui:GridViewItem">
                <Setter Property="Margin" Value="0,0,0,12" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>

            <Style x:Key="IndentedGridViewItemStyleSmall" TargetType="ui:GridViewItem">
                <Setter Property="Margin" Value="12,0,12,12" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>

            <sys:Double x:Key="TeachingTipMinWidth">50</sys:Double>
        </ResourceDictionary>
    </Application.Resources>
</Application> 

4. 修改 MainWindow.xaml

第 3 步做完之后,可以发现控件的风格已经变了,如果想要让窗口的风格也变得话,可以继续往下走。
同样的方法,先添加命名空间

xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"

然后添加这些属性:

     ui:ThemeManager.IsThemeAware="True"
     ui:WindowHelper.SystemBackdropType="Mica"
     ui:WindowHelper.UseModernWindowStyle="True"

这样就为窗口开启了 Modern 样式,并使用云母效果(仅 w11)。

总结

我把这个教程的代码打包了,如果需要的话可以在这里下载。

https://download.csdn.net/download/qq_24888859/88389808

iNKORE.UI.WPF.Modern 是一个免费并且完全开源的库。上面的教程只展示了其很小的一部分,还有很多功能和控件可以探索,比如深色模式,主题色与系统同步,AppBar,NavigationView 等。下面是这套控件库的 Gallery 截图:

在这里插入图片描述

整套控件库的源代码都可以在 Github 上查看和下载,喜欢的话记得 star 哦
https://github.com/InkoreStudios/UI.WPF.Modern 欢迎大家的使用和贡献!


这里附上一张使用这套控件库开发的软件截图(顺便打个广告,手动狗头 图中软件为 MCSkinn,一款 Minecraft 皮肤编辑器,有兴趣看这里 InkoreStudios/MCSkinn

在这里插入图片描述在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值