简体   繁体   中英

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 page = 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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