任何想法为什么"GoToAsync"在AppShell.cs中不起作用?



注册详细页面路由

AppShell.cs

public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell(string message)
{
InitializeComponent();
Routing.RegisterRoute(nameof(ItemDetailPage), typeof(ItemDetailPage));
Routing.RegisterRoute(nameof(NewItemPage), typeof(NewItemPage));
Routing.RegisterRoute("LoginPage", typeof(LoginPage));
if (message != "main") {


int itemid = Int32.Parse(message);
var item = App.Database.GetItemAsyncId(itemid).Result;
var itemValue = new Models.Item
{
Id = item.Id,
Description = item.Description,
Text = item.Text
};
OnItemSelected(itemValue);
}
}
public async void OnItemSelected(Item item)
{  
await Shell.Current.GoToAsync("LoginPage");
//Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?{nameof(ItemDetailViewModel.ItemId)}={item.Id}");

}}

当我在模拟器上启动应用程序时,应用程序应该启动以下页面:

  1. "AboutPage"——开始;
  2. "LoginPage"-它不启动。"LoginPage"应该使用Shell.Current.GoToAsync("LoginPage");

编辑器不显示错误。

是navigationStack

<FlyoutItem x:Name="ItemsPage" Icon="icon_feed.png">
<ShellContent Route="ItemsPage" ContentTemplate="{DataTemplate local:ItemsPage}" />
</FlyoutItem>
<FlyoutItem x:Name="About" Icon="icon_about.png">
<ShellContent Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
</FlyoutItem>

<!-- When the Flyout is visible this will be a menu item you can tie a click behavior to  -->
<MenuItem Text="Logout" StyleClass="MenuItemLayoutStyle" Clicked="OnMenuItemClicked">
</MenuItem>
<!--
TabBar lets you define content that won't show up in a flyout menu. When this content is active
the flyout menu won't be available. This is useful for creating areas of the application where 
you don't want users to be able to navigate away from. If you would like to navigate to this 
content you can do so by calling 
await Shell.Current.GoToAsync("//LoginPage");
-->
<TabBar>
<ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
</TabBar>

你的方法被调用了吗?消息变量的值是多少?有时检查if (message != "main")是否为true它只会发生在构造函数上。

[编辑]

要在构造函数中导航Shell页面,您必须将"Shell.Current.GoToAsync('LoginPage')"替换为"this.GoToAsync('LoginPage')"

在实例的那一刻,创建的Shell不是"当前"应用程序的Shell

相关内容

最新更新