ContentPage中的视图组-导出渲染器问题Xamarin
我想在ContentPage中添加一个Android.Widget.RelativeLayout。为此,我遵循了多个在线示例: 等等 但是我不能让它工作 在我的共享项目中,我有:ContentPage中的视图组-导出渲染器问题Xamarin,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我想在ContentPage中添加一个Android.Widget.RelativeLayout。为此,我遵循了多个在线示例: 等等 但是我不能让它工作 在我的共享项目中,我有: namespace SharedProject { public partial class App : Application { public App () { InitializeComponent(); Main
namespace SharedProject
{
public partial class App : Application
{
public App ()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
....
}
空间
以及正在呈现的MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:SharedProject="clr-namespace:SharedProject;assembly=SharedProject"
x:Class="SharedProject.MainPage">
<ContentPage.Content>
<StackLayout>
<SharedProject:MyCustomViewControl
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
我希望在呈现主页时,还会根据MainPage.xaml和OnElementChanged将rlMainContainer添加到主页中,但不会请求MyCustomViewRenderer中的任何内容(没有断点命中)
我做错了什么?为什么要尝试呈现原生Android RelativeLayout?Xamarin.Forms中有内置选项。看看这个我刚刚将您的代码插入到一个新项目中,只要我明确说明我想使用哪个RelativeLayout,它就可以正常工作。有两种可能,Xamarin.Forms.RelativeLayout和Android.Widget.Relative布局。我假设您想要后者,但我不认为它是完全限定的,而且因为我假设您对Xamarin.Forms(ExportRenderer属性所需)和Android.Widget(RelativeLayout所需)都有使用语句,所以不清楚您是否在所有地方都使用了正确的RelativeLayout 还有一个问题是,您使用的是
Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer
还是Xamarin.Forms.Platform.Android.ViewRenderer
。大多数使用AppCompat作为默认Xam。表单模板使用FormsAppCompatActivity
。因此,假设您使用的是AppCompat,以下自定义渲染器的完整源代码(根据需要提供完整的类型限定)正在我的终端上运行:
using System;
using Android.Content;
using TestRelLayout;
using TestRelLayout.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(MyCustomViewControl), typeof(MyCustomViewRenderer))]
namespace TestRelLayout.Droid
{
public class MyCustomViewRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<MyCustomViewControl, Android.Widget.RelativeLayout>
{
public MyCustomViewRenderer(Context context) : base(context)
{
}
private Android.Widget.RelativeLayout rlMainContainer;
protected override void OnElementChanged(ElementChangedEventArgs<MyCustomViewControl> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
rlMainContainer = new Android.Widget.RelativeLayout(Context);
rlMainContainer.SetMinimumHeight(50);
rlMainContainer.SetMinimumWidth(100);
rlMainContainer.LayoutParameters = new Android.Widget.RelativeLayout.LayoutParams(Android.Widget.RelativeLayout.LayoutParams.MatchParent, Android.Widget.RelativeLayout.LayoutParams.MatchParent);
// set the background color to make it easy to see if renderer is working
rlMainContainer.SetBackgroundColor(
Android.Graphics.Color.Aquamarine);
if (Control == null)
SetNativeControl(rlMainContainer);
// did not bother to set this up in my MainActivity
//MainActivity.instance.setActiveReader(Control);
}
}
}
使用系统;
使用Android.Content;
使用TestRelLayout;
使用TestRelLayout.Droid;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
[程序集:ExportRenderer(typeof(MyCustomViewControl)、typeof(MyCustomViewRenderer))]
命名空间TestRelLayout.Droid
{
公共类MyCustomViewRenderer:Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer
{
公共MyCustomViewRenderer(上下文):基本(上下文)
{
}
私有Android.Widget.RelativeLayout rlmain容器;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | Element==null)
{
回来
}
rlMainContainer=newandroid.Widget.RelativeLayout(上下文);
RLmain容器。设置最小高度(50);
RLmain容器。设置最小宽度(100);
rlMainContainer.LayoutParameters=新的Android.Widget.RelativeLayout.LayoutParams(Android.Widget.RelativeLayout.LayoutParams.MatchParent,Android.Widget.RelativeLayout.LayoutParams.MatchParent);
//设置背景色以便于查看渲染器是否工作
rlMainContainer.SetBackgroundColor(
Android.Graphics.Color.Aquamarine);
if(Control==null)
SetNativeControl(RLmain容器);
//没有费心在我的主要活动中设置这个
//MainActivity.instance.setActiveReader(控件);
}
}
}
经过大量文件比较(我的项目和Microsoft示例项目),我发现问题与
MainPage=newNavigationPage(newmainpage())代码>
为了解决这个问题,我刚刚删除了NavigationPage
wrapper
MainPage=newmainpage()代码>
我还不知道为什么会发生这种情况,但如果有人知道,请编辑我的答案并解释
编辑:渲染器也可以使用NavigationPage
,但只有在OnCreate方法完成执行后才能使用 这不是一个答案,最好加上它作为评论。实际上,我需要一个ViewGroup元素(在我的例子中是RelativeLayout),因为我想在另一个函数中将它用作参数。好的,如果您在表单中工作,为什么希望RelativeLayout作为函数中的参数?因为我使用的是一个需要它的外部库。感谢您提供这样一个全面的答案。关于RelativeLayout
部分,我有一个using语句,比如using RelativeLayout=Android.Widget.RelativeLayout
应该可以,但我使用的是Xamarin.Forms.Platform.Android.ViewRenderer
。我会试试另一个,但是他们在他们的网站或其他答案上没有提到AppCombat。从现在起,我还将在我的问题中发布using语句。奇怪的是,我在测试中使用了导航页面,这很有效。一定是您发布的代码中未显示的其他内容。@jgoldberger是的,我刚刚使用了Microsoft的CustomRenderer,如果您使用NavigationPage
包装器,则渲染器将正常工作,但只有在onCreate方法完成执行之后。
[assembly: ExportRenderer(typeof(MyCustomViewControl), typeof(MyCustomViewRenderer))]
namespace SharedProject.Mobile.Droid
{
public class MyCustomViewRenderer : ViewRenderer<MyCustomViewControl, RelativeLayout>
{
public MyCustomViewRenderer(Context context) : base(context)
{
}
private Android.Widget.RelativeLayout rlMainContainer;
protected override void OnElementChanged(ElementChangedEventArgs<MyCustomViewControl> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
rlMainContainer = new RelativeLayout(Context);
rlMainContainer.SetMinimumHeight(50);
rlMainContainer.SetMinimumWidth(100);
rlMainContainer.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
if (Control == null)
SetNativeControl(rlMainContainer);
MainActivity.instance.setActiveReader(Control);
}
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
instance = this;
LoadApplication(new App());
using System;
using Android.Content;
using TestRelLayout;
using TestRelLayout.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(MyCustomViewControl), typeof(MyCustomViewRenderer))]
namespace TestRelLayout.Droid
{
public class MyCustomViewRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<MyCustomViewControl, Android.Widget.RelativeLayout>
{
public MyCustomViewRenderer(Context context) : base(context)
{
}
private Android.Widget.RelativeLayout rlMainContainer;
protected override void OnElementChanged(ElementChangedEventArgs<MyCustomViewControl> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
rlMainContainer = new Android.Widget.RelativeLayout(Context);
rlMainContainer.SetMinimumHeight(50);
rlMainContainer.SetMinimumWidth(100);
rlMainContainer.LayoutParameters = new Android.Widget.RelativeLayout.LayoutParams(Android.Widget.RelativeLayout.LayoutParams.MatchParent, Android.Widget.RelativeLayout.LayoutParams.MatchParent);
// set the background color to make it easy to see if renderer is working
rlMainContainer.SetBackgroundColor(
Android.Graphics.Color.Aquamarine);
if (Control == null)
SetNativeControl(rlMainContainer);
// did not bother to set this up in my MainActivity
//MainActivity.instance.setActiveReader(Control);
}
}
}