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.
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;
}
精彩评论