【问题标题】:Platform-based property binding in XAML failsXAML 中基于平台的属性绑定失败
【发布时间】:2017-09-12 19:15:00
【问题描述】:

我编写了下面的 XAML 和 C# 代码,在 Android 和 iOS 上将 ActivityIndicatorIsVisible 属性设置为 True。代码隐藏中的 C# 代码将相同的属性绑定到 Windows 上绑定上下文的 IsBusy 属性。此代码在所有平台上都能按预期工作。

# XAML Code
<ActivityIndicator x:Name="AI" IsEnabled="{Binding Path=IsBusy}" IsVisible="True"  IsRunning="{Binding Path=IsBusy}"/>

# C# Code
if (Device.RuntimePlatform == Device.Windows)
{
    AI.SetBinding(IsVisibleProperty, new Binding(nameof(ViewModel.IsBusy)));
}

我希望仅使用 XAML 具有相同的行为。我尝试了下面的代码 sn-ps 都没有工作。

# 1
# Compiles. On Windows throws System.InvalidCastException: 
# 'Unable to cast object of type 'Xamarin.Forms.Binding' 
# to type 'System.Boolean'.'
<ActivityIndicator x:Name="AI" IsEnabled="{Binding Path=IsBusy}" IsRunning="{Binding Path=IsBusy}">
    <ActivityIndicator.IsVisible>
        <OnPlatform x:TypeArguments="x:Boolean">
            <On Platform="Android" Value="True"/>
            <On Platform="iOS" Value="True"/>
            <On Platform="Windows" Value="{Binding Path=IsBusy}"/>
        </OnPlatform>
    </ActivityIndicator.IsVisible>
</ActivityIndicator>

# 2
# Doesn't compile. Gives an error saying "No property, 
# bindable property, or event found for 'IsVisible'"
<ActivityIndicator x:Name="AI" IsEnabled="{Binding Path=IsBusy}" IsVisible="True" IsRunning="{Binding Path=IsBusy}">
    <ActivityIndicator.IsVisible>
        <OnPlatform x:TypeArguments="BindingBase">
            <On Platform="Windows" Value="{Binding Path=IsBusy}"/>
        </OnPlatform>
    </ActivityIndicator.IsVisible>
</ActivityIndicator>

# 3
# Doesn't compile. Gives an error saying "No property, 
# bindable property, or event found for 'IsVisible'"
<ActivityIndicator x:Name="AI" IsEnabled="{Binding Path=IsBusy}" IsVisible="True" IsRunning="{Binding Path=IsBusy}">
    <ActivityIndicator.IsVisible>
        <OnPlatform x:TypeArguments="Binding">
            <On Platform="Windows" Value="{Binding Path=IsBusy}"/>
        </OnPlatform>
    </ActivityIndicator.IsVisible>
</ActivityIndicator>

# 4
# Compiles. On Windows throws System.InvalidCastException: 
# 'Unable to cast object of type 'Xamarin.Forms.Binding' 
# to type 'System.Boolean'.'
<ActivityIndicator x:Name="AI" IsEnabled="{Binding Path=IsBusy}" IsVisible="True" IsRunning="{Binding Path=IsBusy}">
    <ActivityIndicator.IsVisible>
        <OnPlatform x:TypeArguments="x:Boolean">
            <On Platform="Windows">
                <Binding Path="IsBusy" />
            </On>
        </OnPlatform>
    </ActivityIndicator.IsVisible>
</ActivityIndicator>

是否可以在 XAML 中执行此操作?

【问题讨论】:

  • 我认为你不能像这样同时设置IsVisible 属性和ActivityIndicator.IsVisible 元素。
  • @BradleyUffner 这似乎不是问题。无论如何,上面的 XAML sn-ps 都不能使用或不使用 IsVisible 属性。

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

我不确定为什么BindingBase 选项不起作用,但对于其余情况,很明显问题是类型不匹配。

为了解决这个问题,我猜你可以使用样式设置器来提供默认值,同时使用 OnPlatform 覆盖它们:

//App or Page resources to set default properties 
<ResourceDictionary>
    <Style TargetType="ActivityIndicator" x:Key="AIStyle">
        <Setter Property="IsEnabled" Value="{Binding Path=IsBusy}" />
        <Setter Property="IsRunning" Value="{Binding Path=IsBusy}" />
    </Style>
</ResourceDictionary>

//And use following to define control
<OnPlatform x:TypeArguments="View">
    <On Platform="Android, iOS">
        <ActivityIndicator IsVisible="true" Style="{StaticResource AIStyle}" />
    </On>
    <On Platform="Windows">
        <ActivityIndicator IsVisible="{Binding Path=IsBusy}" Style="{StaticResource AIStyle}" />
    </On>
</OnPlatform>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多