The following code creates a Frame within a Window and adds
a button control to the window. On button click event handler we are going to
navigate to a URI using a Frame.
<Window x:Class="FrameSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBlock>Outside area of frame</TextBlock>
<Frame Name="FrameWithinGrid"
>
</Frame>
<Button Height="23"
Margin="114,12,25,0"
Name="button1" VerticalAlignment="Top" Click="button1_Click">Navigate to C# Corner
</Button>
</Grid>
</Window>
The Navigate method of Frame is used to navigate to a URI.
The following code navigates to Page1.xaml.
private void button1_Click(object
sender, RoutedEventArgs e)
{
FrameWithinGrid.Navigate(new System.Uri("Page1.xaml",
UriKind.RelativeOrAbsolute));
}
The following code navigates to an external website URL and
opens the ASPX page within a Frame.
FrameWithinGrid.Source = new Uri("http://www.c-sharpcorner.com/Default.aspx",
UriKind.Absolute);
If you need to open a URI in a new browser window, the
following code will do that. This code first creates a NavigationWindow and
then sets its Source to a URI.
NavigationWindow window = new NavigationWindow();
Uri source = new Uri("http://www.c-sharpcorner.com/Default.aspx",
UriKind.Absolute);
window.Source = source; window.Show();