【问题标题】:Set Xamarin Forms custom renderer control to android resource xml将 Xamarin Forms 自定义渲染器控件设置为 android 资源 xml
【发布时间】:2021-08-06 09:18:48
【问题描述】:

我正在尝试在 Xamarin.Forms 项目上使用材料搜索栏,但原生搜索栏还不够好(谈论外观)。我找到了这个包https://github.com/FANMixco/Xamarin-SearchBar,这正是我需要的,但我不知道如何将这个搜索栏的android资源xml链接到实际视图。我熟悉自定义渲染器,但我似乎找不到覆盖控件并将布局设置为 xml 资源的方法。我知道如何以这种方式设置样式,但不知道整个控件。

如果有人知道,我将不胜感激。 谢谢!

【问题讨论】:

    标签: android xml xamarin xamarin.forms searchbar


    【解决方案1】:

    使用SetNativeControl 方法。

    用法:

        protected override void OnElementChanged(ElementChangedEventArgs<SomeXamarinElement> e)
        { 
             base.OnElementChanged(e);
    
            // If element is being removed from view.
            if (e.NewElement == null)
                return;
    
             ...
             SetNativeControl(...);
        }
    

    ... 正在膨胀并使用本机控件。将 SomeXamarinElement 替换为此渲染器所针对的类的名称。

    搜索 Xamarin.Android 文档或教程,了解如何使用特定 XML 扩展 Android 控件。

    (在 Android 自定义渲染器中,您正在编写 Xamarin.Android 代码。这就像用 java 编写本机应用程序,除了它在 c# 中,具有不同的命名约定。因此也可以通过学习编写的材料来学习概念用于不使用 Xamarin 的 Android 开发。)

    【讨论】:

      猜你喜欢
      • 2018-07-23
      • 1970-01-01
      • 2018-07-29
      • 2016-09-03
      • 1970-01-01
      • 2018-08-19
      • 2021-06-18
      • 1970-01-01
      • 2018-01-04
      相关资源
      最近更新 更多