2017-10-17 117 views
0

我使用PageRenderer時遇到了一些麻煩。Xamarin Forms UWP 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(這是我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; 
     } 
    } 
} 

總是有 拋出異常: 'System.InvalidOperationException' 在Xamarin.Forms.Platform.UAP.dll 錯誤的構建過程中。

但我想這不是一個真正的問題與PageRenderer。看起來這是在ClickEvent期間出現的。

+0

好的,問題是使用Navigation.PushAnsync,而不是PushModalAsync。現在導航部分似乎工作(沒有我的DemoPage.cs中的exportRenderer程序集)。在導航到DemoPage.cs後,我的應用程序總是崩潰(退出代碼爲-1)。實施應該沒問題,或不? – flix

回答

0

總是有一個異常拋出:Xamarin.Forms.Platform.UAP.dll中的'System.InvalidOperationException'生成過程中出現錯誤。

問題是您還沒有將MainPage添加到NavigationPage。 Windows上不支持全局支持PushAsync方法。您可以將以下代碼添加到app.xaml.cs文件中以解決問題。

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

PushModalAsync - 按頁面到一個模式上下文。這將在應用程序內創建一個新的,獨立的導航上下文。所創建的模式可以通過硬件後退按鈕解除;似乎無法停止此功能。

所以PushModalAsync方法不取決於NavigationPage,它將適用於您當前的情況。

在導航到DemoPage.cs後,我的應用程序總是崩潰(退出代碼爲-1)。實施應該沒問題,或不?

我發現你沒有在你的PageRenderer中實現ArrangeOverride方法。而且你不會看到頁面的內容。

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