[英]UWP Frame PageNavigation
我在UWP中的导航系统有问题。
我有3个不同的页面,其中包含BackRequested事件。 问题是相框无法返回到第二页。
例如:
Page 1 -> Page 2 -> Page 3 -> BACKBUTTON -> Page 1
我想访问第2页而不是第一页。
当我想拥有一个导航栏时,我决定创建一个母版页,其中包含一个框架控件。 例如,此控件称为“图层”。
我可以使用此代码导航到第二页
Layer.Navigate(typeof(BlankPage2), this);
“ this”包含整个FirstPage,我需要从第二页访问该图层。
当我想调用第三页时,代码将如下所示
_FirstPage.Layer.Navigate(BlankPage2),this);
我可以通过使用获取FirstPage实例
protected async override void OnNavigatedTo(NavigationEventArgs e)
{FirstPage _FirstPage = e.Parameter as FirstPage;}
我认为问题是我的FirstPage实例不同于第二页中的给定参数。
我可以尝试调用FirstPage(例如
WinForm -> (Form1) master = (Form1)Application.OpenForms["Form1"];)
但是我不知道如何打开页面
这是什么标准?
如何在一个页面中有一个包含不同页面的导航栏?
语法我很抱歉。 由于Windows 10支持键盘输入,因此它会尝试更正enter code here
从英文到德文enter code here
任何enter code here
。
*编辑
我尝试创建一个测试项目,以查看没有UIElement(框架)如何工作,我能够获得正确的BackStack。 但是不幸的是,问题没有解决!
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
try
{
int count = 0;
StringBuilder sb = new StringBuilder();
foreach (var item in this.Frame.BackStack)
{
count++;
sb.AppendLine(item.SourcePageType.FullName + " " + count.ToString());
}
MessageDialog md = new MessageDialog(sb.ToString());
await md.ShowAsync();
}
catch
{
}
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
currentView.BackRequested += CurrentView_BackRequested;
}
private void CurrentView_BackRequested(object sender, BackRequestedEventArgs e)
{
if (this.Frame.CanGoBack == true) { this.Frame.GoBack();}
}
public BlankPage3()
{
this.InitializeComponent();
}
这是我的主要密码
Sites.ContentPage cp = new Sites.ContentPage();
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
Classes.Transmission.Passwort.CreateTransmissionPasswort ctp = e.Parameter as Classes.Transmission.Passwort.CreateTransmissionPasswort;
this.PW = ctp.PW;
this.cp = ctp.CP;
try
{
int count = 0;
StringBuilder sb = new StringBuilder();
foreach (var item in cp.Layer.BackStack)
{
count++;
sb.AppendLine(item.SourcePageType.FullName + " " + count.ToString());
}
MessageDialog md = new MessageDialog(sb.ToString());
await md.ShowAsync();
}
catch
{
}
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
currentView.BackRequested += CurrentView_BackRequested1;
if(Classes.CheckScreenState.GetFactorType() == Classes.CheckScreenState.DeviceForFactorType.Phone)
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
if (Classes.CheckScreenState.GetFactorType() != Classes.CheckScreenState.DeviceForFactorType.Phone)
{
Window.Current.CoreWindow.PointerPressed += CoreWindow_PointerPressed;
}
}
private void CoreWindow_PointerPressed(CoreWindow sender, PointerEventArgs args)
{
if (args.CurrentPoint.Properties.IsXButton1Pressed)
{
if (cp.Layer.CanGoBack == true) { cp.Layer.GoBack(); }
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
}
if (args.CurrentPoint.Properties.IsXButton2Pressed)
{
if (cp.Layer.CanGoForward == true) { cp.Layer.GoForward(); }
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
}
}
private void CurrentView_BackRequested1(object sender, BackRequestedEventArgs e)
{
if (cp.Layer.CanGoBack == true) {cp.Layer.GoBack(); e.Handled = true; }
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
}
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
if (cp.Layer.CanGoBack == true) {cp.Layer.GoBack(); e.Handled = true; }
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
}
CP = ContentPage(主页)
我做了一个简单的应用程序,向您显示导航。
应用程序的MainPage是(称为此根页面):FramePage.xaml还有其他3个xaml页面(称为子页面):Frame1.xaml,Frame2.xaml,Frame3.xaml
因此,我在根目录中有一个框架,在底部有一个命令栏,其中有两个按钮“后退”和“下一个”来浏览框架。
因此,当我们单击“下一步”按钮时,它将像这样
框架1->框架2->框架3
当您单击“返回”按钮时,它将检查Frame是否可以返回。 如果可以,它将返回上一帧。
为了跟踪框架,我在每个框架中都保留了一个TextBlock来显示框架名称。
这是代码。
对于FramePage.xaml
XAML
<Page.BottomAppBar>
<CommandBar>
<AppBarButton x:Name="back" HorizontalAlignment="Left" Icon="Back" Label="back" VerticalAlignment="Top" Click="back_Click"/>
<AppBarButton x:Name="next" HorizontalAlignment="Left" Icon="Forward" Label="forward" VerticalAlignment="Top" Click="next_Click"/>
</CommandBar>
</Page.BottomAppBar>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Frame x:Name="mainFrame">
</Frame>
</Grid>
XAML文件
public FramePage()
{
this.InitializeComponent();
mainFrame.Navigate(typeof(Frame1));
}
private void back_Click(object sender, RoutedEventArgs e)
{
if (mainFrame.CanGoBack)
mainFrame.GoBack();
}
private void next_Click(object sender, RoutedEventArgs e)
{
Type current = mainFrame.SourcePageType;
if (current.Name == "Frame1")
mainFrame.Navigate(typeof(Frame2));
if (current.Name == "Frame2")
mainFrame.Navigate(typeof(Frame3));
}
框架1.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="Frame 1" FontSize="36"/>
</Grid>
框架2.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="Frame 2" FontSize="36"/>
</Grid>
框架3.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="Frame 3" FontSize="36"/>
</Grid>
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.