2015-10-11 144 views
2

我尝试使用MvvmCross创建具有最新UWP位(RTM)的Windows通用应用程序。在App.xaml.cs,当我尝试运行 -启动MvvmCross Uwp应用程序时出现异常:“System.AccessViolationException:试图读取或写入受保护的内存”

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

+1

你可以把测试项目放在Github仓库吗?这会让你更容易看。 – Martijn00

+0

好吧,我在这里添加它:https://github.com/ehuna/MvvmCross.Test.Uwp – ehuna

+0

嘿@ehuna说实话我已经尝试改变你的github回购,但也无法启动和运行。所以也许我们可以倒退?我在我的github上添加了一个可用的UWP版本https://github.com/Depechie/MvvmCrossUWPSplitView,或许你可以把它当作你的基地? – Depechie

回答

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<TestViewModel>似乎根本不起作用。

1

这是我发现的,它可能不同。 我创建一个UWP应用程序(project1),添加一个UWP库(project2)和project1参考project2。 在库中添加一个页面。在project1 app.xaml.cs中,更改rootFrame.Navigate(typeof(MainPage),例如。参数)转换为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中实例化它,它会起作用。

相关问题