1、創(chuàng)建新項(xiàng)目
使用Visual Studio 2022創(chuàng)建新項(xiàng)目,如圖點(diǎn)擊創(chuàng)建新項(xiàng)目
:
選擇WPF應(yīng)用(.NET Framework)
:
配置新項(xiàng)目,如圖:
隨便寫(xiě)幾個(gè)WPF默認(rèn)樣式控件:
2、安裝 MahApps.Metro
點(diǎn)擊工具->NuGet包管理器->管理解決方案的NuGet程序包,如圖所示:
搜索MahApps,點(diǎn)擊第一個(gè),安裝即可:
點(diǎn)擊確認(rèn)
,如圖:
安裝完成,如圖所示:
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>
如圖所示:
此時(shí),重新生成并運(yùn)行程序,窗體內(nèi)部的控件樣式已經(jīng)發(fā)生改變,如圖:
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>
如圖所示:
打開(kāi)文件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();
}
}
}
如圖所示:
保存后重新編譯運(yùn)行,窗體的樣式已經(jīng)發(fā)生改變,如圖所示:
MahApps官網(wǎng): https://mahapps.com/
希望本文對(duì)大家有幫助,上文若有不妥之處,歡迎指正文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-698026.html
分享決定高度,學(xué)習(xí)拉開(kāi)差距文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-698026.html
到了這里,關(guān)于手把手一起使用WPF開(kāi)源UI框架MahApps.Metro的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!