wpf開源控件MahApps.Metro

wpf開源控件MahApps.Metro

安裝

您能夠經過NuGet GUI(右鍵單擊您的項目,單擊Manage NuGet Packages,選擇Online並搜索MahApps.Metro)或使用Package Manager控制檯安裝MahApps.Metro。c#

PM> Install-Package MahApps.Metro

或使用軟件包管理器控制檯:app

PM> Install-Package MahApps.Metro -Pre

造型窗口

您能夠使用兩種方法使用MahApps.Metro設置Window的樣式:spa

修改XAML文件

安裝MahApps.Metro以後:code

  • 打開 MainWindow.xaml
  • 在打開的Window標記內添加此屬性。(這是您在XAML中引用其餘名稱空間的方式):
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"

    xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
  • 標籤更改成(請記住也要更改結束標籤!)

您應該有相似如下內容(請勿複製和粘貼):component

<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文件,以使其基類MainWindowMetroWindowXAML文件的類匹配。xml

// To access MetroWindow, add the following reference
using MahApps.Metro.Controls;

namespace WpfApplication
{
  public partial class MainWindow : MetroWindow
  {
    public MainWindow()
    {
      InitializeComponent();
    }
  }
}

可是在大多數狀況下,您能夠刪除基類(由於這是partialXAML應該處理的類):資源

namespace WpfApplication
{
  public partial class MainWindow
  {
    public MainWindow()
    {
      InitializeComponent();
    }
  }
}

使用內置樣式

MahApp.Metro的全部資源都包含在單獨的資源詞典中。爲了使大多數控件採用MahApps.Metro主題,您須要將ResourceDictionaries添加到App.xamlit

App.xaml(v2.0.0及更高版本)io

<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和更低版本)class

<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>
相關文章
相關標籤/搜索