Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MahApps在WPF中切换页面_C#_Wpf_Mahapps.metro - Fatal编程技术网

C# 使用MahApps在WPF中切换页面

C# 使用MahApps在WPF中切换页面,c#,wpf,mahapps.metro,C#,Wpf,Mahapps.metro,首先,感谢您花时间阅读本文。 我对WPF非常陌生,尤其是使用MahApps- 我在窗口中使用MahApps创建的页面之间切换时遇到问题 这是我的开始窗口(主窗口): 这是我想要移动到的页面(我在主框架中创建了一个框架,以便将此页面的内容放在其中): 提前谢谢你 最新答案: 框架本身用于加载页面,而您的菜单项是从窗口继承的,因此永远不会工作 以下是您需要制作的MOD: 在MainWindow.xaml中,将窗口标记更改为Controls:MetroWindow(就像主菜单中的xaml一

首先,感谢您花时间阅读本文。 我对WPF非常陌生,尤其是使用MahApps-

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

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


这是我想要移动到的页面(我在主框架中创建了一个框架,以便将此页面的内容放在其中):


提前谢谢你

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

以下是您需要制作的MOD:

  • 在MainWindow.xaml中,将窗口标记更改为Controls:MetroWindow(就像主菜单中的xaml一样)

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

  • 在main menu.xaml中,将控件:MetroWindow更改为UserControl

  • 回到MainWindow.xaml,向名称空间xmlns:my=“clr namespace:WhateverHere”添加一个ref

  • 在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>
    
    main menu.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>
    
    
    
    请提供一个确定的问题。添加了一些代码您好,感谢您的回复。我尝试过这种方法,但不幸的是我收到了这样一条消息:我认为这是因为主菜单是一种类型,而不是常规的……不确定如何解决它。我正在查看我以前与mahapps合作的项目,以供参考,现在正在更新答案。谢谢!可以吗我在usercontrols中使用MahApps控件?当然,您只需要在用户控件xaml中添加对MahApps控件的引用。如果我的答案解决了您的问题,请不要忘记将其标记为已接受的答案,以供其他用户将来参考。
        <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>
    
        public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    
        <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>