使用MahApps在WPF中切换页面

时间:2017-07-05 18:10:21

标签: c# wpf mahapps.metro

首先,感谢您花时间阅读本文。 我对WPF很新,特别是使用MahApps - http://mahapps.com

我在窗口中使用MahApps创建的页面之间切换时遇到问题。

这是我的开始窗口(MainWindow):

<Window
                  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"
                  xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro" x:Class="WpfApplication.MainWindow"
                  xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                  Dialog:DialogParticipation.Register="{Binding}"
                  Title="MainWindow"
                  Height="600"
                  Width="800">
<Grid>
    <Frame x:Name="Main"/>
</Grid>

这是我要移动的页面(我在main中创建了一个框架,因此我可以将此页面的内容放在其上):

<Controls:MetroWindow
                  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"
                  xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro" x:Class="WpfApplication.MainMenu"
                  xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                  Dialog:DialogParticipation.Register="{Binding}"
                  Title="MainMenu"
                  Height="600"
                  Width="800" NonActiveBorderBrush="#FFC32C2C">
<Grid>

    <Image x:Name="Background" Source="C:\Users\User\Desktop\Iddo Work\C#Learning\WpfApplication/teaserBackground.jpg" Stretch="UniformToFill">
        <Image.BitmapEffect>
            <BlurBitmapEffect Radius="17" />
        </Image.BitmapEffect>
    </Image>
    <Image x:Name="Logo" Source="C:\Users\User\Desktop\Iddo Work\C#Learning\WpfApplication/Trivia.png" Margin="0,60,0,320" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    <TextBox x:Name="Username" Controls:TextBoxHelper.Watermark="       USERNAME" Margin="0,308,0,226" Width="200" Height="35" TextAlignment="Justify" FontSize="20" FontWeight="Bold" HorizontalAlignment="Center" />
    <TextBox x:Name="Password" Controls:TextBoxHelper.Watermark="       PASSWORD" Margin="0,378,0,156" Width="200" Height="35" TextAlignment="Justify" FontSize="20" FontWeight="Bold" />
    <Button x:Name="SignIn" Content="" HorizontalAlignment="Left" Margin="407,435,0,0" VerticalAlignment="Top" Width="80" Style="{DynamicResource MetroCircleButtonStyle}" Height="80" BorderBrush="{x:Null}" Foreground="{x:Null}" Click="SignIn_Click">
        <Button.Background>
            <ImageBrush ImageSource="C:\Users\User\Desktop\Iddo Work\C#Learning\WpfApplication/signIn.png"/>
        </Button.Background>
    </Button>
    <Button x:Name="SignUp" Content="" HorizontalAlignment="Left" Margin="310,435,0,0" VerticalAlignment="Top" Width="80" Style="{DynamicResource MetroCircleButtonStyle}" Height="80" BorderBrush="{x:Null}" Foreground="{x:Null}" Click="SignUp_Click">
        <Button.Background>
            <ImageBrush ImageSource="C:\Users\User\Desktop\Iddo Work\C#Learning\WpfApplication/sign-add-icon.png"/>
        </Button.Background>
    </Button>

</Grid>

提前谢谢!

1 个答案:

答案 0 :(得分:0)

更新的答案: 框架本身用于加载页面,而您的菜单项是从窗口继承的,因此永远不会起作用。

以下是您需要制作的mod:

  1. 在您的MainWindow.xaml中,将Window标记更改为Controls:MetroWindow(就像您在MainMenu xaml中一样

  2. 在MainWindow.xaml.cs中删除MainWindow的基类引用(MainWindow:Window变为MainWindow)

  3. 在您的MainMenu.xaml更改控件:MetroWindow到UserControl

  4. 返回MainWindow.xaml,在命名空间中添加一个引用xmlns:my =“clr-namespace:WhateverHere”

  5. 在MainWindow.xaml中,直接删除并添加您的控件

  6. MainWindow.xaml

        <Controls:MetroWindow x:Class="WPFDeleteMe.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"
                xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                xmlns:my="clr-namespace:WPFDeleteMe"
                Title="MainWindow" Height="700" Width="900">
            <Grid x:Name="RootGrid">
                <my:MainMenu></my:MainMenu>
            </Grid>
        </Controls:MetroWindow>
    

    MainWindow.xaml.cs

        public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    

    MainMenu.xaml

        <UserControl x:Class="WPFDeleteMe.MainMenu"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     mc:Ignorable="d" 
                     d:DesignHeight="600" d:DesignWidth="800">
            <Grid>
                <Image x:Name="Background" Source="d:\images\doll-161405_960_720.png" Stretch="UniformToFill">
                    <Image.BitmapEffect>
                        <BlurBitmapEffect Radius="17" />
                    </Image.BitmapEffect>
                </Image>
                <Image x:Name="Logo" Source="d:\images\Carestream_Solutions.gif" Margin="0,0,0,0" Width="150" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                <Button x:Name="SignIn" Content="" HorizontalAlignment="Left" Margin="407,435,0,0" VerticalAlignment="Top" Width="80" Height="80" BorderBrush="{x:Null}" Foreground="{x:Null}" Click="SignIn_Click">
                    <Button.Background>
                        <ImageBrush ImageSource="d:\images\war_kitten.jpg"/>
                    </Button.Background>
                </Button>
                <Button x:Name="SignUp" Content="" HorizontalAlignment="Left" Margin="310,435,0,0" VerticalAlignment="Top" Width="80" Height="80" BorderBrush="{x:Null}" Foreground="{x:Null}" Click="SignUp_Click">
                    <Button.Background>
                        <ImageBrush ImageSource="d:\images\war_kitten.jpg"/>
                    </Button.Background>
                </Button>
            </Grid>
        </UserControl>