导航到另一个WPF,而无需打开新窗口

时间:2020-04-02 16:00:46

标签: c# wpf visual-studio xaml navigationservice

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

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

cs代码

            main.Content = new Window1();

但是当我运行它给我中断异常 我也尝试了导航服务,但发现它仅与Pages相关 有什么建议怎么做? 谢谢

1 个答案:

答案 0 :(得分:0)

Frame可以托管任何内容,甚至HTML。
Page仅提供NavigationService之类的特殊帮助程序,以使页面之间的导航更加方便。

Window can not be the child of another element e.g. child of框架. It must be the root element. By assigning the窗口to框架。内容the框架beceomes the parent of the窗口`。

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

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

现在,您的分配将生效:

main.Content = new MyUserControl();

main.Navigate(new MyUserControl());

main.Navigate("file path to/MyUserControl.xaml");
相关问题