【问题标题】:System.TypeLoadException: Could not resolve type with token 01000019System.TypeLoadException:无法使用令牌 01000019 解析类型
【发布时间】:2017-02-06 17:51:44
【问题描述】:

我有一个 Xamarin.Forms 解决方案,它在每个项目(Android、iOS 和 Windows 8.1)中都包含一个名为 Plugin.SecureStorage 的库,来自此处: https://github.com/sameerkapps/SecureStorage 我在每个项目中通过 NuGET 安装它。

在 iOS 和 Windows 8.1 中一切正常,问题出在 Android 上。 Android中的项目可以正确构建,但是在启动时我得到了这个:

[...]
Loaded assembly: MonoDroidConstructors [External]
09-27 18:14:49.880 D/Mono    (30329): Assembly Ref addref AppConsume.Droid[0xb8cb0608] -> mscorlib[0xb8c64bc0]: 23
09-27 18:14:49.890 D/Mono    (30329): Assembly Ref addref Xamarin.Forms.Core[0xb8cbca58] -> System.Collections[0xb8cc5980]: 3
09-27 18:14:49.900 D/Mono    (30329): Assembly Ref addref Xamarin.Forms.Core[0xb8cbca58] -> System.Threading[0xb8cd4948]: 3
09-27 18:14:49.930 D/Mono    (30329): Assembly Ref addref AppConsume.Droid[0xb8cb0608] -> Plugin.SecureStorage[0xb8cb43f8]: 2
Unhandled Exception:

System.TypeLoadException: Could not resolve type with token 01000019

这是什么意思?对我来说有点神秘。我该如何解决这个问题?

当然,作为要求,我添加了这一行...

SecureStorageImplementation.StoragePassword = "mypass";

在Android项目的MainActivity.cs中...

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Plugin.SecureStorage;

namespace MyApp.Droid
{
    [Activity(Label = "MyApp", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SecureStorageImplementation.StoragePassword = "mypass";
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }
}

我还发现更改行位置会在异常中引发不同的“令牌类型”。

更新:我刚刚发现应用程序在发布模式下编译时运行成功。但是,不在调试模式下工作是我想解决的问题,我认为这超出了这个问题的范围。

【问题讨论】:

  • 在执行非常糟糕的代码合并之后,除了令牌 0100002a 之外,我也收到了这个错误,但是当我注释掉命名空间范围和/或 App 类 XAMLC 注释时,错误就会消失。我最终重新开始合并,问题就消失了。

标签: c# android xamarin.forms


【解决方案1】:

对我来说同样的错误。

问题:

我的解决方案中有不同版本的 Xamarin.Forms 包。

解决方案:

在您的 Core、Droid 和 IOS 项目中更改 Xamarin.Forms 版本。确保所有版本都相同。

我希望这行得通。

【讨论】:

  • 这为我解决了这个问题,尽管它在另一个跨平台包中不匹配。谢谢!
  • 投票中,我在所有以下软件包中的 Xamarin.FormsXamarin.Forms.Maps 软件包版本不匹配:便携式、iOS 和 Android 项目。
  • 哇,哇……不知何故,在这些共享项目文件中存在一个链接,由于不存在真正的依赖关系,导致整个事情中断……仍然准确,你得到了我的投票!跨度>
  • 我开始认为 Xamarin 不值得为 Android 付出努力。在 Android Studio 中编写您的应用程序比解决晦涩的 Visual Studio 错误更快。此外,生成的应用程序占用的空间更少,速度更快。从长远来看,它只是一个临时解决方案。
  • 问题今天仍然存在,但在 iOS 上。 Xamarin.Forms 在所有项目中都有最新版本。仅在某些开发机器和 iOS 设备上发生,我不知道为什么。
【解决方案2】:

在 Visual Studio 2015 中,以发布模式运行项目没有问题(如果您不更改默认设置)

调试模式下,通过在 Project Properties -> Android Options -> Linker 中选择 Linking: "SDK Assemblies Only",将毫无问题地运行项目。

或者只是简单地保留这些调试设置并在 Android 项目中添加一个名为“SecureStorageLinkerOverride.cs”的文件

using System;
using Plugin.SecureStorage;

namespace MyApp.Droid
{
    public static class LinkerPreserve
    {
        static LinkerPreserve()
        {
            throw new Exception(typeof(SecureStorageImplementation).FullName);
        }
    }

    public class PreserveAttribute : Attribute
    {
    }

}

【讨论】:

【解决方案3】:

这里是完整的解决方案

  1. 安装nuget包https://www.nuget.org/packages/sameerIOTApps.Plugin.SecureStorage/
  2. Droid 项目中创建 SecureStorageLinkerOverride.cs

    使用系统; 使用 Plugin.SecureStorage; 命名空间 MyApp.Droid { 公共静态类 LinkerPreserve { 静态链接器保留() { 抛出新异常(typeof(SecureStorageImplementation).FullName); } }
        public class PreserveAttribute : Attribute
       {
       }
    
    }
  3. 右击Droid Project -> Property -> Android Option-> Linker -> "SDK Assemblies Only"

现在运行您的项目。对于其他标记为已回答的问题,请在下方评论。

【讨论】:

  • 旧版本的xamarin需要在新版本中复制错误
【解决方案4】:

确保 Xamarin.Forms 在 Xamarin.Android、Xamarin.iOS... 项目中是最新的

【讨论】:

    猜你喜欢
    • 2020-09-07
    • 2019-02-19
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    相关资源
    最近更新 更多