【问题标题】:Xamarin Forms custom view not rendering on real Android deviceXamarin Forms 自定义视图不在真正的 Android 设备上呈现
【发布时间】:2019-04-03 08:32:38
【问题描述】:

我是 Xamarin 表单的新手。我已经为 Android 制作了一个 Entry 渲染器。自定义渲染器在 Android 模拟器上运行良好,但是当我将它部署到真正的 android 设备上时应用程序崩溃。有什么建议!

这是代码!

入门类

命名空间 ARO {
公共类 Rounded_Entry: 条目

{

}

}

Android 渲染器类

[程序集:ExportRenderer(typeof(Rounded_Entry),typeof(RoundedEntryRendererAndroid))]

命名空间 ARO.Droid {

public class RoundedEntryRendererAndroid : EntryRenderer
{
    public RoundedEntryRendererAndroid(Context context) : base(context)
    {

    }
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            //Control.SetBackgroundResource(Resource.Layout.layout1);
            var gradientDrawable = new GradientDrawable();
            gradientDrawable.SetCornerRadius(60f);
            gradientDrawable.SetStroke(5, Android.Graphics.Color.DeepPink);
            gradientDrawable.SetColor(Android.Graphics.Color.LightGray);
            Control.SetBackground(gradientDrawable);

            Control.SetPadding(50, Control.PaddingTop, Control.PaddingRight,
                Control.PaddingBottom);
        }
    }
}

}

【问题讨论】:

  • 建议您应该展示您的代码,否则有人可以提供帮助的机会少于您可以修复错误的机会...
  • @IvanIčin 添加了代码。
  • 尝试更改您的 if 语句,即 if (Control != null && e !=null)。对控制的保护似乎是必不可少的,请参阅此处docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
  • 我有一个新的简单 xamarin 表单解决方案,并使用您的渲染代码对其进行修改,并在我的 Android 设备上对其进行测试,它可以正常工作。我认为问题可能在您的代码的其他地方提出。如果附上完整的代码或至少附上与渲染相关的代码,将有助于解决问题。

标签: xamarin.forms


【解决方案1】:

渲染器中需要这一行

if (Control == null)
    return;

【讨论】:

  • 其实我是在摩托罗拉机上测试的,当我在三星设备上测试时,同样的代码可以完美运行,感谢您的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多