繁体   English   中英

Xamarin Forms XAML-XAML中的布尔属性集

[英]Xamarin Forms XAML - Boolean attribute set from XAML

我不了解有关如何为布尔值的xaml设置对象属性的知识。

我有一个这样的MainPage.xaml ,其中我将ProportionalSize设置为true

<ContentPage.Resources>
  <ResourceDictionary>
    <converter:BooleanConverter x:Key="Boolean"/>
  </ResourceDictionary>
</ContentPage.Resources>

<ContentPage.Content>
  <!-- Background during loading of start -->
  <AbsoluteLayout>
    <local:CustomImage Source="{extension:ImageResource HomeBG.png}"
                     ProportionalWidth="100" ProportionalHeight="100" ProportionalSize="{True, Converter={StaticResource Boolean}}"
                     AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                     AbsoluteLayout.LayoutFlags="All"/>
  </AbsoluteLayout>
</ContentPage.Content>  

我出于某种原因使用customImage,这是该类

public class CustomImage : Image
{
    private bool _ProportionalSize;
    public bool ProportionalSize
    {
        get { return this._ProportionalSize; }
        set
        {
            this._ProportionalSize = value;
            TrySize();
        }
    }
}

因为trueTrue 不起作用 ,所以我做了一个BooleanConverter

public class BooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (bool)value;
    }
}

但是,它仍然不起作用...


附加信息:位置19:75。 找不到真正的 ProportionalSize="{True, Converter={StaticResource Boolean}}" MarkupExtension


我做错什么了吗?

只需设置该值,就无需使用标记扩展语法(那些“ {}”括号)或转换器:

ProportionalSize="True"

如果您实际上没有绑定到将改变的值,请不要使用转换器或属性。 看起来你只是想设置true一次在XAML。 您可以为此使用x:Arguments属性。

<x:Arguments>
    <x:Boolean>True</x:Boolean>
</x:Arguments>

来自DataTrigger更大示例。

用例-绑定到IsVisible的网格具有一个不同的值,但是如果管理员登录,我们要覆盖该值。因此,除了常规绑定之外,我们还可以放入一个使用x:Booleanx:Arguments的数据触发器。将其设置为true -没有转换器...没有属性。

<Grid.Triggers>
    <DataTrigger Binding="{Binding IsAdmin}"
        TargetType="{x:Type Grid}"
        Value="True">
        <Setter Property="IsVisible">
            <Setter.Value>
                <x:Arguments>
                    <x:Boolean>True</x:Boolean>
                </x:Arguments>
            </Setter.Value>
        </Setter>
    </DataTrigger>
</Grid.Triggers>

尝试使用BindableProperty

    public static readonly BindableProperty ProportionalSizeProperty =
            BindableProperty.Create(nameof(ProportionalSize),
                                    typeof(bool),
                                    typeof(CustomImage),
                                    default(bool),
                                    propertyChanged: OnProportionalSizeChanged);

    public bool ProportionalSize
    {
        get { return (bool)GetValue(ProportionalSizeProperty); }
        set { SetValue(ProportionalSizeProperty, value); }
    }

    private static void OnProportionalSizeChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var customImage = bindable as CustomImage;
        if (customImage != null)
        {
            customImage.TrySize();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM