You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI中Button派生自定义控件的自定义属性样式设置问题

问题:自定义Button控件的绑定属性在资源字典样式中无法识别

我通过继承Button创建了自定义控件UiButton,用来实现鼠标悬停切换背景色的功能(已知不适用于触控设备)。但在资源字典中给该控件设置样式时,自定义属性HooverBackgroundColor无法被识别;直接在XAML视图中显式设置这个属性却能正常生效。另外,继承自ContentView的自定义控件在样式设置上没有这个问题,怀疑是.NET MAUI的Bug,想请教解决方法,以及如果确认为Bug该如何上报。

相关代码

C#自定义控件代码

public class UiButton : Button
{
    // 注意:此处BindableProperty名称存在拼写错误(少了一个'd')
    public static readonly BindableProperty HooverBackgrounColorProperty =
        BindableProperty.Create(nameof(HooverBackgroundColor),
            typeof(Color), typeof(UiButton), Colors.LightYellow,
            BindingMode.OneWay);

    protected Color originalBackgroundColor;

    public Color HooverBackgroundColor
    {
        get { return (Color)GetValue(HooverBackgrounColorProperty); }
        set { SetValue(HooverBackgrounColorProperty, value); }
    }

    public UiButton()
    {
        var pointerGestureRecognizer = new PointerGestureRecognizer();
        pointerGestureRecognizer.PointerEntered += PointerGestureRecognizer_PointerEntered;
        pointerGestureRecognizer.PointerExited += PointerGestureRecognizer_PointerExited;
        GestureRecognizers.Add(pointerGestureRecognizer);
    }

    private void PointerGestureRecognizer_PointerEntered(object sender, PointerEventArgs e)
    {
        originalBackgroundColor = BackgroundColor;
        BackgroundColor = HooverBackgroundColor;
    }


    private void PointerGestureRecognizer_PointerExited(object sender, PointerEventArgs e)
    {
        BackgroundColor = originalBackgroundColor;
        originalBackgroundColor = null;
    }
}

资源字典代码

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:ui="clr-namespace:MyExample.Frontend.Ui">

    <Color x:Key="ForegroundNotice">#FF008080</Color>
    <Color x:Key="ForegroundSubtle">#FF006060</Color>
    <Color x:Key="ForegroundHoover">Cyan</Color>

    <Style x:Key="FormButton" TargetType="{x:Type ui:UiButton}">
        <Setter Property="TextColor" Value="{StaticResource ForegroundNotice}"/>
        <Setter Property="BackgroundColor" Value="Transparent"/>
        <Setter Property="BorderWidth" Value="2"/>
        <Setter Property="BorderColor" Value="{StaticResource ForegroundNotice}"/>
        <!-- Maui Bug: it is not recognizing a custom property if the class derived from is Button -->
        <!--<Setter Property="HooverBackgroundColor" Value="{StaticResource ForegroundHoover}"/>-->
    </Style>

</ResourceDictionary>

解决方法

1. 修复BindableProperty名称拼写错误

这是核心问题:你的BindableProperty命名存在拼写错误——属性名为HooverBackgroundColor,但对应的绑定字段写成了HooverBackgrounColorProperty(少了一个'd',正确应为HooverBackgroundColorProperty)。

绑定属性的命名规范要求字段名必须是属性名 + "Property",不匹配的话XAML样式系统无法正确关联属性,导致Setter无法识别。修正后的代码如下:

public static readonly BindableProperty HooverBackgroundColorProperty =
    BindableProperty.Create(nameof(HooverBackgroundColor),
        typeof(Color), typeof(UiButton), Colors.LightYellow,
        BindingMode.OneWay);

public Color HooverBackgroundColor
{
    get { return (Color)GetValue(HooverBackgroundColorProperty); }
    set { SetValue(HooverBackgroundColorProperty, value); }
}

修正后即可在资源字典中正常使用Setter设置该属性。

2. 确认命名空间引用正确

确保资源字典中已经正确引用了自定义控件所在的命名空间(你的代码中已添加xmlns:ui="clr-namespace:MyExample.Frontend.Ui",这一步没问题)。

若确认为Bug的上报方式

如果上述方法都无效,确认是.NET MAUI框架问题,可按以下步骤上报:

  • 打开.NET MAUI的官方GitHub仓库
  • 点击顶部的「Issues」标签,选择「New issue」
  • 选择对应的Issue模板(如「Bug report」)
  • 详细填写问题描述:包括复现步骤、环境版本(.NET MAUI版本、操作系统)、相关代码片段、截图等信息,确保开发团队能快速复现问题。

内容的提问来源于stack exchange,提问作者G.Y

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:23:27