因此,我试图在用户单击每个选项卡时更新我的页面,我尝试在OnAppearing方法中添加代码,但似乎无法正常工作。因此,我尝试了针对iOS的自定义渲染器:
using UIKit;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(MainTabbedPage), typeof(MainPageRenderer))]
namespace YourNameSpace
{
public class MainPageRenderer : TabbedRenderer
{
private MainTabbedPage _page;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
_page = (MainTabbedPage)e.NewElement;
}
else
{
_page = (MainTabbedPage)e.OldElement;
}
try
{
var tabbarController = (UITabBarController)this.ViewController;
if (null != tabbarController)
{
tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private async void OnTabbarControllerItemSelected(object sender, UITabBarSelectionEventArgs eventArgs)
{
if (_page?.CurrentPage?.Navigation != null && _page.CurrentPage.Navigation.NavigationStack.Count > 0)
{
await _page.CurrentPage.Navigation.PopToRootAsync();
}
}
}
}
这似乎仅对我的选项卡中的一个导航页面有效,但对其他两个正常的选项卡页面却不会刷新。我在这里想念什么吗?
任何帮助都将不胜感激。