我在我的Xamarin.Forms应用程序中创建了一个自定义选择器渲染器,在IOS上运行正常,但是当我运行Android时崩溃了。
我已经更新了Visual Studio 2017,而且我正在运行Xamarin.Forms 2.3.4.247。
目标Android版本设置为7.1(6.0上的相同错误) 最低Android版本设置为4.3
自定义选择器:
using System; using Xamarin.Forms; namespace Attest.Combustivel.Infra.Application.Controls { public class CustomPicker : Picker { public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(p => p.FontSize, 22); public Double FontSize { get { return (double)GetValue(FontSizeProperty); } set { if (this.FontSize != value) { SetValue(FontSizeProperty, value); } } } } }
自定义Droid Picker渲染器 - 出错:
using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(Attest.Combustivel.Infra.Application.Controls.CustomPicker), typeof(Attest.Combustivel.Target.Droid.Renderers.DroidCustomPickerRenderer))] namespace Attest.Combustivel.Target.Droid.Renderers { public class DroidCustomPickerRenderer : PickerRenderer { protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); //if (Control == null) //{ // return; //} //var customPicker = e.NewElement as CustomPicker; //if (customPicker != null) //{ // Control.TextSize = Convert.ToSingle(customPicker.FontSize); //} } } }
自定义IOS Picker渲染器 - 工作正常:
using Attest.Combustivel.Infra.Application.Controls; using CoreGraphics; using System; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ExportRenderer(typeof(Attest.Combustivel.Infra.Application.Controls.CustomPicker), typeof(Attest.Combustivel.Target.Droid.Renderers.IOSCustomPickerRenderer))] namespace Attest.Combustivel.Target.Droid.Renderers { public class IOSCustomPickerRenderer : PickerRenderer { protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); if (Control == null) { return; } var customPicker = e.NewElement as CustomPicker; // FontSize if (customPicker?.FontSize != null) { Control.Font = UIFont.FromName(".SF UI Text", Convert.ToSingle(customPicker.FontSize)); } } } }
错误:
06-20 16:15:02.261 W/art ( 7541): JNI RegisterNativeMethods: attempt to register 0 native methods for md5b60ffeb829f638581ab2bb9b1a7f4f3f.TableViewRenderer
06-20 16:15:02.261 W/art ( 7541): JNI RegisterNativeMethods: attempt to register 0 native methods for md5718175f0eae2ce5b6bb8ba01f33420ae.DroidCustomTableViewRenderer
06-20 16:15:04.911 D/Mono ( 7541): DllImport attempting to load: '/system/lib/liblog.so'.
06-20 16:15:04.911 D/Mono ( 7541): DllImport loaded library '/system/lib/liblog.so'.
06-20 16:15:04.911 D/Mono ( 7541): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
06-20 16:15:04.911 D/Mono ( 7541): Searching for '__android_log_print'.
06-20 16:15:04.911 D/Mono ( 7541): Probing '__android_log_print'.
06-20 16:15:04.911 D/Mono ( 7541): Found as '__android_log_print'.
06-20 16:15:04.921 I/MonoDroid( 7541): UNHANDLED EXCEPTION:
06-20 16:15:04.981 I/MonoDroid( 7541): System.ArgumentException: element is not of type Xamarin.Forms.TableView
06-20 16:15:04.981 I/MonoDroid( 7541): Parameter name: element
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:135
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:289
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x00023] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:84
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:217
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:68
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:335
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x00111] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:199
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:137
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:289
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x00023] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:84
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:217
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:68
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:335
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x00111] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:199
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:137
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:289
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x00023] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:84
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:217
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:68
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:335
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x00111] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:199
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:137
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Support.V4.App.FragmentManager fragmentManager) [0x00031] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:325
06-20 16:15:04.981 I/MonoDroid( 7541): at Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnCreateView (Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState) [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:67
06-20 16:15:04.981 I/MonoDroid( 7541): at Android.Support.V4.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_inflater, System.IntPtr native_container, System.IntPtr native_savedInstanceState) [0x00022] in <27c17fe440cf491ba8255bcefade6e02>:0
06-20 16:15:04.981 I/MonoDroid( 7541): at (wrapper dynamic-method) System.Object:63a8a4b1-f4fe-4a75-89d0-ed93d90f2880 (intptr,intptr,intptr,intptr,intptr)
答案 0 :(得分:2)
我们从异常中发现有相同类型的渲染器。
答案 1 :(得分:1)
尝试将OnElementChanged
的覆盖方法更改为以下内容:
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Picker> e)
{
base.OnElementChanged(e);
if (Control == null)
{
return;
}
var customPicker = e.NewElement as CustomPicker;
if (customPicker != null)
{
Control.TextSize = Convert.ToSingle(customPicker.FontSize);
}
}