首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin:检测NavigationRenderer上的页面

Xamarin:检测NavigationRenderer上的页面
EN

Stack Overflow用户
提问于 2018-11-23 08:09:16
回答 1查看 234关注 0票数 0

我命令为不同的页面应用一些navigationBar属性(比如背景图像),我认为在我的自定义NavigationRenderer上有一个条件。

我的想法是有一些条件,比如(在我的工作代码中)

代码语言:javascript
复制
   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类,但我认为是不可能的。

你知道怎么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-24 16:57:32

如果您查看NavigationRenderer 这里的源代码,您会发现有很多方法和回调可以利用。

我建议你可以这样做:

1)定制NavigationRenderer的代码(iOS项目,您必须在Android上执行类似的操作):

代码语言:javascript
复制
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;
        }
    }
}

2)基于上述代码,需要添加两个接口。它们应该位于页面所在的同一个项目/ dll中(所有Xamarin.Forms UI):

代码语言:javascript
复制
    public interface IMyPageType1
    {
    }

    public interface IMyPageType2
    {
    }

3)现在剩下的一切都是在需要的页面上实现接口。例如:

代码语言:javascript
复制
    public partial class MyPage1 : ContentPage, IMyPageType1
    {
        //...
    }

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53442795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档