简体   繁体   中英

Xamarin Forms UWP PageRenderer

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM