[英]NavigationPage rendere for android. Xamarin Forms
我正在尝试从自定义渲染器访问操作栏(我知道我可以使用toolbar.xml,但它不适合我的情况)。 这是我的代码
[assembly: ExportRenderer(typeof(NavigationPage), typeof(NoviNavigationPageRenderer))]
namespace NoviMobileTest.Droid.Renderers
{
public class NoviNavigationPageRenderer : PageRenderer
{
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
var actionBar = ((Activity)Context).ActionBar;
actionBar.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.YourImageInDrawable));
}
}
}
变量actionbar
始终为null。 我也尝试扩展NavigationRenderer,但出现类强制转换异常。 我见过很多人都遇到同样的问题,但是令人惊讶的是,没有可看到的例子。 我应该如何获取工具栏的实例?
注意:我的MainActivity扩展了FormsAppCompatActivity
提前致谢
尝试在您的Android项目中安装CurrentActivity插件 ,然后像这样:
using Plugin.CurrentActivity;
...
private static Android.Support.V7.Widget.Toolbar GetToolbar() => (CrossCurrentActivity.Current?.Activity as MainActivity)?.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
最后:
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
var actionBar = GetToolbar();
actionBar.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.YourImageInDrawable));
}
有关此的更多信息,请参见: IconNavigationPageRenderer.cs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.