我正在主页中创建一个WPF应用程序框架和一些按钮。 如果我按下Button1,我的框架将导航到Page1,
Page1 vPage1 = new Page1();
Frame.Content = vPage1;
如果我按下Button2,如果Page1在Frame中打开,它应该只导航到Page2。 如何检查Page1是否已打开?
THX
答案 0 :(得分:0)
这里有一个样本。
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));
}