我正在制作一个 WPF 应用程序,在主页中有一个 Frame 和一些按钮。如果我按下 Button1,我的框架会导航到 Page1,
第 1 页 vPage1 = 新第 1 页();
Frame.Content = vPage1;
如果我按下 Button2,它应该只在 Page1 在框架中打开时导航到 Page2。如何检查 Page1 是否打开?
谢谢
这是给你的样本。
XAML
</Grid.RowDefinitions>
<Frame x:Name="fram" NavigationUIVisibility="Hidden"
Background="Silver"
Navigated="Frame_Navigated"
Source="Pages/Page1.xaml" />
<StackPanel Grid.Row="1"
HorizontalAlignment="Right"
Orientation="Horizontal">
<Button x:Name="btnPrev"
Click="btnPrev_Click"
Content="Prev" />
<Button x:Name="btnNext"
Click="btnNext_Click"
Content="Next" />
</StackPanel>
</Grid>
代码。
int pageindex;
private void btnPrev_Click(object sender, RoutedEventArgs e)
{
if (pageindex <= 1)
{
fram.Navigate(new Uri("Pages/Page" + (pageindex + 2) + ".xaml",UriKind.Relative));
return;
}
fram.Navigate(new Uri("Pages/Page" + (pageindex - 1) + ".xaml", UriKind.Relative));
}
private void btnNext_Click(object sender, RoutedEventArgs e)
{
if (pageindex >= 3)
{
fram.Navigate(new Uri("Pages/Page" + (pageindex - 2) + ".xaml", UriKind.Relative));
return;
}
fram.Navigate(new Uri("Pages/Page" + (pageindex + 1) + ".xaml", UriKind.Relative));
}
private void Frame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
pageindex = Convert.ToInt32(e.Content.GetType().Name.Substring(4));
}