【问题标题】:Navigate to another wpf without opening new windows在不打开新窗口的情况下导航到另一个 wpf
【发布时间】:2020-04-02 16:00:46
【问题描述】:

我是 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();

但是当我运行时它给了我中断异常 我也试过导航服务,但我发现它只与页面相关联 任何建议如何做到这一点? 谢谢

【问题讨论】:

标签: c# wpf visual-studio xaml navigationservice


【解决方案1】:

Frame 可以托管任何内容,甚至是 HTML。
Page 只公开了像 NavigationService 这样的特殊助手,以使页面之间的导航更加方便。

Window 不能是另一个元素的子元素,例如Frame 的孩子。它必须是根元素。通过将Window 分配给Frame.ContentFrame 成为Window 的父级,这是非法的。

一个简单的解决方案是将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");

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多