如何使用相同类型创建多个自定义渲染器?

发布于 2025-01-26 16:08:25 字数 1640 浏览 3 评论 0原文

我想创建一个使用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)]

,然后上面的代码在工作,而不是两个。 请帮忙

I wanted to create a page render with ContentPage type. I created so in android and it is working but in IOS there has custom page renderer with same type (ContentPage). It can be removed as this was from nuget package and working on different context.

Here is my code

[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);
    }
}

It seems it not registering and that is why not calling.
I have another page renderer that is register in assembly

[assembly: ExportRenderer(typeof(ContentPage), typeof(IOSToolbarExtensions.iOS.Renderers.IOSToolbarExtensionsContentPageRenderer), Priority = short.MaxValue)]

If I removed this line then above code is working but not two in the same time.
Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2025-02-02 16:08:25

相同类型似乎对多个渲染器不起作用。
我已经创建了自定义渲染器的子类型,并覆盖了所需的方法。它运行良好

Same type seems not working for multiple renderer.
I have created sub type of my custom renderer and override the methods which needed to. It is working well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文