【发布时间】:2017-09-12 19:15:00
【问题描述】:
我编写了下面的 XAML 和 C# 代码,在 Android 和 iOS 上将 ActivityIndicator 的 IsVisible 属性设置为 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