手把手一起使用WPF开源UI框架MahApps.Metro

1、创建新项目

使用Visual Studio 2022创建新项目,如图点击创建新项目

在这里插入图片描述

选择WPF应用(.NET Framework)

在这里插入图片描述

配置新项目,如图:

在这里插入图片描述
随便写几个WPF默认样式控件:

在这里插入图片描述

2、安装 MahApps.Metro

点击工具->NuGet包管理器->管理解决方案的NuGet程序包,如图所示:

在这里插入图片描述
搜索MahApps,点击第一个,安装即可:

在这里插入图片描述
点击确认,如图:

在这里插入图片描述
安装完成,如图所示:

在这里插入图片描述

3、App.xaml修改代码

在App.xaml中修改如下代码:

<Application x:Class="FMSH_NVM_WPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:FMSH_NVM_WPF"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <!-- Theme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

如图所示:

在这里插入图片描述
此时,重新生成并运行程序,窗体内部的控件样式已经发生改变,如图:

在这里插入图片描述

4、MainWindow.xaml修改代码

在MainWindow.xaml中修改如下代码:

<mah:MetroWindow x:Class="FMSH_NVM_WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FMSH_NVM_WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="226,67,0,0" VerticalAlignment="Top"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="454,86,0,0" VerticalAlignment="Top"/>
        <Calendar HorizontalAlignment="Left" Margin="136,149,0,0" VerticalAlignment="Top"/>
        <TabControl Margin="390,170,10,22">
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>

    </Grid>
</mah:MetroWindow>

如图所示:

在这里插入图片描述
打开文件MainWindow.xaml.cs,修改代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;

namespace FMSH_NVM_WPF
{
    
    
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
    
    
        public MainWindow()
        {
    
    
            InitializeComponent();
        }
    }
}

如图所示:

在这里插入图片描述
保存后重新编译运行,窗体的样式已经发生改变,如图所示:

在这里插入图片描述
MahApps官网: https://mahapps.com/

希望本文对大家有帮助,上文若有不妥之处,欢迎指正

分享决定高度,学习拉开差距

猜你喜欢

转载自blog.csdn.net/qq_42078934/article/details/132049765