我有一個 .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 ...})傳遞到自定義處理程式,該字串將用于設定邊框自定義處理程式代碼中的顏色。我只需要知道如何傳遞該值,其余的我可以自己解決;) 這可能嗎?
謝謝!
uj5u.com熱心網友回復:
從根本上說,您需要對自定義控制元件BindableProperty進行自定義。然后處理程式可以訪問該屬性。
這個答案顯示了Xamarin Forms代碼。應該很容易適應MAUIHandler。
在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的相應公共字串屬性。在這里,我假設它是MySpecialString,并且它在 的代碼后面MyPage,所以“源”是“這個”:
<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;
}
更新- 對于MAUI 處理程式(基于下面的評論):
string PassedColorParameter = ((CustomHandlerLabelInfoCard)view).MyString;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/376343.html
標籤:C# 沙马林 xamarin.forms 毛伊岛
