AccessViolationException引发导航到Xamarin表单页面

问题描述:

我想导航到Xamarin表单导航页面。不过,我正在尝试在Frame首次加载一些“本机”UWP页面后执行此操作。AccessViolationException引发导航到Xamarin表单页面

this.rootFrame.Navigate(page.GetType(), param); 

此代码的结果为AccessViolationException。页面对象是一个Xamarin.Forms.Page,但我也尝试过使用NavigationPage(page)包装器。

我意识到普通的XF导航是通过Navigation.PushAsync等完成的,但我在XF应用程序和导航基础设施尚未初始化的时候。

这是我如何创建我的初始NavigationPage

XFormsApp.MainPage = new NavigationPage(contentPage); 

var converter = Mvx.Resolve<IMvxNavigationSerializer>(); 
var requestText = converter.Serializer.SerializeObject(request); 

_rootFrame.Navigate(mainPage.GetType(), requestText); 

我想创建一个混合UWP应用程序,与本地的页面开始,后来推出了一些XF页面。官方Xamarin Forms samples有这样的iOS和Android的例子

我想导航到Xamarin表单导航页面。不过,我正在尝试在Frame首次加载一些“本机”UWP页面后执行此操作。

this.rootFrame.Navigate(page.GetType(), param);

此代码导致AccessViolationException。页面对象是一个Xamarin.Forms.Page,但我也尝试过使用NavigationPage(page)包装器。

rootFrame.Navigate方法中的第一个参数的类型是forms:WindowsPage。但在你的代码中,你已经使用ContentPage作为第一个参数,在Xamarin.Forms命名空间下,它不是WindowsPage。所以它会抛出异常。

您无法导航到本地客户端项目中的Xamarin Forms Page。因为使用的页面在Xamarin.Forms和UWP中不同。该页面在uwp中继承Windows.UI.Xaml.Controls.Page,并且继承了TemplatedPage

我正尝试创建一个混合UWP应用程序,该应用程序以本地页面启动,然后启动一些XF页面。 Xamarin Forms官方示例中有iOS和Android的示例。

为了您的要求,您可以使用DependencyService实现这一回从ContentPageWindowsPage。有关更多详细信息,请参阅以下代码。

MainPage.xaml.cs中

public sealed partial class MainPage 
{ 
public MainPage() 
{ 
    this.InitializeComponent(); 

} 

private void MyBtn_Click(object sender, RoutedEventArgs e) 
{ 
    LoadApplication(new App65.App()); 
} 
} 

MainPage.xaml中(便携式)

<StackLayout> 
    <Label Text="Welcome to Xamarin Forms!" 
     VerticalOptions="Center" 
     HorizontalOptions="Center" /> 
    <Button Text="GoBack" Clicked="Button_Clicked"/> 
</StackLayout> 

GoBackImplementation。CS

[assembly: Xamarin.Forms.Dependency(typeof(GoBackImplementation))] 

namespace App65.UWP 
{ 
    public class GoBackImplementation : IGoBack 
    { 
     public void GoBack() 
     { 
      Frame rootFrame = Window.Current.Content as Frame; 
      if (rootFrame != null) 
      { 
       rootFrame = new Frame(); 
       rootFrame.NavigationFailed += RootFrame_NavigationFailed; 

       Window.Current.Content = rootFrame; 
      } 
      if (rootFrame.Content == null) 
      { 
       rootFrame.Navigate(typeof(MainPage)); 
      } 
     } 

     private void RootFrame_NavigationFailed(object sender, Windows.UI.Xaml.Navigation.NavigationFailedEventArgs e) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

enter image description here

我已经上传code sample与git枢纽。请检查。 “

+0

”rootFrame.Navigate方法中第一个参数的类型是窗体:WindowsPage“这是不正确的。该类型是“System.Type”。将NavigationPage作为类型传递正是MVVMCross所做的“所有UWP”应用程序,您可以在这里看到https://github.com/MvvmCross/MvvmCross/tree/develop/TestProjects/Forms/Example001XAML。请参阅Presenter.Your DependenmcyService方法不适用于MVVMCross,即使它确实如此,我不认为“回头”是我的要求 –

+0

也许我的描述不清楚。该类型是['sourcePageType'](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.frame#remarks)而不是'System.Type'。所以当你通过带有NavigationPage(page)包装器的'Xamarin.page'时,它会抛出异常。 –

+0

如果这是正确的“你知道它为什么抛出一个异常,为什么它会是AccessViolationException比类似InvalidArgumentException更大,但更重要的是传递一个NavigationPage类型作为第一个参数就是它在MVVMCross中的工作原理。 https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross-Forms/MvvmCross.Forms.Uwp/Presenters/MvxFormsUwpPagePresenter.cs#L30。我会为您创建一个更简单的repro,但我的环境目前已被拧紧 –