欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

wpf开源控件MahApps.Metro

程序员文章站 2022-08-31 11:47:20
wpf开源控件MahApps.Metro 安装 您可以通过NuGet GUI(右键单击您的项目,单击 Manage NuGet Packages ,选择 Online 并搜索 MahApps.Metro )或使用Package Manager控制台安装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>