繁体   English   中英

将具有非默认构造函数的页面加载到Frame中

[英]Loading a page with non-default constructor into Frame

我正在尝试使用frame来托管不同类型的xaml页面。 用户将选择要加载的页面。
我想将一个带有非默认构造函数的页面加载到Frame中。 我在MSDN上找到以下代码:

void hyperlink_Click(object sender, RoutedEventArgs e)
{
    // Instantiate the page to navigate to
    PageWithNonDefaultConstructor page = new PageWithNonDefaultConstructor("Hello!");

    // Navigate to the page, using the NavigationService
    this.NavigationService.Navigate(page);
}

上面的代码替换了当前显示的页面,但是我希望它加载到当前页面上的特定框架元素中。

<Frame Source="Default.xaml" />

如何才能做到这一点?

您可以使用Frame of Frame而不是Source来解决问题。

//xaml
<Page x:Class="WpfApplication1.Something"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
    <StackPanel>
        <Button Click="Button_Click" Content="click"/>
        <Frame Content="{Binding}"/>
    </StackPanel>
</Page>

//codebehinde
private void Button_Click(object sender, RoutedEventArgs e)
{
    // Instantiate the page to navigate to
    Page1 page = new Page1("Hello!");
    this.DataContext = page;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM