使用 MahApps 在 WPF 中切换页面

Switch Pages In WPF using MahApps

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

我在 window.

中使用 MahApps 创建的页面之间切换时遇到问题

这是我的开始 window (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>

这是我要移动到的页面(我在主页面创建了一个框架,所以我可以把这个页面的内容放在上面):

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

提前致谢!

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

以下是您需要制作的模组:

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

  2. 在您的 MainWindow.xaml.cs 中删除 MainWindow 的基础 class 引用(MainWindow : Window 变为只是主要Window)

  3. 在您的 MainMenu.xaml 中将 Controls:MetroWindow 更改为 UserControl

  4. 返回您的 MainWindow.xaml,添加对您的命名空间的引用 xmlns:my="clr-namespace:WhateverHere"

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

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>