xamarin - 导航到 Xamarin 表单页面时引发的 AccessViolationException

标签 xamarin uwp xamarin.forms

我正在尝试导航到 Xamarin Forms NavigationPage。但是,我试图在框架首次加载一些“ native ”UWP 页面后执行此操作。

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 的例子

最佳答案

I am trying to Navigate to a Xamarin Forms NavigationPage. However I am trying to do it after the Frame has been first loaded up with some "native" UWP pages.

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

This code results in a AccessViolationException. The page object is a Xamarin.Forms.Page but I have also tried it with a NavigationPage(page) wrapper.

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

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

I am trying to create a hybrid UWP app that starts with Native Pages and later launches some XF Pages. The official Xamarin Forms samples have an example of this for iOS and Android.

根据您的要求,您可以使用 DependencyService要实现这一点,请从 ContentPage 返回到 WindowsPage。更多详情请引用以下代码。

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 中心。请检查。

关于xamarin - 导航到 Xamarin 表单页面时引发的 AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44285177/

相关文章:

c# - 在 Xamarin Forms UWP 中突出显示选定的选项卡

android - Xamarin - 权限 "REQUEST_INSTALL_PACKAGES"- 哪个依赖项带来了此权限

c# - 编码问题。我哪里错了?

c# - ListView 的单元格重用问题和自定义渲染器的 View 单元格

ios - 具有背景图像和透明度的 View Controller xamarin

c# - 如何在继续执行之前等待 Navigation.PushAsync() 任务完成

c# - 使用 MVVM 在 UWP 中处理 ListView ItemClick 的最佳实践

xaml - 如何向 UWP 中的 MenuFlyout 添加图标?

c# - 从 xamarin 将长视频上传到 Web API 服务器

c# - 在 Xamarin 中停止 WebView