我想創建一個 ContentPage 型別的頁面渲染。我在 android 中創建了它并且它正在作業,但在 IOS 中有具有相同型別(ContentPage)的自定義頁面渲染器。它可以被洗掉,因為它來自 nuget 包并在不同的背景關系中作業。
這是我的代碼
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]
namespace AlwinInvoiceGenerator.IOS
{
using CoreGraphics;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
public class CustomPageRenderer : PageRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (Element == null || !(Element is ContentPage basePage) || basePage.BindingContext == null ||
!(basePage.BindingContext is BaseViewModel baseViewModel))
{
return;
}
SetCustomBackButton(baseViewModel);
}
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;
}
private void SetCustomBackButton(BaseViewModel baseViewModel)
{
var fakeButton = new UIButton {Frame = new CGRect(0, 0, 50, 40), BackgroundColor = UIColor.Clear};
fakeButton.TouchDown = (sender, e) =>
{
baseViewModel.OnBackButtonClicked();
};
NavigationController?.NavigationBar.AddSubview(fakeButton);
}
}
似乎它沒有注冊,這就是為什么不打電話的原因。我有另一個在程式集中注冊的頁面渲染器
[assembly: ExportRenderer(typeof(ContentPage), typeof(IOSToolbarExtensions.iOS.Renderers.IOSToolbarExtensionsContentPageRenderer), Priority = short.MaxValue)]
如果我洗掉了這一行,那么上面的代碼可以作業,但不能同時作業兩個。請幫忙
uj5u.com熱心網友回復:
相同型別似乎不適用于多個渲染器。我已經創建了自定義渲染器的子型別并覆寫了需要的方法。它運作良好
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473733.html