Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

iOS CarouselPageRenderer causes exception on device(s) but works on simulators

$
0
0

I have a custom renderer for my app in iOS both Page, TabbedPage and CarouselPage that when used in a form called Modal just moves the "Cancel" button from right to left side so it shows as " Cancel pagetitle Save" rather than " pagetitle Cancel Save" and works for Page and TabbedPage but I'm getting an issue with the CarouselPage when running on a device it causes the following exception ...

Unable to cast object of type ‘myApp.iOS.puiFormCarouselPageRenderer' (Objective-C type: ‘myApp_iOS_puiFormCarouselPageRenderer') to type 'Xamarin.Forms.Platform.iOS.PageRenderer'.
Additional information:
Selector: viewDidLoad
Method: Xamarin.Forms.Platform.iOS.PageRenderer:ViewDidLoad ()

Yet if I run on simulators all is ok???

Note: tried 8.4 and 9.2 both devices and simulators and works on both simulators but both devices throw exception.

Here's the full code of custom renderer .. anyone have any idea if this a bug in Forms or something else?

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using System;

[assembly: ExportRenderer (typeof(myApp.ui_FormPage.pui_FormPage), typeof(myApp.iOS.puiFormPageRenderer))]
[assembly: ExportRenderer (typeof(myApp.ui_FormPage.pui_FormTabbedPage), typeof(myApp.iOS.puiFormTabbedPageRenderer))]
[assembly: ExportRenderer (typeof(myApp.ui_FormPage.pui_FormCarouselPage), typeof(myApp.iOS.puiFormCarouselPageRenderer))]

namespace myApp.iOS
{
public class puiFormPageRenderer : PageRenderer
{
bool updatedButtons = false;

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        if (!updatedButtons) 
        {
            MoveButtonToLeft();
        }
    }
    protected void MoveButtonToLeft()
    {
        var ni = NavigationController.TopViewController.NavigationItem;
        var cancelButton = ni.RightBarButtonItems [0];
        if (cancelButton.Title == "Cancel") 
        {
            ni.LeftBarButtonItem = cancelButton;
            var saveButton = ni.RightBarButtonItems[1];
            ni.RightBarButtonItems = new UIBarButtonItem[1] {saveButton};

            Page page = Element as Page;
            page.ToolbarItems.RemoveAt(1);
        }
        updatedButtons = true;
    }
}
public class puiFormTabbedPageRenderer : TabbedRenderer
{
    bool updatedButtons = false;

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear (animated);

        if (!updatedButtons) 
        {
            MoveButtonToLeft();
        }
    }
    protected void MoveButtonToLeft()
    {
        var ni = NavigationController.TopViewController.NavigationItem;
        var cancelButton = ni.RightBarButtonItems [0];
        if (cancelButton.Title == "Cancel") 
        {
            ni.LeftBarButtonItem = cancelButton;
            var saveButton = ni.RightBarButtonItems[1];
            ni.RightBarButtonItems = new UIBarButtonItem[1] {saveButton};

            Page page = Element as Page;
            page.ToolbarItems.RemoveAt(1);
        }
        updatedButtons = true;
    }
}
public class puiFormCarouselPageRenderer : CarouselPageRenderer
{
    bool updatedButtons = false;

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        if (!updatedButtons) 
        {
            MoveButtonToLeft();
        }
    }
    protected void MoveButtonToLeft()
    {
        var ni = NavigationController.TopViewController.NavigationItem;
        var cancelButton = ni.RightBarButtonItems [0];
        if (cancelButton.Title == "Cancel") 
        {
            ni.LeftBarButtonItem = cancelButton;
            var saveButton = ni.RightBarButtonItems[1];
            ni.RightBarButtonItems = new UIBarButtonItem[1] {saveButton};

            Page page = Element as Page;
            page.ToolbarItems.RemoveAt(1);
        }
        updatedButtons = true;
    }
}

}


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>