0
votes

i got some troubles with using the PageRenderer.

MainPage.xml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="abc.CustomView">
    <ContentPage.Content>
            <StackLayout>
                <Button Text="scann" Clicked="BtnScannClicked"></Button>
            </StackLayout>
    </ContentPage.Content>

MainPage.cs

async void BtnScannClicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new CustomView());
        }

CustomView.Xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="abc.CustomView">
    <ContentPage.Content>
  </ContentPage.Content>
</ContentPage>

CustomView.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CustomView : ContentPage
    {
        public CustomView ()
        {
            InitializeComponent ();
        }
    }

DemoPage.cs (which is my CustomRenderer)

[assembly: ExportRenderer(typeof(CustomView), typeof(DemoPage))]
namespace abc.UWP
{
    class DemoPage: PageRenderer
    {
        Page page;
        Application app;


        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                return;
            }
            try
            {
                app = Application.Current;

                SetupUserInterface();
                this.Children.Add(page);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"      ERROR: ", ex.Message);
            }
        }


        void SetupUserInterface()
        {
            var stackPanel = new StackPanel();
            page = new Page();
            page.Content = stackPanel;
        }
    }
 }

There is always a Exception thrown: 'System.InvalidOperationException' in Xamarin.Forms.Platform.UAP.dll error during the build.

But I guess this is not really a problem with the PageRenderer. Seems that this appears during the ClickEvent.

1
ok, problem was the usage of Navigation.PushAnsync instead of PushModalAsync. Now the navigation part seems to work (without the exportRenderer assembly in my DemoPage.cs). Put my application is always crashing (has exit code -1) after the navigation to DemoPage.cs. The Implementation should be ok, or not? - flix

1 Answers

0
votes

There is always a Exception thrown: 'System.InvalidOperationException' in Xamarin.Forms.Platform.UAP.dll error during the build.

The problem is that you have not add the MainPage to NavigationPage. The PushAsync method is not supported globally on Windows. You could add the the following code to the app.xaml.cs file to solve the issue.

public App()
{
    InitializeComponent();
    var RootNav = new NavigationPage(new MainPage());
    MainPage = RootNav; 
}

PushModalAsync - Push a page into a modal context. This will create a new, independent, Navigation context within the application. The modal that is created can be dismissed with a hardware back button; there appears to no way to stop this functionality.

So the PushModalAsync method does not depend on NavigationPage, it will work in your current scenario.

Put my application is always crashing (has exit code -1) after the navigation to DemoPage.cs. The Implementation should be ok, or not?

I have found that you have not implemented ArrangeOverride method in your PageRenderer. And you will not see the content of page.

protected override Size ArrangeOverride(Size finalSize)
{
    page.Arrange(new Windows.Foundation.Rect(0, 0, finalSize.Width, finalSize.Height));
    return finalSize;        
}