简体   繁体   English

将Prism.unity 6.2.0添加到Windows 10 UWP之后,应用程序引发异常

[英]After adding Prism.unity 6.2.0 to Windows 10 UWP, application throws exception

When creating a Universal Windows Application with Prism, I always receive an error upon running the application (or, sometimes, upon exiting the running application). 使用Prism创建通用Windows应用程序时,在运行该应用程序时(有时退出运行的应用程序时),我总是会收到错误消息。 Here are the steps I take to reproduce the problem: 这是我要重现该问题的步骤:

  • Create a new UWP application Using Visual Studio 2015. 使用Visual Studio 2015创建新的UWP应用程序。
  • Use NuGet to install Prism (Install-Package Prism.unity) 使用NuGet安装Prism(Install-Package Prism.unity)
  • Change the App class to inherit from PrismUnityApplication (code below) 更改App类以从PrismUnityApplication继承(下面的代码)
  • Create a folder called Views and add a new page called MainPage, making sure that it is in the correct namespace 创建一个名为Views的文件夹,并添加一个名为MainPage的新页面,确保该文件夹位于正确的名称空间中

Code: 码:

using Microsoft.Practices.Unity;
using Prism.Events;
using Prism.Mvvm;
using Prism.Unity.Windows;
using Prism.Windows.AppModel;
using Prism.Windows.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Resources;
using Windows.System;
using Windows.UI.Notifications;
using Windows.UI.Xaml;

namespace TestAppForPrism {
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : PrismUnityApplication {

    public App() {
        this.InitializeComponent();
    }

    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args) {
        NavigationService.Navigate("Main", null);
        return Task.FromResult<object>(null);
    }

    protected override Task OnInitializeAsync(IActivatedEventArgs args) {
        Container.RegisterInstance<INavigationService>(NavigationService);

        return base.OnInitializeAsync(args);
    }
}
}

When I run the application, an exception is thrown: 当我运行应用程序时,会引发异常:

Could not load file or assembly 'System.Runtime.Serialization.Xml, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. 无法加载文件或程序集“ System.Runtime.Serialization.Xml,版本= 4.1.1.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a”或其依赖项之一。 The located assembly's manifest definition does not match the assembly reference. 找到的程序集的清单定义与程序集引用不匹配。 (Exception from HRESULT: 0x80131040) (来自HRESULT的异常:0x80131040)

Am I setting things up incorrectly? 我设置不正确吗?

The problem is that the default UWP template in Visual Studio 2015 still comes with "Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0" , while this should be version 5.2.2. 问题是Visual Studio 2015中的默认UWP模板仍带有"Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0" ,而该版本应为5.2.2。

So simply update that package to 5.2.2 and everything should work. 因此,只需将该程序包更新为5.2.2,一切正常。


Small sidenote on your code: 代码上的小注释:

  • return Task.CompletedTask if you don't have to execute a task. 如果您不必执行任务,则返回Task.CompletedTask。 This is added in .NET 4.6. 这是在.NET 4.6中添加的。 It's a bit more performant. 性能更高。
  • there's no need to register the INavigationService in your container, that's already done in the base class. 无需在您的容器中注册INavigationService,这已经在基类中完成了。

So your code can be trimmed down to 因此您的代码可以精简为

sealed partial class App : PrismUnityApplication
{

    public App()
    {
        this.InitializeComponent();
    }

    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
    {
        NavigationService.Navigate("Main", null);
        return Task.CompletedTask;
    }
}

After playing around for a while, I think I found a solution: 玩了一段时间后,我想我找到了一个解决方案:

In the NuGet console, I ran "update-package -reinstall", and then rebuilt the solution. 在NuGet控制台中,我运行“ update-package -reinstall”,然后重新构建解决方案。 I still have a warning in the error window that says "Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.", but I seem to be able to run the application without receiving that exception any more. 我仍然在错误窗口中看到一条警告,提示“发现无法解决的相同从属程序集的不同版本之间存在冲突。将日志详细程度设置为详细时,这些引用冲突会在构建日志中列出。”以便能够运行应用程序而不会再收到该异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该在Prism模块引用中添加“ Prism.Unity 6.2.0”还是仅添加“ Microsoft.Practices.Unity”以使IUnityContainer可用? - Should I add “Prism.Unity 6.2.0” or only “Microsoft.Practices.Unity” in Prism Module references to get IUnityContainer available? 在Prism.Unity中注册依赖于另一个服务的服务实例 - Register an instance of a service in Prism.Unity which relies on another service UWP SerialDevice.FromIdAsync 在 Windows 10 上抛出“找不到元素”(来自 HRESULT 的异常:0x80070490) - UWP SerialDevice.FromIdAsync throws “Element not found” (Exception from HRESULT: 0x80070490) on Windows 10 Prism DelgateComand引发异常 - Prism DelgateComand throws an exception 在UWP中处理未处理的异常 - Windows 10 - Handling unhandled exception in UWP - Windows 10 Unity UWP FolderPicker引发无效的窗口句柄异常 - Unity UWP FolderPicker Throws Invalid Window Handle Exception Win10 UWP、Prism/Unity 和单元测试加上类库,静默失败 - Win10 UWP, Prism/Unity and Unit Tests plus Class Libraries, silent failure 适用于UWP Windows 10应用程序的Google Calendar API - Google Calendar API for UWP Windows 10 Application TimeZoneInfo.FindSystemTimeZoneById()在Unity应用程序中抛出异常 - TimeZoneInfo.FindSystemTimeZoneById() throws exception in a Unity application 在Android中转换现有的Windows 10 UWP应用程序 - Convert Existing Windows 10 UWP Application in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM