简体   繁体   English

如何使用WPF和MahApps框架创建基本窗口?

[英]How to create a base window using WPF and the MahApps framework?

I am new to WPF and XAML and I am currently using the MahApps framework to get the Windows Metro theme for my application. 我是WPF和XAML的新手,目前正在使用MahApps框架来获取应用程序的Windows Metro主题。

I am following along using this guide to get the Metro theme incorporated. 我将按照本指南进行操作,以将Metro主题纳入其中。

My question is how do I create a base window that has the MahApps theme and then other windows can inherit from this base window so they also would get the theme. 我的问题是如何创建具有MahApps主题的基本窗口,然后其他窗口可以从该基本窗口继承,以便它们也可以获取主题。

Thank you for your help! 谢谢您的帮助!

Here is a short how to for creating a base MetroWindow and it's usage. 这是一个简短的如何创建基本MetroWindow及其用法。

1) Create a class with your base window (without any xaml code) 1)用您的基本窗口创建一个类(没有任何XAML代码)

using System.Windows;
using MahApps.Metro.Controls;

namespace MahAppsMetroSample
{
    public class CustomBaseMetroWindow : MetroWindow
    {
        static CustomBaseMetroWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomBaseMetroWindow), new FrameworkPropertyMetadata(typeof(CustomBaseMetroWindow)));
        }
    }
}  

2) create aa theme resource dictionary in your solutio, call it Generic.xaml (it's only an example) 2)在您的解决方案中创建一个主题资源字典,将其称为Generic.xaml (仅是示例)

在此处输入图片说明

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:mahAppsMetroSample="clr-namespace:MahAppsMetroSample"
                    xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/MetroWindow.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="mahAppsMetroSample:CustomBaseMetroWindow" BasedOn="{StaticResource {x:Type controls:MetroWindow}}">
        <Setter Property="TitleCharacterCasing" Value="Lower" />
        <Setter Property="WindowTransitionsEnabled" Value="False" />
        <Setter Property="WindowTitleBrush" Value="Brown" />
    </Style>

</ResourceDictionary>

3) use your custom window instead the MetroWindow 3)使用您的自定义窗口代替MetroWindow

using MahApps.Metro.Controls;

namespace MahAppsMetroSample
{
    public partial class MainWindow : CustomBaseMetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

and

<mahAppsMetroSample:CustomBaseMetroWindow x:Class="MahAppsMetroSample.MainWindow"
                                          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                          xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                                          xmlns:mahAppsMetroSample="clr-namespace:MahAppsMetroSample"
                                          Title="MainWindow">

    <Grid>
    </Grid>
</mahAppsMetroSample:CustomBaseMetroWindow>

You can find this sample also in my GitHub MahAppsMetroSample code-sample repository . 您也可以在我的GitHub MahAppsMetroSample 代码示例存储库中找到此示例。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM