【发布时间】:2020-03-25 19:14:15
【问题描述】:
我试图弄清楚如何通过单个页面上的按钮单击事件来浏览我的 shell 页面。我希望按钮单击与打开菜单并选择下一页一样。例如,我将有一系列页面,我从用户那里收集信息以构建报告。我希望用户在第一页输入一些数据,然后单击下一步进入第二页以输入更多信息,依此类推。我想要保留 shell 页面菜单的原因是,如果用户决定他们需要返回页面来修改以前输入的某些信息,他们可以打开 shell 菜单并选择页面,而不是单击返回按钮 n次返回上一页。
我尝试使用路由和 Shell.Current.GoToAsync() 来实现这一点,但得到一个空引用
在 UIApplication.Main(args, null, "AppDelegate"); 行的 Main.cs 中,我避免使用 Navigation.PushModalAsync(new Page2());,因为以这种方式导航会将汉堡菜单删除到我的 shell 页面。我还尝试在 ShellPage.xaml.cs 中创建一个名为 Change_Page() 的函数,我通过单击第一页的按钮调用该函数。在这个函数中,我尝试了相同的Shell.Current.GoToAsync("myPage2"),这再次导致了空引用错误,并尝试了Shell.Current.CurrentItem = myPage2;,这导致了同一行的中断,但是我得到了一些 UIKit.UIApllication 错误t 似乎显示错误,但确实在此处停止了调试器。
ShellPage.xaml
...
<FlyoutItem Title="Page1">
<ShellContent>
<views:Page1View Title="Page1" x:Name="myPage1" Route="myPage1"/>
</ShellContent>
</FlyoutItem>
<FlyoutItem Title="Page2">
<ShellContent>
<views:Page2View Title="Page2" x:Name="myPage2" Route="myPage2"/>
</ShellContent>
</FlyoutItem>
<FlyoutItem Title="Page3">
<ShellContent>
<views:Page1View Title="Page1" x:Name="myPage3" Route="myPage3"/>
</ShellContent>
</FlyoutItem>
...
myShellPage.xaml.cs
[DesignTimeVisible(false)]
public partial class MyShellPage : Shell
{
public MyShellPage()
{
InitializeComponent();
Routing.RegisterRoute("myPage1", typeof(CallInfoPage));
Routing.RegisterRoute("myPage2", typeof(SpillInfoPage));
Routing.RegisterRoute("myPage3", typeof(ItemsPage));
Device.StartTimer(TimeSpan.FromSeconds(3), () =>
{
// Do something
Current.CurrentItem = myPage3; // throws error 'myPage3' does not exist in current context
return false; // True = Repeat again, False = Stop the timer
});
}
}
Page1View.xaml
...
<Button Text="Next" Clicked="Next_Clicked" />
...
Page1View.xaml.cs
...
async void Next_Clicked(System.Object sender, System.EventArgs e)
{
await Shell.Current.GoToAsync("myPage2");
}
...
【问题讨论】:
标签: c# xaml xamarin.forms xamarin.ios navigation