How to define an constructor argument accessible from Xaml

前端 未结 1 940
我寻月下人不归
我寻月下人不归 2021-01-15 22:48

Xamarin\'s ListView defines a 1-argument constructor as follows:

public ListView([Parameter(\"CachingStrategy\")] ListViewCachingStrategy cachin         


        
相关标签:
1条回答
  • 2021-01-15 23:09

    To make things simpler, I would recommend creating a BindableProperty for IsReadOnly. But you can always use x:Arguments to pass in parameters to constructor:

    <local:ItemListControl ...>
        <x:Arguments>
            <x:Boolean>true</x:Boolean>
        </x:Arguments>
    </local:ItemListControl>
    

    EDIT - 1

    There is one hack that you can use - (I wouldn't recommend as this could change anytime with an update in XAMLC compilation) - but you can make sure to keep the namespace same as the one used internally while defining the parameter attribute.

    namespace Xamarin.Forms
    {
        [AttributeUsage(AttributeTargets.Parameter)]
        internal sealed class ParameterAttribute : Attribute
        {
            public ParameterAttribute(string name)
            {
                Name = name;
            }
    
            public string Name { get; }
        }
    }
    

    And XAML usage would look like:

    <local:ItemListControl IsReadOnly="true" .. />
    

    EDIT - 2 This hack only seems to work if XAMLCompilation is applied to host control/page.

    0 讨论(0)
提交回复
热议问题