【问题标题】:Xamarin Forms - Android button using relative layour from Resources/LayoutXamarin Forms - 使用资源/布局中的相对布局的 Android 按钮
【发布时间】:2019-06-05 15:56:08
【问题描述】:

Visual Studio 2017 所以我有一个使用 Xamarin Forms 的应用程序,IOS 和 Android。 我已作为 RelativeLayout 文件下载到资源/布局中。

有没有办法可以在自定义按钮渲染器中使用此布局?

我们所有的表单都在 Xamarin 项目中,所以我不能只将它包含在片段中。

我是不是通过在客户渲染器中捏造使用它来鞭笞死马?

【问题讨论】:

  • 您想通过使用 Android RelativeLayout 与基于 Forms 的布局来实现什么?
  • 我需要显示一个按钮,该按钮对于 IOS 和 Android 将有所不同,并且实际上遵循相关第三方的品牌指南。

标签: android xamarin android-relativelayout


【解决方案1】:

为您的自定义按钮创建视图渲染器。然后用你的原生布局替换它:

[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace App.Droid
{
    public class CustomButtonRenderer : ViewRenderer<CustomButton, Android.Views.View>
    {
        Context _context;
        public CustomButtonRenderer(Context context) : base(context)
        {
            _context = context;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<CustomButton> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                SetNativeControl(getView());
            }
        }

        Android.Views.View getView()
        {
            return LayoutInflater.From(_context).Inflate(Resource.Layout.custom_layout, null);
        }
    }
}

最后,您可以使用 XAML 中的自定义控件,例如:

<StackLayout>
    <local:CustomButton />
</StackLayout>

【讨论】:

  • 你先生是明星!你让它看起来非常简单,它似乎对我有用。谢谢你。我是移动开发新手,我确实对 XAML 和片段之类的东西感到困惑,但我已经到了那里。
  • @AntDC 很高兴为您提供帮助。并随时在这里提问。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多