我命令为不同的页面应用一些navigationBar属性(比如背景图像),我认为在我的自定义NavigationRenderer上有一个条件。
我的想法是有一些条件,比如(在我的工作代码中)
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类,但我认为是不可能的。
你知道怎么做吗?
发布于 2018-11-24 16:57:32
如果您查看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;
}
}
}2)基于上述代码,需要添加两个接口。它们应该位于页面所在的同一个项目/ dll中(所有Xamarin.Forms UI):
public interface IMyPageType1
{
}
public interface IMyPageType2
{
}3)现在剩下的一切都是在需要的页面上实现接口。例如:
public partial class MyPage1 : ContentPage, IMyPageType1
{
//...
}从这里开始,可能性是无限的!例如,您可以向IMyPageType1添加一个返回颜色的方法,然后在呈现器中,一旦您知道被推送的页面正在实现IMyPageType1,就可以调用该方法并获取要使用的颜色。
https://stackoverflow.com/questions/53442795
复制相似问题