【问题标题】:Exception when starting a MvvmCross Uwp app: "System.AccessViolationException: Attempted to read or write protected memory"启动 MvvmCross Uwp 应用程序时出现异常:“System.AccessViolationException: Attempted to read or write protected memory”
【发布时间】:2016-01-08 22:49:13
【问题描述】:

我正在尝试使用 MvvmCross 创建一个具有最新 UWP 位 (RTM) 的 Windows 通用应用程序。在 App.xaml.cs 中,当我尝试运行时 -

var start = Mvx.Resolve<IMvxAppStart>();
start.Start();

我收到“System.AccessViolationException”-

Message: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

我创建了一个示例项目来重现该问题,它包含 -

  • 面向“.NET Framework 4.6”和“Windows 10”的可移植类库 (Test.NewCore)。
  • “通用 Windows”应用 (Test.Uwp),针对“Windows 10 (10.0; Build 10240)”
  • 我正在使用最新的 nuget 预发布包“MvvmCross 4.0.0-beta3”,我相信我已正确设置(我查看了此处的示例:https://github.com/MvvmCross/MvvmCross/tree/4.0/nuspec

你可以在这里下载测试项目:http://1drv.ms/1G6w2m3

完整的堆栈跟踪如下 -

System.AccessViolationException was unhandled
  HResult=-2147467261
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=Windows
  StackTrace:
       at Windows.UI.Xaml.Controls.Frame.Navigate(Type sourcePageType, Object parameter)
       at Cirrious.MvvmCross.WindowsUWP.Views.MvxWindowsViewPresenter.Show(MvxViewModelRequest request)
       at Cirrious.MvvmCross.WindowsUWP.Views.MvxWindowsMainThreadDispatcher.RequestMainThreadAction(Action action)
       at Cirrious.MvvmCross.ViewModels.MvxNavigatingObject.ShowViewModel[TViewModel](IMvxBundle parameterBundle, IMvxBundle presentationBundle, MvxRequestedBy requestedBy)
       at Cirrious.MvvmCross.ViewModels.MvxAppStart`1.Start(Object hint)
       at Test.Uwp.App.OnLaunched(LaunchActivatedEventArgs e)
  InnerException: 

我使用的是 Windows 10 RTM 和 Visual Studio 2015 RTM,没有 beta 位(MvvmCross 除外)。

我做错了什么?

我非常喜欢 MvvmCross,并且我开始将现有的 Windows 8.1 / Windows Phone 8.1 解决方案移植到 Uwp - 只是对在 Uwp 中使用最新的 MvvmCross 位进行一些探索性研究。

感谢您的帮助! @ehuna

【问题讨论】:

  • 你能把测试项目放在 Github repo 中吗?这样更容易查看。
  • 好的,我在这里添加了它:github.com/ehuna/MvvmCross.Test.Uwp
  • 嘿@ehuna 老实说我已经尝试更改您的 github 存储库,但也无法启动并运行它。所以也许我们可以向后工作?我在我的 github github.com/Depechie/MvvmCrossUWPSplitView 上添加了一个可用的 UWP 版本,也许你可以把它作为你的基础?

标签: c# mvvmcross uwp


【解决方案1】:

您使用的视图不正确,并且您得到的 AccessViolation - 诚然 - 根本没有帮助。问题似乎是运行时找不到您指定的视图,并因不明异常而崩溃。

TestView.xaml 看起来像

<views:MvxWindowsPage
    x:Class="Test.Uwp.Views.View1"
    ...

但应该是的

<views:MvxWindowsPage
    x:Class="Test.Uwp.Views.TestView"

分别你的 TestView.xaml.cs 看起来像

public sealed partial class TestView : MvxWindowsPage<TestViewModel>
{
    public TestView()
    {
    }
}

但它应该看起来像这样

public sealed partial class TestView : MvxWindowsPage
{
    public TestView()
    {
        this.InitializeComponent();
    }
}

然后一切都会正常工作。我获得的有趣见解:MvxWindowsPage&lt;TestViewModel&gt; 似乎根本不起作用。

【讨论】:

    【解决方案2】:

    这是我发现的,可能会有所不同。 我创建了一个 UWP 应用(project1),添加了一个 UWP 库(project2),并且 project1 引用了 project2。 在库中添加一个页面。在 project1 app.xaml.cs 中,将 rootFrame.Navigate(typeof(MainPage), e.Arguments) 更改为 rootFrame.Navigate(typeof(Project2.MainPage), e.Arguments) 以便导航到 project2 中的页面。 运行并得到异常。 解决方法: 在project1 app.xaml中,添加以下内容来创建页面的实例(作为资源,没关系):

    xmlns:project2="using:project2"
    <Application.Resources>
    <ResourceDictionary>
    <project2:MainPage x:Key="MainPage"></view:MainPage>
    </ResourceDictionary>
    </Application.Resources>
    

    运行应用程序,它可以工作。 所以看起来异常的原因是其他项目中的页面尚未实例化引起的。如果在 app.xaml 中实例化它,它就可以工作。

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多