Skip to content

Commit

Permalink
fixes #377
Browse files Browse the repository at this point in the history
  • Loading branch information
softlion committed Sep 19, 2020
1 parent 9b643ff commit 9f77551
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Samples/MaterialMvvmSample/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public partial class App : Application
{
public App(INavigationService navigationService)
{
XF.Material.Forms.Material.Init(this);
InitializeComponent();
XF.Material.Forms.Material.Use("Material.Style");

XF.Material.Forms.Material.Init(this, "Material.Style");
XamSvg.Shared.Config.ResourceAssembly = typeof(App).Assembly;

navigationService.SetRootView(ViewNames.LandingView);
Expand Down
40 changes: 28 additions & 12 deletions XF.Material/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ namespace XF.Material.Forms
public class Material
{
private static readonly Lazy<IMaterialUtility> MaterialUtilityInstance = new Lazy<IMaterialUtility>(() => DependencyService.Get<IMaterialUtility>());
private readonly MaterialConfiguration _config;
private MaterialConfiguration _config;
private static ResourceDictionary _res;

private static Material Instance;

internal Material(Application app, MaterialConfiguration materialResource) : this(app)
{
_config = materialResource;
Instance = this;
}

internal Material(Application app, string key) : this(app)
{
_config = GetResource<MaterialConfiguration>(key);
Instance = this;
}

internal Material(Application app)
Expand All @@ -33,6 +37,15 @@ internal Material(Application app)
ColorConfiguration = new MaterialColorConfiguration(),
FontConfiguration = new MaterialFontConfiguration()
};
Instance = this;
}

public static void Use(string key)
{
if (Instance == null)
Init(Application.Current);
// ReSharper disable once PossibleNullReferenceException
Instance._config = GetResource<MaterialConfiguration>(key);
}

/// <summary>
Expand All @@ -49,6 +62,9 @@ internal Material(Application app)
/// <exception cref="ArgumentNullException" />
public static T GetResource<T>(string key)
{
if(_res == null)
throw new Exception("You must call one of the Init() methods in App.xaml.cs before InitializeComponent()");

_res.TryGetValue(key ?? throw new ArgumentNullException(nameof(key)), out var value);

if (value is T resource)
Expand Down Expand Up @@ -76,17 +92,17 @@ public static void Init(Application app, MaterialConfiguration materialResource)
material.MergeMaterialDictionaries();
}

/// <summary>
/// Configure's the current app's resources by merging pre-defined Material resources and creating new resources based on the <see cref="MaterialConfiguration"/>'s properties.
/// </summary>
/// <param name="app">The cross-platform mobile application that is running.</param>
/// <param name="key">The key of the <see cref="MaterialConfiguration"/> object in the current app's resource dictionary.</param>
/// <exception cref="ArgumentNullException" />
public static void Init(Application app, string key)
{
var material = new Material(app ?? throw new ArgumentNullException(nameof(app)), key ?? throw new ArgumentNullException(nameof(key)));
material.MergeMaterialDictionaries();
}
///// <summary>
///// Configure's the current app's resources by merging pre-defined Material resources and creating new resources based on the <see cref="MaterialConfiguration"/>'s properties.
///// </summary>
///// <param name="app">The cross-platform mobile application that is running.</param>
///// <param name="key">The key of the <see cref="MaterialConfiguration"/> object in the current app's resource dictionary.</param>
///// <exception cref="ArgumentNullException" />
//public static void Init(Application app, string key)
//{
// var material = new Material(app ?? throw new ArgumentNullException(nameof(app)), key ?? throw new ArgumentNullException(nameof(key)));
// material.MergeMaterialDictionaries();
//}

/// <summary>
/// Configure's the current app's resources by merging pre-defined Material resources.
Expand Down

0 comments on commit 9f77551

Please sign in to comment.