C# 导航到另一个wpf而不打开新窗口

C# 导航到另一个wpf而不打开新窗口,c#,wpf,visual-studio,xaml,navigationservice,C#,Wpf,Visual Studio,Xaml,Navigationservice,我是WPF新手,我找不到在同一个主WPF应用程序上打开新WPF窗口的方法 我尝试了框架方法,下面是代码:- <Window x:Class="WPF_FINAL.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="

我是WPF新手,我找不到在同一个主WPF应用程序上打开新WPF窗口的方法 我尝试了框架方法,下面是代码:-

<Window x:Class="WPF_FINAL.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_FINAL"
        mc:Ignorable="d"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        TextElement.FontWeight="Regular"
        TextElement.FontSize="13"
        TextOptions.TextFormattingMode="Ideal"
        TextOptions.TextRenderingMode="Auto"
        Background="{DynamicResource MaterialDesignPaper}"
        Height="768"
        Width="1366"
        WindowState="Maximized"
        Title="MainWindow">

    <Grid Background="#dff9fb"
          Margin="33,10,-33,-10">

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="13.5" />
            <ColumnDefinition Width="152" />
            <ColumnDefinition Width="auto"
                              MinWidth="335.5" />
            <ColumnDefinition />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="20" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="20" />
        </Grid.RowDefinitions>
        <Frame Margin="0,0,0.5,10"
               Grid.ColumnSpan="5"
               x:Name="main"
               Grid.RowSpan="6">

        </Frame>
    </Grid>
</Window>
但当我运行时,它会给我中断异常 我也尝试了导航服务,但我发现它只与网页相关 有什么建议吗?
谢谢

一个
框架可以承载任何内容,甚至HTML。
页面
仅公开特殊的帮助程序,如
导航服务
,以使页面之间的导航更加方便

窗口
不能是另一个元素的子元素,例如
框架
的子元素。它必须是根元素。通过将
窗口
分配给
框架内容
框架
成为
窗口
的父级,这是非法的

一个简单的解决方案是将
Window1
类转换为
UserControl

<UserControl x:Class="MyUserControl">
  <TextBlock Text="TEST CONTROL" FontSize="25"/>
</UserControl>


框架可以承载任何内容,甚至HTML。
页面
仅公开特殊的帮助程序,如
导航服务
,以使页面之间的导航更加方便

窗口
不能是另一个元素的子元素,例如
框架
的子元素。它必须是根元素。通过将
窗口
分配给
框架内容
框架
成为
窗口
的父级,这是非法的

一个简单的解决方案是将
Window1
类转换为
UserControl

<UserControl x:Class="MyUserControl">
  <TextBlock Text="TEST CONTROL" FontSize="25"/>
</UserControl>


不,那不是办法,艾哈迈德!为此,您应该使用
UserControl
Pages
看看这个tut:
UserControl
就像一个没有框架的
窗口(默认情况下)ok,您可以像窗口一样实例化和使用它。但与窗口不同,您可以在窗口中嵌入
userControl
。希望有帮助:)还有一个弹出控件,它可以创建一个完全基于用户界面的短期对话框。不,那不是办法,艾哈迈德!为此,您应该使用
UserControl
Pages
看看这个tut:
UserControl
就像一个没有框架的
窗口(默认情况下)ok,您可以像窗口一样实例化和使用它。但与窗口不同,您可以在窗口中嵌入
userControl
。希望有帮助:)还有一个弹出控件,它可以创建一个完全基于用户界面的短期对话框。
main.Content = new MyUserControl();
main.Navigate(new MyUserControl());
main.Navigate("file path to/MyUserControl.xaml");