2016-05-15 11 views
0

Ich bin neu bei WPF und XAML und verwende derzeit das MahApps-Framework, um das Windows-Metro-Theme für meine Anwendung zu erhalten.Wie erstellt man ein Basisfenster mit WPF und dem MahApps-Framework?

Ich folge mit diesem guide, um das Metro-Thema einzubeziehen.

Meine Frage ist, wie erstelle ich ein Basisfenster, das das MahApps Thema hat und dann andere Fenster von diesem Basisfenster erben können, so dass sie auch das Thema bekommen würden.

Vielen Dank für Ihre Hilfe!

Antwort

1

Hier ist eine kurze Anleitung zum Erstellen einer Basis MetroWindow und deren Verwendung.

1) Erstellen Sie eine Klasse mit dem Basisfenster (ohne XAML-Code)

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

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

2) erstellen aa Thema Ressourcenverzeichnis in Ihrem solutio, nennt es Generic.xaml (es ist nur ein Beispiel)

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) benutzen, um Ihre benutzerdefinierten Fenster anstelle der MetroWindow

using MahApps.Metro.Controls; 

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

und

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

Sie diese Probe auch code-sample repository in meinem GitHub MahAppsMetroSample finden.

Hoffe, das hilft!