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

[Reverted] Expose the Source for a StyleSheet #2589

Merged
merged 1 commit into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 41 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,44 @@ 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);

// Reset state to its initial
Xamarin.Forms.Internals.ResourceLoader.ResourceProvider = null;
DesignMode.IsDesignModeEnabled = false;
}

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