InverseBoolConverter 像标记扩展一样使用,但不是从 MarkupExtension 派生的。 XLS0505 XAML
Posted
技术标签:
【中文标题】InverseBoolConverter 像标记扩展一样使用,但不是从 MarkupExtension 派生的。 XLS0505 XAML【英文标题】:InverseBoolConverter is used like a markup extension but does not derive from MarkupExtension. XLS0505 XAML 【发布时间】:2021-07-20 02:27:23 【问题描述】:我正在使用 Xamarin Forms 并尝试实现一个加载叠加层,该叠加层将显示文本显示旋转的东西并禁用按钮。
由于加载时需要禁用按钮I am trying to useInverseBoolConverter
它在 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=""
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=""
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;
肯定是已知问题,已经有人铺路,如何解决?
【问题讨论】:
您在资源中定义了吗? docs.microsoft.com/en-us/xamarin/community-toolkit/converters/… 【参考方案1】:为了将其用作标记扩展,转换器实现需要实现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=""
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=""
FontSize="40"
VerticalOptions="StartAndExpand"
Grid.Row="2"
IsEnabled="Binding IsLoading, Converter=StaticResource InverseBoolConverter"
Command="Binding OpenGaleryCommand"
Style="StaticResource GrayButtonWhiteText"
FontFamily="StaticResource FontAwesomeSolid"/>
一种更简单的方法是使用现有的东西(而不是重新发明***), invertedboolconverter 来自 XamarinCommunityToolkit 包。
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
<Button Padding="20"
Margin="0,5,0,0"
Text=""
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
【讨论】:
嗨 Cfun,感谢您的回答。添加工具包后,我现在收到`Error NETSDK1136 The target platform must be set to Windows (通常通过在 TargetFramework 属性中包含'-windows'),当使用 Windows 窗体或 WPF 或引用这样做的项目或包时。xxx.Tests C:\Program Files\dotnet\sdk\5.0.202\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets 362
这个你见过吗?
从来没有遇到过这种情况,你的目标是 wpf 吗?
仅没有 xamarin
Android 和 iOS 没有 UWP 或其他任何东西
StaticResource
选项对我有用(感谢@Cfun!)但我不得不给x:Key
值一个不同的名称,因为InverseBoolConverter
与类名发生冲突。以上是关于InverseBoolConverter 像标记扩展一样使用,但不是从 MarkupExtension 派生的。 XLS0505 XAML的主要内容,如果未能解决你的问题,请参考以下文章
将 laravel 5 内置身份验证扩展为仅登录“如果用户 == 活动”