Xamarin:检测NavigationRenderer上推送的页面

Xamarin:检测NavigationRenderer上推送的页面,xamarin,navigation,custom-controls,Xamarin,Navigation,Custom Controls,我为了应用一些导航栏属性,比如作为不同页面的背景图像,我认为对我的自定义导航渲染器有一个条件 我的想法是在我的工作代码中有一些类似的条件 public class CustomNavigationRenderer : NavigationRenderer { public override void ViewDidLoad() { base.ViewDidLoad(); if (pagePushed is 1) {

我为了应用一些导航栏属性,比如作为不同页面的背景图像,我认为对我的自定义导航渲染器有一个条件

我的想法是在我的工作代码中有一些类似的条件

   public class CustomNavigationRenderer : NavigationRenderer
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        if (pagePushed is 1)
        {
            NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
            NavigationBar.ShadowImage = new UIImage();
        }

        else (ahother page){
            var img = UIImage.FromBundle("MyImage");
            NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
        }
    }
}
这允许我至少有一个条件来应用不同的导航属性。另一种方法是使用2个Navigationrenderer类,但我认为这是不可能的


你知道怎么做吗?

如果你看一下NavigationRenderer的源代码,你会发现有很多方法和回调可以利用

我建议您可以这样做:

1个自定义NavigationRenderer iOS项目的代码,您必须在Android上执行类似操作:

using System.Threading.Tasks;
using MyProject.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(NavigationPage), typeof(NavRenderer))]
namespace MyProject.iOS
{
    public class NavRenderer : NavigationRenderer
    {
        protected override async Task<bool> OnPushAsync(Page page, bool animated)
        {
            var result = await base.OnPushAsync(page, animated);

            if(result)
            {
                if (page is IMyPageType1)
                {
                    NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
                    NavigationBar.ShadowImage = new UIImage();
                }

                else if(page is IMyPageType2)
                {
                    var img = UIImage.FromBundle("MyImage");
                    NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
                }
            }

            return result;
        }
    }
}
3现在剩下的就是在需要的页面上实现接口。例如:

    public partial class MyPage1 : ContentPage, IMyPageType1
    {
        //...
    }

从这里开始,可能性是无限的!例如,您可以向IMyPageType1添加一个返回颜色的方法,然后在渲染器中,一旦您知道正在推送的页面正在实现IMyPageType1,就可以调用该方法并获取要使用的颜色。

Nice。德克萨斯州!你知道我可以重写OnPopViewAsync吗?我尝试在基本POP之后调用它,但不确定需求是什么,但我将首先重写该方法并将对base的调用放在它的末尾
    public partial class MyPage1 : ContentPage, IMyPageType1
    {
        //...
    }