wpf開源控件MahApps.Metro


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標記內添加此屬性。(這是您在XAML中引用其他名稱空間的方式):
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"

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

您應該有類似以下內容(請勿復制和粘貼):

<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文件的類匹配。

// 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.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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM