.NET Maui 自定义处理程序将参数从 xaml 代码传递到处理程序代码?

.NET Maui Custom Handler pass parameters from xaml code to handler code?

我有一个 .Net MAUI 应用程序。它有一个页面,我在其中使用一些自定义处理程序(自定义渲染器)作为我的控件。例如,我有一个标签被一些代码覆盖以在其周围创建边框:

    Microsoft.Maui.Handlers.LabelHandler.LabelMapper.AppendToMapping(nameof(IView.Background), (handler, view) =>
            {
                if (view is CustomHandlerLabelPriceTag)
                {
#if __ANDROID__
                    handler.NativeView.SetBackgroundColor(Colors.Red.ToNative());

                    var gradientDrawable = new GradientDrawable();
                    gradientDrawable.SetCornerRadius(70f);
                    gradientDrawable.SetStroke(5, global::Android.Graphics.Color.ParseColor(SharedAppMethods.GetColorByKey("ColorPriceTag")));
                    gradientDrawable.SetColor(global::Android.Graphics.Color.ParseColor(SharedAppMethods.GetColorByKey("ColorBackground")));
                    handler.NativeView.SetBackgroundDrawable(gradientDrawable);

                    handler.NativeView.SetPadding(handler.NativeView.PaddingLeft, handler.NativeView.PaddingTop, handler.NativeView.PaddingRight, handler.NativeView.PaddingBottom);
#elif __IOS__
                    handler.NativeView.BackgroundColor = Colors.Red.ToNative();
                    handler.NativeView.BorderStyle = UIKit.UITextBorderStyle.Line;

                    handler.NativeView.Layer.CornerRadius = 30;
                    handler.NativeView.Layer.BorderWidth = 3f;
                    handler.NativeView.Layer.BorderColor = Color.FromArgb(SharedAppMethods.GetColorByKey("ColorPriceTag")).ToCGColor();
                    handler.NativeView.Layer.BackgroundColor = Color.FromArgb(SharedAppMethods.GetColorByKey("ColorBackground")).ToCGColor();

                    handler.NativeView.LeftView = new UIKit.UIView(new CGRect(0, 0, 0, 0));
                    handler.NativeView.LeftViewMode = UIKit.UITextFieldViewMode.Always;
#endif
                }

它有这个class,这里不需要太多:

using Microsoft.Maui.Controls;

namespace SheeperMAUI.CustomHandlers
{
    internal class CustomHandlerLabelPriceTag : Label
    {
    }
}

这就是我在页面上的 XAML 代码中使用它的方式:

 <customHandler:CustomHandlerLabelPriceTag 
        Text="{Binding text}" FontSize="15" 
        Padding="9,0,9,0" TextColor="{StaticResource ColorText}" 
        HorizontalOptions="CenterAndExpand" 
        VerticalOptions="Center" HorizontalTextAlignment="Center" 
        VerticalTextAlignment="Center"/>

我的问题:我希望能够将上面 XAML 代码中的字符串(通过将其绑定到我已有的字符串,即使用 {Binding ...})传递给自定义处理程序,其中该字符串将用于在自定义处理程序代码中设置边框颜色。我只需要知道如何传递那个值,剩下的我自己解决;)这可能吗?

谢谢!

从根本上说,您的自定义控件需要一个自定义 BindableProperty。然后处理程序可以访问 属性.

此答案显示 Xamarin Forms 代码。应该很容易适应MAUI Handler.

CustomHandlerLabelPriceTag.xaml.cs中:

public class CustomHandlerLabelPriceTag : Label
{
    // The property that will contain this special string.
    public static readonly BindableProperty MyStringProperty =
        BindableProperty.Create(nameof(MyString), typeof(string), typeof(MainPage), "");

    public double MyString
    {
        get { return (double)GetValue(MyStringProperty); }
        set { SetValue(MyStringProperty, value); }
    }
}

要在页面上使用它,我将其命名为 MyPage.xaml.cs:

public string MySpecialString { get; set; }

MyPage.xaml 中,将控件的 MyString 绑定到 BindingContext 的相应 public 字符串 属性。在这里,我假设它是 MySpecialString,并且它在 MyPage 的代码后面,所以“Source”是“this”:

<customHandler:CustomHandlerLabelPriceTag MyString={Binding MySpecialString, Source={x:Reference this}} ... />

在自定义渲染器中(希望在 MAUI 处理程序中类似):

// In XF, `Element` is the XF view being rendered.
if (Element != null) {
    string specialString = Element.MyString;
    // OR cast if necessary:
    string specialString = ((CustomHandlerLabelPriceTag)Element).MyString;
}

UPDATE - 对于 MAUI 处理程序(基于以下评论):

string PassedColorParameter = ((CustomHandlerLabelInfoCard)view).MyString;