创建 .NET MAUI SingleProject 应用程序时,我尝试实现一些 XAML 代码,但无法编译它并收到奇怪的构建错误。
这正是我所做的。在我的 XAML 文件中,我有这样的代码:
<maui:IconButton
...some Properties
>
<maui:IconButton.Style>
<Style TargetType="{x:Type maui:IconButton}">
<Setter Property="IsVisible" Value="False" />
<Style.Triggers>
<DataTrigger
Binding="{Binding LokalBearbeitet, Mode=OneWay}"
TargetType="{x:Type maui:IconButton}"
Value="True">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</maui:IconButton.Style>
</maui:IconButton>
IconButton的定义如下:
public class IconButton : ImageButton
{
// Necessary implementation...
IsVisible 属性在命名空间 Microsoft.Maui.Controls:
中定义public partial class VisualElement ...
{
[TypeConverter(typeof(VisibilityConverter))]
public bool IsVisible
{
get { return (bool)GetValue(IsVisibleProperty); }
set { SetValue(IsVisibleProperty, value); }
}
当我编译此代码时,出现以下错误: 错误 XFC0040:无法将值“IsVisible”转换为“Microsoft.Maui.Controls.BindableProperty”。
我快要疯了。这种神秘行为的原因可能是什么?
将其定义为 BindableProperty
public static readonly BindableProperty IsVisibleProperty =
BindableProperty.Create ("IsVisible", typeof(bool), typeof(IconButton), false);
public bool IsVisible
{
get => (bool)GetValue(IsVisibleProperty);
set => SetValue(IsVisibleProperty, value);
}
然后在调用 xaml 中的 IconButton 绑定到 IsVisible 属性时使用它