Pengikatan properti berbasis platform di XAML gagal

Saya menulis kode XAML dan C# di bawah ini untuk menyetel properti IsVisible dari ActivityIndicator ke True di Android dan iOS. Kode C# di belakang kode mengikat properti yang sama ke properti IsBusy dari konteks pengikatan di Windows. Kode ini berfungsi seperti yang diharapkan di semua platform.

# 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)));
}

Saya ingin memiliki perilaku yang sama hanya menggunakan XAML. Saya mencoba cuplikan kode di bawah ini tidak ada yang berhasil.

# 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>

Apakah mungkin melakukan ini di XAML?


person nlogn    schedule 12.09.2017    source sumber
comment
Saya rasa Anda tidak dapat menyetel atribut IsVisible dan elemen ActivityIndicator.IsVisible secara bersamaan seperti itu.   -  person Bradley Uffner    schedule 12.09.2017
comment
@BradleyUffner Sepertinya itu tidak menjadi masalah. Tidak ada cuplikan XAML di atas yang berfungsi dengan atau tanpa atribut IsVisible.   -  person nlogn    schedule 12.09.2017


Jawaban (1)


Saya tidak begitu yakin mengapa opsi BindingBase tidak berfungsi, tetapi untuk kasus lainnya, jelas bahwa masalahnya adalah ketidakcocokan tipe.

Untuk menyiasatinya, saya kira Anda dapat menggunakan penyetel gaya untuk memberikan default sambil menggantinya menggunakan 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>
person Sharada Gururaj    schedule 13.09.2017