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

Commit

Permalink
For consistency with ResourceDictionary, expose the Source for a Styl…
Browse files Browse the repository at this point in the history
…eSheet

This would allow previewer/designer/reloader to determine the source of a
stylesheet and refresh its content when appropriate.
  • Loading branch information
kzu committed May 3, 2018
1 parent 6e95a6d commit d45801c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Xamarin.Forms.Core/StyleSheets/StyleSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public sealed class StyleSheet : IStyle
{
}

public Uri Source { get; internal set; }

internal IDictionary<Selector, Style> Styles { get; set; } = new Dictionary<Selector, Style>();

public static StyleSheet FromAssemblyResource(Assembly assembly, string resourceId, IXmlLineInfo lineInfo = null)
Expand Down
39 changes: 37 additions & 2 deletions Xamarin.Forms.Xaml.UnitTests/StyleSheet.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using NUnit.Framework;

using Xamarin.Forms.Core.UnitTests;
using NUnit.Framework.Constraints;

namespace Xamarin.Forms.Xaml.UnitTests
{
Expand Down Expand Up @@ -42,6 +43,40 @@ public void StyleSheetsAreApplied(bool useCompiledXaml)
Assert.That(layout.label0.TextColor, Is.EqualTo(Color.Azure));
Assert.That(layout.label0.BackgroundColor, Is.EqualTo(Color.AliceBlue));
}

[TestCase(false), TestCase(true)]
public void StyleSheetSourceIsApplied(bool useCompiledXaml)
{
// Having a custom ResourceProvider forces the LoadFromXaml code path
// for both XamlC and non-XamlC
Xamarin.Forms.Internals.ResourceLoader.ResourceProvider = GetResource;

var layout = new StyleSheet(useCompiledXaml);

Assert.AreEqual("css/foo.css", layout.Resources.StyleSheets[0].Source.OriginalString);
}

string GetResource(AssemblyName name, string path)
{
var assembly = Assembly.Load(name);
var resourceId = assembly
.GetCustomAttributes<XamlResourceIdAttribute>()
.Where(x => x.Path == path)
.Select(x => x.ResourceId)
.FirstOrDefault();

if (!string.IsNullOrEmpty(resourceId))
{
using (var stream = assembly.GetManifestResourceStream(resourceId))
{
if (stream != null)
using (var reader = new StreamReader(stream))
return reader.ReadToEnd();
}
}

return null;
}
}
}
}
4 changes: 3 additions & 1 deletion Xamarin.Forms.Xaml/MarkupExtensions/StyleSheetExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ object IValueProvider.ProvideValue(IServiceProvider serviceProvider)
var rootTargetPath = XamlResourceIdAttribute.GetPathForType(rootObjectType);
var resourcePath = ResourceDictionary.RDSourceTypeConverter.GetResourcePath(Source, rootTargetPath);
var resString = DependencyService.Get<IResourcesLoader>().GetResource(resourcePath, rootObjectType.GetTypeInfo().Assembly, lineInfo);
return StyleSheet.FromString(resString);
var styleSheet = StyleSheet.FromString(resString);
styleSheet.Source = new Uri(resourcePath, UriKind.Relative);
return styleSheet;
}

if (!string.IsNullOrEmpty(Style)) {
Expand Down

0 comments on commit d45801c

Please sign in to comment.