System.TypeLoadException: Could not resolve type w

2020-02-06 05:43发布

I have a Xamarin.Forms solution which contains in each project (Android, iOS and Windows 8.1) a lib called Plugin.SecureStorage from here: https://github.com/sameerkapps/SecureStorage I installed it via NuGET in each project.

Everything works fine in iOS and Windows 8.1, the problem is in Android. The project in Android builds correctly, however at startup I get this:

[...]
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

What it does mean? is a bit cryptic to me. How can I resolve this problem?

Of course, as a requirement, I added this line...

SecureStorageImplementation.StoragePassword = "mypass";

in the MainActivity.cs of the Android project...

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());
        }
    }
}

I also found that changing the line position throws different 'token types' in the exception.

UPDATE: I just found the app runs sucessfully when compiling in Release Mode. However, not working in Debug mode is a problem that I would like to fix I don't think that is out of scope of this question.

4条回答
够拽才男人
2楼-- · 2020-02-06 05:57

In Visual Studio 2015, running the project in Release Mode have no issues (if you don't change the default settings)

In Debug mode by selecting Linking: "SDK Assemblies Only" in Project Properties -> Android Options -> Linker, will run the project without problems.

Or just simply leave those Debug settings and add a file called "SecureStorageLinkerOverride.cs" in the Android project:

using System;
using Plugin.SecureStorage;

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

    public class PreserveAttribute : Attribute
    {
    }

}
查看更多
ら.Afraid
3楼-- · 2020-02-06 05:59

Same error for me.

Problem:

I had different versions of the Xamarin.Forms package in my solution.

Solution:

Change in your Core, Droid and IOS project the Xamarin.Forms versions. Make sure that all versions are the same.

I hope this works.

查看更多
▲ chillily
4楼-- · 2020-02-06 06:04

Here is the complete solution

  1. Install nuget package https://www.nuget.org/packages/sameerIOTApps.Plugin.SecureStorage/
  2. Create SecureStorageLinkerOverride.cs in Droid project

    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. Right click on Droid Project -> Property -> Android Option-> Linker -> "SDK Assemblies Only"

Now run your project. Comment bellow for any issues else marked it answer.

查看更多
我命由我不由天
5楼-- · 2020-02-06 06:09

Make sure that Xamarin.Forms are up to date in the Xamarin.Android, Xamarin.iOS... projects

查看更多
登录 后发表回答