2016-05-15 122 views
0

我是WPF和XAML的新手,我正在使用MahApps框架來獲取我的應用程序的Windows Metro主題。如何使用WPF和MahApps框架創建基本窗口?

我在沿着使用這個guide獲得Metro主題。

我的問題是如何創建一個具有MahApps主題的基本窗口,然後其他窗口可以從此基本窗口繼承,這樣他們也可以獲得主題。

謝謝你的幫助!

回答

1

這裏是一個簡短的如何創建一個基地MetroWindow和它的用法。

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)建立在你的搜索解決方案AA主題資源字典,把它Generic.xaml(這只是一個例子)

enter image description here

<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)使用自定義窗口,而不是MetroWindow

using MahApps.Metro.Controls; 

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

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

您也可以找到這個樣品在我的GitHub MahAppsMetroSample code-sample repository

希望這會有所幫助!