InverseBoolConverter 像标记扩展一样使用,但不是从 MarkupExtension 派生的。 XLS0505 XAML

InverseBoolConverter is used like a markup extension but does not derive from MarkupExtension. XLS0505 XAML

我正在使用 Xamarin Forms 并尝试实现一个加载叠加层,该叠加层将显示文本显示旋转的东西并禁用按钮。

因为加载时需要禁用按钮 InverseBoolConverter

它在 iOS 上工作正常(发出警告,但我太酷了,无法在意),Android 却给出错误, Error XLS0505 Type 'Helpers:InverseBoolConverter' is used like a markup extension but does not derive from MarkupExtension. 而且构建机制不够酷,无法构建它,所以我遇到了问题。

XAML:

xmlns:Helpers="clr-namespace:xxx.MobileApp.Converters"
....

<AbsoluteLayout VerticalOptions="Fill">
    <Grid AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" BackgroundColor="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <ScrollView Grid.Row="1">
        <StackLayout Orientation="Vertical" Padding="30,-80,30,24" Spacing="10">
            <Image x:Name="previewImage" Source="{Binding PreviewImage}" IsVisible="false" />
            <Grid VerticalOptions="CenterAndExpand">
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
            <Button Padding="20"
                    Margin="0,0,0,5" 
                    Text="&#xf030;"
                    FontSize="40"
                    VerticalOptions="EndAndExpand"
                    Grid.Row="1"
                    IsEnabled="{Binding IsLoading, Converter={Helpers:InverseBoolConverter}}"
                    Command="{Binding OpenCameraCommand}"                        
                    Style="{StaticResource GrayButtonWhiteText}" 
                    FontFamily="{StaticResource FontAwesomeSolid}"/>
            <Button Padding="20"
                    Margin="0,5,0,0" 
                    Text="&#xf302;"
                    FontSize="40"
                    VerticalOptions="StartAndExpand"
                    Grid.Row="2"
                    IsEnabled="{Binding IsLoading, Converter={Helpers:InverseBoolConverter}}"
                    Command="{Binding OpenGaleryCommand}"     
                    Style="{StaticResource GrayButtonWhiteText}" 
                    FontFamily="{StaticResource FontAwesomeSolid}"/>
            </Grid>
        </StackLayout>
    </ScrollView>
</Grid>
    <StackLayout 
        AbsoluteLayout.LayoutFlags="PositionProportional" 
        AbsoluteLayout.LayoutBounds=".5,.5,-1,-1"
        Orientation="Vertical">
        
        <Label Text="Downloading the metadata"
               TextColor="Black"
               IsVisible="{Binding IsLoading, Mode=TwoWay}">
        </Label>
        <ActivityIndicator 
        Color="Black"
        Scale="5"
        HorizontalOptions="CenterAndExpand"
        VerticalOptions="CenterAndExpand" 
        IsRunning="{Binding IsLoading, Mode=TwoWay}" />
    </StackLayout>
</AbsoluteLayout>

转换器:

public class InverseBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !((bool)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

这肯定是已知问题,已经有人铺路了,怎么解决?

为了将其用作标记扩展,转换器实现需要实现 IMarkupExtension<IValueConverter> Creating XAML Markup Extensions,否则您可以以经典方式将其用作 StaticResource.

<ContentPage.Resources>
      <Helpers:InverseBoolConverter x:Key="InverseBoolConverter "/>
</ContentPage.Resources>
<Button Padding="20"
                    Margin="0,0,0,5" 
                    Text="&#xf030;"
                    FontSize="40"
                    VerticalOptions="EndAndExpand"
                    Grid.Row="1"
                    IsEnabled="{Binding IsLoading, Converter={StaticResource InverseBoolConverter}}"
                    Command="{Binding OpenCameraCommand}"                        
                    Style="{StaticResource GrayButtonWhiteText}" 
                    FontFamily="{StaticResource FontAwesomeSolid}"/>
            <Button Padding="20"
                    Margin="0,5,0,0" 
                    Text="&#xf302;"
                    FontSize="40"
                    VerticalOptions="StartAndExpand"
                    Grid.Row="2"
                    IsEnabled="{Binding IsLoading, Converter={StaticResource InverseBoolConverter}}"
                    Command="{Binding OpenGaleryCommand}"     
                    Style="{StaticResource GrayButtonWhiteText}" 
                    FontFamily="{StaticResource FontAwesomeSolid}"/>

一种更简单的方法是使用现有的东西(而不是重新发明轮子), invertedboolconverter from XamarinCommunityToolkit 包。

xmlns:xct="http://xamarin.com/schemas/2020/toolkit"

 <Button Padding="20"
                    Margin="0,5,0,0" 
                    Text="&#xf302;"
                    FontSize="40"
                    VerticalOptions="StartAndExpand"
                    Grid.Row="2"
                    IsEnabled="{Binding IsLoading, Converter={xct:InvertedBoolConverter}}"
                    Command="{Binding OpenGaleryCommand}"     
                    Style="{StaticResource GrayButtonWhiteText}" 
                    FontFamily="{StaticResource FontAwesomeSolid}"/>

InvertedBoolConverter source code