开发者

Loading a page with non-default constructor into Frame

I am trying to use a frame to host different type of xaml pages. The user will select the page to be loaded.

I want to load a page with a non-default constructor into the Frame. I found the following code on MSDN:

void hyperlink_Click(object sender, RoutedEventArgs e)
{
    // Instantiate the page to navigate to
    PageWithNonDefaultConstructor pa开发者_如何学JAVAge = new PageWithNonDefaultConstructor("Hello!");

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

The above code replaces the currently displayed page, but I want it to load into a specific frame element on the current page.

<Frame Source="Default.xaml" />

How can this be done?


You can solve the problem using the Content of Frame instead of 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜