Xamarin Forms XAML -来自XAML的布尔属性集

本文关键字:XAML 布尔 属性 来自 Forms Xamarin | 更新日期: 2023-09-27 17:54:11

我不明白如何为xaml设置布尔对象属性。

我有一个主页。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都不能工作,我做了一个布尔转换器

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}}"


我做错了什么吗?

Xamarin Forms XAML -来自XAML的布尔属性集

只需设置值,不需要使用标记扩展语法(那些"{}"括号)或转换器:

ProportionalSize="True"

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

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

DataTrigger的更大示例。

Usecase - grid有一个不同的值绑定到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();
        }
    }