Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix getting packagename and pass context into RM
Browse files Browse the repository at this point in the history
- fixes #6327
  • Loading branch information
PureWeen committed Jun 5, 2019
1 parent 1db7a74 commit 4430ce3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Xamarin.Forms.Platform.Android/AppCompat/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ internal class Platform : BindableObject, IPlatformLayout, INavigation, IDisposa
bool _navAnimationInProgress;
NavigationModel _navModel = new NavigationModel();
Page _pendingRootChange = null;
internal static string PackageName { get; private set; }
internal static string GetPackageName() => PackageName ?? Android.Platform.PackageName;

public Platform(Context context)
{
_context = context;

PackageName = context?.PackageName;
_renderer = new PlatformRenderer(context, this);

FormsAppCompatActivity.BackPressed += HandleBackPressed;
Expand Down
2 changes: 2 additions & 0 deletions Xamarin.Forms.Platform.Android/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Platform : BindableObject, INavigation, IDisposable, IPlatformLayou
{

internal static string PackageName { get; private set; }
internal static string GetPackageName() => PackageName ?? AppCompat.Platform.PackageName;

internal const string CloseContextActionsSignalName = "Xamarin.CloseContextActions";

internal static readonly BindableProperty RendererProperty = BindableProperty.CreateAttached("Renderer", typeof(IVisualElementRenderer), typeof(Platform), default(IVisualElementRenderer),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static bool DecodeSynchronously {
if (File.Exists (file))
bitmap = !DecodeSynchronously ? (await BitmapFactory.DecodeFileAsync (file).ConfigureAwait (false)) : BitmapFactory.DecodeFile (file);
else
bitmap = !DecodeSynchronously ? (await context.Resources.GetBitmapAsync (file).ConfigureAwait (false)) : context.Resources.GetBitmap (file);
bitmap = !DecodeSynchronously ? (await context.Resources.GetBitmapAsync (file, context).ConfigureAwait (false)) : context.Resources.GetBitmap (file, context);

if (bitmap == null)
{
Expand Down
24 changes: 22 additions & 2 deletions Xamarin.Forms.Platform.Android/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,21 @@ public static Bitmap GetBitmap(this Resources resource, string name)
return BitmapFactory.DecodeResource(resource, IdFromTitle(name, DrawableClass, _drawableDefType, resource));
}

public static Bitmap GetBitmap(this Resources resource, string name, Context context)
{
return BitmapFactory.DecodeResource(resource, IdFromTitle(name, DrawableClass, _drawableDefType, resource, context.PackageName));
}

public static Task<Bitmap> GetBitmapAsync(this Resources resource, string name)
{
return BitmapFactory.DecodeResourceAsync(resource, IdFromTitle(name, DrawableClass, _drawableDefType, resource));
}

public static Task<Bitmap> GetBitmapAsync(this Resources resource, string name, Context context)
{
return BitmapFactory.DecodeResourceAsync(resource, IdFromTitle(name, DrawableClass, _drawableDefType, resource, context.PackageName));
}

[Obsolete("GetDrawable(this Resources, string) is obsolete as of version 2.5. "
+ "Please use GetDrawable(this Context, string) instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down Expand Up @@ -310,11 +320,21 @@ public static int GetLayoutByName(string name)
return IdFromTitle(name, LayoutClass);
}

public static int GetLayout(this Context context, string name)
{
return IdFromTitle(name, LayoutClass, "layout", context);
}

public static int GetStyleByName(string name)
{
return IdFromTitle(name, StyleClass);
}

public static int GetStyle(this Context context, string name)
{
return IdFromTitle(name, StyleClass, "style", context);
}

public static void Init(Assembly masterAssembly)
{
DrawableClass = masterAssembly.GetTypes().FirstOrDefault(x => x.Name == "Drawable" || x.Name == "Resource_Drawable");
Expand All @@ -335,7 +355,7 @@ static int IdFromTitle(string title, Type type)

static int IdFromTitle(string title, Type resourceType, string defType, Resources resource)
{
return IdFromTitle(title, resourceType, defType, resource, Platform.PackageName);
return IdFromTitle(title, resourceType, defType, resource, AppCompat.Platform.GetPackageName());
}

static int IdFromTitle(string title, Type resourceType, string defType, Context context)
Expand All @@ -359,7 +379,7 @@ static int IdFromTitle(string title, Type resourceType, string defType, Resource
if (packageName != null)
{
id = resource.GetIdentifier(name, defType, packageName);

if (id > 0)
return id;
}
Expand Down

0 comments on commit 4430ce3

Please sign in to comment.