简体   繁体   中英

How do i navigate from one Xaml file to another?

I am very new to XAML and WPF.I have a problem.

I have two files. first.xaml and second.Xaml. There is a button in first.xaml, on click of which it should navigate to second.xaml.How do i achieve this.

This is one way of organising the code:

Your second.xaml should contain your window definiton eg:

<Window x:Class="MediaCheckerWPF.AboutBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="About Media Checker" Height="300" Width="400" ResizeMode="NoResize"
    Icon="/MediaCheckerWPF;component/Resources/checker.ico"
    ShowInTaskbar="False">
    <Grid>
        ...
    </Grid>
</Window>

Your first.xaml has the button eg:

<Button Height="23" HorizontalAlignment="Right" Name="aboutButton"
 VerticalAlignment="Top" Width="23" Click="AboutButton_Click"
 Content="{DynamicResource TInformationButton}"
 ToolTip="{DynamicResource TInformationButtonTooltip}" Margin="0,0,8,0"/>

Then in the code behind:

private void AboutButton_Click(object sender, RoutedEventArgs e)
{
    var about = new AboutBox { Owner = this };
    about.Initialise();
    about.Show();
}

ChrisF's answer is a good way of popping up a new window in a Windows-style application, except you should not call Initialize that way (it should be called from the constructor).

If you want web-style navigation instead, you should use the Page class instead of Window, along with NavigationService . This also allows your WPF application to run inside an actual web browser.

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