Я написал приведенный ниже код XAML и C#, чтобы установить для свойства IsVisible
объекта ActivityIndicator
значение True
на Android и iOS. Код C# в отделенном коде привязывает одно и то же свойство к свойству IsBusy
контекста привязки в Windows. Этот код работает должным образом на всех платформах.
# 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. Я попробовал приведенные ниже фрагменты кода, ни один из которых не сработал.
# 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
. - person Bradley Uffner   schedule 12.09.2017