Skip to content

Instantly share code, notes, and snippets.

@cryophobia
Created April 20, 2017 11:26
Show Gist options
  • Save cryophobia/ace5ae71a5948594f6d7901525a67c14 to your computer and use it in GitHub Desktop.
Save cryophobia/ace5ae71a5948594f6d7901525a67c14 to your computer and use it in GitHub Desktop.
object_setClass - Type ObjCRuntime.Class which is passed to unmanaged code must have a StructLayout attribute.
using System;
using System.Runtime.InteropServices;
using AVKit;
using Foundation;
using ObjCRuntime;
namespace Xamarin_ios_language_manager
{
public class ExtendedBundle : NSBundle
{
static NSString bundleKey = new NSString("0");
public override string LocalizedString(string key, string value, string table)
{
NSBundle bundle = this.GetProperty<ExtendedBundle>(bundleKey);
if (bundle != null)
{
return bundle.LocalizedString(key, value, table);
}
else
{
return base.LocalizedString(key, value, table);
}
}
public static void SetLanguage(string currentLanguage)
{
try
{
//static dispatch_once_t onceToken;
//dispatch_once(&onceToken, ^{
NSBundleAssociatedObjectExtension.object_setClass(MainBundle.Handle, new Class (typeof(ExtendedBundle)).Handle);
//});
var bundle = FromPath(MainBundle.PathForResource("de", "lproj"));
MainBundle.SetProperty(bundleKey, bundle, NSBundleAssociatedObjectExtension.AssociationPolicy.RetainNonAtomic);
}
catch (Exception ex)
{
throw (new Exception(ex.Message));
}
}
}
public static class NSBundleAssociatedObjectExtension
{
public enum AssociationPolicy
{
Assign = 0,
RetainNonAtomic = 1,
CopyNonAtomic = 3,
Retain = 01401,
Copy = 01403,
}
[DllImport("/usr/lib/libobjc.dylib")]
public static extern Class object_getClass(IntPtr obj);
[DllImport("/usr/lib/libobjc.dylib")]
public static extern Class object_setClass(IntPtr obj, IntPtr cls);
[DllImport("/usr/lib/libobjc.dylib")]
static extern void objc_setAssociatedObject(IntPtr pointer, IntPtr key, IntPtr value, AssociationPolicy policy);
[DllImport("/usr/lib/libobjc.dylib")]
static extern IntPtr objc_getAssociatedObject(IntPtr pointer, IntPtr key);
public static T GetProperty<T>(this NSBundle bundle, NSString propertyKey) where T : NSObject
{
var pointer = objc_getAssociatedObject(bundle.Handle, propertyKey.Handle);
return Runtime.GetNSObject<T>(pointer);
}
public static void SetProperty<T>(this NSBundle bundle, NSString propertyKey, T value, AssociationPolicy policy) where T : NSObject
{
objc_setAssociatedObject(bundle.Handle, propertyKey.Handle, value.Handle, policy);
}
}
}
@GitHub-Ram
Copy link

How did you fix this error?

@GitHub-Ram
Copy link

I am too getting this problem: Type ObjCRuntime.Class which is passed to unmanaged code must have a StructLayout attribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment