【问题标题】:How to use one Page object in multiple Frames in another Page?如何在另一个页面的多个框架中使用一个页面对象?
【发布时间】:2023-04-03 12:51:02
【问题描述】:

我想要一个包含 Frames 的 Page 对象,该对象包含另一个 Page。当前结果如下所示:
但我希望页面出现在所有框架中。
我怎样才能做到这一点?
包含 Frames 的页面:

public partial class CombinedPage : Page
{
    public CombinedPage()
    {
        InitializeComponent();
        Frame1.Content = MainWindow.testPage;
        Frame2.Content = MainWindow.testPage;
        Frame3.Content = MainWindow.testPage;
        Frame4.Content = MainWindow.testPage;
    }
}

XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
        
    <GridSplitter Grid.Column="1" Grid.RowSpan="3" Width="5" Background="Black"
                VerticalAlignment="Stretch" HorizontalAlignment="Center" />
    <GridSplitter Grid.Row="1" Grid.ColumnSpan="3" Height="5" Background="Black"
                VerticalAlignment="Center" HorizontalAlignment="Stretch" />

    <Frame NavigationUIVisibility="Hidden" x:Name="Frame1" Grid.Row="0" Grid.Column="0"/>
    <Frame NavigationUIVisibility="Hidden" x:Name="Frame2" Grid.Row="0" Grid.Column="2"/>
    <Frame NavigationUIVisibility="Hidden" x:Name="Frame3" Grid.Row="2" Grid.Column="0"/>
    <Frame NavigationUIVisibility="Hidden" x:Name="Frame4" Grid.Row="2" Grid.Column="2"/>
</Grid>

TestPageXAML:

<Grid>
    <Viewbox Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Label  Name="LiveTimeLabel" Content="%TIME%" HorizontalContentAlignment="Stretch"  HorizontalAlignment="Stretch" Foreground="#cccccc" VerticalAlignment="Stretch" FontWeight="Bold" />
    </Viewbox>
</Grid>

感谢您的建议!

编辑 1: 好的,那么如果我只能使用 Page 对象一次,那么如何将它的位置从一帧更改为另一帧?我试过这个,但它似乎不起作用:

public partial class CombinedPage : Page
{
    public CombinedPage()
    {
        InitializeComponent();
        Frame1.Content = MainWindow.testPage;
        Frame2.Content = MainWindow.testPage;
        Frame3.Content = MainWindow.testPage;
        Frame4.Content = MainWindow.testPage;
    }

    private void butt1_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Frame1.Content = MainWindow.testPage;
    }

    private void butt2_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Frame2.Content = MainWindow.testPage;
    }

    private void butt3_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Frame3.Content = MainWindow.testPage;
    }

    private void butt4_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Frame4.Content = MainWindow.testPage;
    }
}
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
        
    <GridSplitter Grid.Column="1" Grid.RowSpan="3" Width="5" Background="Black"
                VerticalAlignment="Stretch" HorizontalAlignment="Center" />
    <GridSplitter Grid.Row="1" Grid.ColumnSpan="3" Height="5" Background="Black"
                VerticalAlignment="Center" HorizontalAlignment="Stretch" />

    <Frame NavigationUIVisibility="Hidden" x:Name="Frame1" Grid.Row="0" Grid.Column="0"/>
    <Frame NavigationUIVisibility="Hidden" x:Name="Frame2" Grid.Row="0" Grid.Column="2"/>
    <Frame NavigationUIVisibility="Hidden" x:Name="Frame3" Grid.Row="2" Grid.Column="0"/>
    <Frame NavigationUIVisibility="Hidden" x:Name="Frame4" Grid.Row="2" Grid.Column="2"/>
    <StackPanel Grid.Row="3" Grid.Column="0" Orientation="Horizontal">
        <Button x:Name="butt1" Width="50" Click="butt1_Click"/>
        <Button x:Name="butt2" Width="50" Click="butt2_Click"/>
        <Button x:Name="butt3" Width="50" Click="butt3_Click"/>
        <Button x:Name="butt4" Width="50" Click="butt4_Click"/>
    </StackPanel>
</Grid>

【问题讨论】:

    标签: c# wpf frame


    【解决方案1】:

    您应该创建同一 Page 类的单独实例:

    public CombinedPage()
    {
        InitializeComponent();
        Frame1.Content = new TestPage();
        Frame2.Content = new TestPage();
        Frame3.Content = new TestPage();
        Frame4.Content = new TestPage();
    }
    

    控件的单个实例只能在可视化树中出现一次,因此您不能在多个Frame 中显示相同的页面实例。

    【讨论】:

    • 感谢您的回答,但我知道这一点。问题是 - 如何使用 one Page 对象......所以我真的只需要使用一个对象。我想知道有没有办法做到这一点。
    • 我虽然回答了这个问题。答案是不。控件的单个实例只能在可视化树中出现一次。
    • 好的,@mm8,但是如何在帧之间更改对象的位置?我更新了问题
    • 请提出一个新问题,而不是更改原来的问题。
    • 但是您可以尝试将其他帧的Content 设置为null 或其他内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多