我對XAML和WPF很新,我有一個問題。如何從一個Xaml文件導航到另一個Xaml文件?
我有兩個文件。 first.xaml和second.Xaml。在first.xaml中有一個按鈕,點擊它應該導航到second.xaml。我該如何實現這一點。
我對XAML和WPF很新,我有一個問題。如何從一個Xaml文件導航到另一個Xaml文件?
我有兩個文件。 first.xaml和second.Xaml。在first.xaml中有一個按鈕,點擊它應該導航到second.xaml。我該如何實現這一點。
這是一個方式組織代碼:
你second.xaml
應該包含你的窗口definiton如:
<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>
你first.xaml
有按鈕,例如:
<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"/>
然後在後面的代碼:
private void AboutButton_Click(object sender, RoutedEventArgs e)
{
var about = new AboutBox { Owner = this };
about.Initialise();
about.Show();
}
ChrisF的回答是在Windows風格的應用程序中彈出一個新窗口的好方法,除非您不應該以這種方式調用Initialize(它應該從構造函數中調用)。
如果您想改爲使用網頁式導航,則應使用Page類而不是Window以及NavigationService。這也允許您的WPF應用程序在真實的Web瀏覽器中運行。