gpt4 book ai didi

c# - Xamarin Forms - 从不从 ContentPage 继承的类中查找 App 的当前页面

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:53 29 4
gpt4 key购买 nike

我的问题并不像标题所暗示的那么简单。我知道我可以使用导航堆栈找出最顶层的页面并调用该页面上的导航事件。

这就是我打算做的..

        Page currentPage;
int index = Application.Current.MainPage.Navigation.NavigationStack.Count - 1;
currentPage = Application.Current.MainPage.Navigation.NavigationStack[index];

//after getting the correct page..
//await currentPage.Navigation.PushAsync(new SomePage());

我的问题是索引返回为 -1。我相信由于我的页面层次结构/结构而增加了一些复杂性。

我的应用程序主页是一个 LoginPage - 一旦用户成功登录,他们就会被推送到模态页面,然后从那里进行其余的导航。

await Navigation.PushModalAsync(new NavigationPage(new MainMenu()));



当 NavigationStack 包含模态页面时,如何从未继承自 ContentPage 的类中找到当前事件页面(用户正在查看的页面)?

我需要这样做,以便我的静态“帮助程序”类(平台特定的 iOS/Android 代码访问)可以传递一个字符串作为页面名称,并且这个帮助程序类可以解析该名称,并导航到页面(如果存在)。在电话上发生事件(点击推送通知)后,通过特定于平台的实现访问帮助程序类

最佳答案

(新)解决方案 #2

另一种方法是继承 NavigationPage、TabbedPage 类,然后使用 Xamarin 为我们提供的导航事件来跟踪当前页面。这些事件是:ModalPushed、ModalPopped、Pushed、Popped、CurrentPageChanged 等。

它的代码有点多,但它在所有平台上的行为都更可预测,如果应用程序被置于后台,您不需要保存任何额外的状态。

1) App.xaml.cs

public App()
{
...
this.ModalPushed += OnModalPushed;
this.ModalPopped += OnModalPopped;
}

//keep track of any modals presented
private readonly Stack<Page> ModalPages = new Stack<Page>();

void OnModalPushed(object sender, ModalPushedEventArgs e)
{
ModalPages.Push(e.Modal);
PageService.CurrentPage = FindCurrentPage();
}

void OnModalPopped(object sender, ModalPoppedEventArgs e)
{
ModalPages.Pop();
PageService.CurrentPage = FindCurrentPage();
}

public Page FindCurrentPage()
{
//If there is a Modal present, start there, or else start in the MainPage
Page root = ModalPages.Count > 0 ? ModalPages.Peek() : this.MainPage;

var tabbedPage = root as TabbedPage;
if (tabbedPage != null)
{
var currentTab = tabbedPage.CurrentPage;
var navPage = currentTab as NavigationPage;
if (navPage != null)
{
return navPage.CurrentPage;
}
else
{
return currentTab;
}
}
else
{
var navPage = root as NavigationPage;
if (navPage != null)
{
return navPage.CurrentPage;
}
return root;
}
}

2) CustomNavigationPage.cs

//All NavigationPage in your app should use this class!
public class CustomNavigationPage : NavigationPage
{
public BaseNavigationPage(Page page) : base(page)
{
this.Pushed += OnPushed;
this.Popped += OnPopped;
}

void OnPushed(object sender, NavigationEventArgs e)
{
PageService.CurrentPage = e.Page;
}

void OnPopped(object sender, NavigationEventArgs e)
{
PageService.CurrentPage = ((App)App.Current).FindCurrentPage();
}
}

3) CustomTabbedPage.cs --- 仅当您的应用使用标签时

public class CustomTabbedPage : TabbedPage
{
public CustomTabbedPage()
{
this.CurrentPageChanged += OnTabbedPageTabChanged;
}

void OnTabbedPageTabChanged(object sender, EventArgs e)
{
PageService.CurrentPage = ((App)App.Current).FindCurrentPage();
}
}

4) PageService.cs

public static class PageService
{
public Page CurrentPage
{
get;
set;
}
}

(原始)解决方案 #1

对我有用的是使用静态类来跟踪用户当前所在的页面。我的所有 ContentPages 都继承了一个 BasePage,它在 OnAppearing() 中设置当前页面。在 Android 中,您还必须处理应用程序暂停/恢复的特殊情况,因为 Xamarin 将在应用程序的根页面(不一定是 View 中的页面)上调用 OnAppearing。

此方法允许我从我的 View 模型层中的任何位置执行推送/弹出页面等行为,而无需将 ViewModel 直接耦合到 View 。

页面服务.cs:

public static class PageService
{
public Page CurrentPage
{
get;
set;
}

public Page SavedStatePage
{
get;
set;
}
}

BasePage.cs:

//Have all of your pages inherit this page
public abstract class BasePage : ContentPage
{
public BasePage () : base()
{
}

public override void OnAppearing()
{
protected override void OnAppearing()
{
base.OnAppearing();

//Mainly for Android, restore the current page to the last saved page when the app paused
if( PageService.SavedStatePage != null )
{
PageService.CurrentPage = PageService.SavedStatePage;
PageService.SavedStatePage = null;
}
else
{
//default behavior. Set the current page to the one currently appearing.
PageService.CurrentPage = this;
}
}
}
}

MainActivity.cs(在原生 Droid 项目中):

protected override void OnPause()
{
base.OnPause();

//save the current page before app pauses, because Xamarin doesn't always call OnAppearing on the correct page when resume happens
PageService.SavedStatePage = PageService.CurrentPage;
}

关于c# - Xamarin Forms - 从不从 ContentPage 继承的类中查找 App 的当前页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45851420/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com