【问题标题】:Xamarin access property of a custom renderer from xaml来自 xaml 的自定义渲染器的 Xamarin 访问属性
【发布时间】:2018-08-16 21:07:47
【问题描述】:

我有一个用于 xamarin 编辑器的 ios 自定义渲染,我试图将 Control.ScrollEnabled 属性在某些页面上设置为 true,在其他页面上设置为 false。

[assembly: ExportRenderer(typeof(ExtendedEditor), typeof(ExtendedEditorRenderer))]
namespace My.iOS.Renderers
{
    public class ExtendedEditorRenderer : EditorRenderer
    {
        public ExtendedEditor ExtendedEditorElement => Element as ExtendedEditor;

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

            if (e.NewElement != null)
            {
                Control.ScrollEnabled = false;
            }
        }
    }
}

上面是我的客户渲染器代码。如您所见,ScrollEnabled 属性始终设置为 false。 有没有办法让我可以访问这些属性,例如可滚动、边框颜色、背景颜色等,并从 XAML 中更改它们?

【问题讨论】:

    标签: xamarin.forms xamarin.ios custom-renderer


    【解决方案1】:

    您需要在 ExtendedEditor 控件上创建这些 Properties/BindableProperties,然后使用您创建的 ExtendedEditorElement 属性在渲染器中访问它们。

    当您在 ExtendedEditor 类中声明了属性后,您可以访问以下内容:

    protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
    {
        base.OnElementChanged(e);
    
        if (e.NewElement != null)
        {
            Control.ScrollEnabled = ExtendedEditorElement.ScrollEnabled;
        }
    }
    

    【讨论】:

    • 谢谢!这是我必须放在我的 ExtendedEditor 类中的代码。 public static readonly BindableProperty IsScrollEnabledProperty = BindableProperty.Create("IsScrollEnabled", typeof(bool), typeof(ExtendedEditor), false); public bool IsScrollEnabled { get { return (bool)GetValue(IsScrollEnabledProperty); } set { SetValue(IsScrollEnabledProperty, value); } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2018-08-19
    • 2020-04-06
    • 1970-01-01
    相关资源
    最近更新 更多