wpf開源控件MahApps.Metro
安裝
您可以通過NuGet GUI(右鍵單擊您的項目,單擊Manage NuGet Packages,選擇Online并搜索MahApps.Metro)或使用Package Manager控制臺安裝MahApps.Metro。
PM> Install-Package MahApps.Metro
或使用軟件包管理器控制臺:
PM> Install-Package MahApps.Metro -Pre
造型窗口
您可以使用兩種方法使用MahApps.Metro設置Window的樣式:
修改XAML文件
安裝MahApps.Metro之后:
- 打開
MainWindow.xaml
- 在打開的Window標記內(nèi)添加此屬性。(這是您在XAML中引用其他名稱空間的方式):
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
或
xmlns:Controls="http://metro./winfx/xaml/controls"
- 將
標簽更改為 (請記住也要更改結束標簽?。?/li>
您應該有類似以下內(nèi)容(請勿復制和粘貼):
<Controls:MetroWindow x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="MainWindow"
Height="600"
Width="800">
<!-- your content -->
</Controls:MetroWindow>
修改CodeBehind文件
您還需要修改MainWindow.xaml.cs 文件,以使其基類MainWindow 與MetroWindow XAML文件的類匹配。
// To access MetroWindow, add the following reference
using MahApps.Metro.Controls;
namespace WpfApplication
{
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
但是在大多數(shù)情況下,您可以刪除基類(因為這是partial XAML應該處理的類):
namespace WpfApplication
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
使用內(nèi)置樣式
MahApp.Metro的所有資源都包含在單獨的資源詞典中。為了使大多數(shù)控件采用MahApps.Metro主題,您需要將ResourceDictionaries添加到App.xaml 。
App.xaml(v2.0.0及更高版本)
<Application x:Class="WpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
App.xaml(v1.6.5和更低版本)
<Application x:Class="WpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
|