如何解决 mscorlib 和 Nuget 包之间的 ApplicationException 冲突

How to resolve ApplicationException conflict between mscorlib and Nuget package

问题

我正在尝试向我的 Xamarin.Forms Android 和 iOS 应用程序的用户显示 pdf 文件。我正在尝试使用 Nuget 包 Syncfusion.Xamarin.SfPdfViewer.

但是,安装软件包并重新编译会导致以下错误:

Error CS0433 The type 'ApplicationException' exists in both 'Syncfusion.Compression.Portable, Version=16.1451.0.37, Culture=neutral, PublicKeyToken=null' and 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'

尝试的步骤

我已阅读并留意 Nuget 包上的说明:

Note: This package needs to be installed in all Xamarin.Forms projects (PCL, Android, iOS and UWP).

我在网上搜索过类似案例,例如:

规格

我正在使用:


感谢您的帮助。

找到解决方法。由于 good practice 是从 Exception 派生出有意义的异常,因此我创建了一个并使用它来代替我使用 ApplicationException 的地方。这消除了歧义和解决方案,然后编译。

public class ClaimWriterException : Exception
{
    public ClaimWriterException()
    {
    }

    public ClaimWriterException(string message)
        : base(message)
    {
    }

    public ClaimWriterException(string message, Exception inner)
        : base(message, inner)
    {
    }
}

出现此问题是因为 Syncfusion.Compression.Portable、dll(包含在 Syncfusion.Xamarin.SfPdfViewer 包中)和 mscorlib.dll 包含相同的完全限定类型 System.ApplicationException。这个歧义问题最近已在我们的项目中修复,修复将包含在我们将于 2018 年 7 月底推出的下一个版本中。

解决方法: 但是,我们已经找到了从我们这边解决此问题的解决方法。如本 link 中所述,我们要求您为 Syncfusion.Compression.Portable 程序集设置 extern 别名,并在您的应用程序中使用所需的 class。

解决此问题的步骤: • 在您的 csproj 文件中添加以下代码片段,其中为 Syncfusion.Compression.Portable 程序集设置了别名“compression”。

<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'Syncfusion.Compression.Portable'">
        <Aliases>compression</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>

• 在ApplicationException used classes中,添加extern别名压缩;在文件顶部,并在 class 文件中使用所需的 ApplicationException。

extern alias compression;
using System;
using Xamarin.Forms;
namespace App1
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
            {
                InitializeComponent();
                try {}          

                // To use the netstandard dll ApplicationException
                catch(global::System.ApplicationException ee1) {}

                // To use the compression dll ApplicationException
                catch (compression::System.ApplicationException ee2)   {}
             }
      }
}

下面我们分享了一个简单的例子link来演示修改,供大家参考。

示例 Link: http://www.syncfusion.com/downloads/support/directtrac/general/ze/App1243042706.zip

我为 Syncfusion 工作。