【发布时间】:2022-01-12 01:58:49
【问题描述】:
我有一个 .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
}
它有这个类,这里不需要太多:
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 ...})传递给自定义处理程序,该字符串将用于在自定义处理程序代码中设置边框颜色。我只需要知道如何传递该值,其余的我可以自己解决;)这可能吗?
谢谢!
【问题讨论】:
标签: c# xamarin xamarin.forms maui