Browse Source

Load xaml from resources rather than filesystem

pull/81/head
Steven Kirk 11 years ago
parent
commit
b5df5a5610
  1. 78
      Perspex.Xaml.Desktop/InflatableResourceTranslator.cs
  2. 2
      Perspex.Xaml.Desktop/Perspex.Xaml.Desktop.csproj
  3. 2
      Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs
  4. 17
      Perspex.Xaml.Desktop/WindowsResourceProvider.cs
  5. 5
      XamlTestApplication/XamlTestApplication.csproj

78
Perspex.Xaml.Desktop/InflatableResourceTranslator.cs

@ -0,0 +1,78 @@
namespace Perspex.Xaml.Desktop
{
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using OmniXaml.AppServices;
public class InflatableResourceTranslator : IInflatableTranslator
{
public Stream GetStream(Type type)
{
var uri = GetUriFor(type);
var assembly = Assembly.GetEntryAssembly();
var resourceName = assembly.GetName().Name + ".g";
var manager = new ResourceManager(resourceName, assembly);
using (ResourceSet resourceSet = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true))
{
Stream s = (Stream)resourceSet.GetObject(uri.ToString(), true);
if (s == null)
{
throw new IOException(
"The requested resource could not be found: " +
uri.OriginalString);
}
return s;
}
}
private static Uri GetUriFor(Type type)
{
if (type.Namespace != null)
{
var toRemove = type.Assembly.GetName().Name;
var substracted = toRemove.Length < type.Namespace.Length ? type.Namespace.Remove(0, toRemove.Length + 1) : "";
var replace = substracted.Replace('.', Path.PathSeparator);
if (replace != string.Empty)
{
replace = replace + "/";
}
return new Uri(replace + type.Name + ".xaml", UriKind.Relative);
}
return null;
}
public Type GetTypeFor(Uri uri)
{
var withExt = uri.OriginalString;
var lastSlash = withExt.LastIndexOf("/", StringComparison.Ordinal);
var innerNs = withExt.Substring(0, lastSlash);
var fileName = withExt.Substring(lastSlash + 1, withExt.Length - lastSlash - 1);
var className = fileName.Substring(0, fileName.LastIndexOf('.'));
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var type = from assembly in assemblies
let t = assembly.GetType(GetName(assembly, innerNs, className))
where t != null
select t;
return type.First();
}
private static string GetName(Assembly assembly, string innerNs, string className)
{
var ns = assembly.GetName().Name + "." + innerNs;
var fullLocator = ns + "." + className;
return fullLocator;
}
}
}

2
Perspex.Xaml.Desktop/Perspex.Xaml.Desktop.csproj

@ -126,7 +126,7 @@
<Compile Include="PerspexWindow.cs" />
<Compile Include="PerspexXamlLoader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WindowsResourceProvider.cs" />
<Compile Include="InflatableResourceTranslator.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Perspex.Animation\Perspex.Animation.csproj">

2
Perspex.Xaml.Desktop/PerspexInflatableTypeFactory.cs

@ -9,7 +9,7 @@ namespace Perspex.Xaml.Desktop
public class PerspexInflatableTypeFactory : InflatableTypeFactory
{
public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableTranslator(), typeFactory => new PerspexXamlLoader(typeFactory))
public PerspexInflatableTypeFactory() : base(new TypeFactory(), new InflatableResourceTranslator(), typeFactory => new PerspexXamlLoader(typeFactory))
{
Inflatables = new Collection<Type> { typeof(Window), typeof(UserControl) };
}

17
Perspex.Xaml.Desktop/WindowsResourceProvider.cs

@ -1,17 +0,0 @@
namespace Perspex.Xaml.Desktop
{
using System;
using System.IO;
using System.Reflection;
using HighLevel;
public class WindowsResourceProvider : IResourceProvider
{
public Stream GetStream(Uri uri)
{
var absoluteUri = new Uri(Assembly.GetExecutingAssembly().Location, UriKind.Absolute);
var finalUri = new Uri(absoluteUri, uri);
return new FileStream(finalUri.LocalPath, FileMode.Open);
}
}
}

5
XamlTestApplication/XamlTestApplication.csproj

@ -201,10 +201,9 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="Views\MainWindow.xaml">
<Resource Include="Views\MainWindow.xaml">
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</Resource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\SharpDX.2.6.3\build\SharpDX.targets" Condition="Exists('..\packages\SharpDX.2.6.3\build\SharpDX.targets')" />

Loading…
Cancel
Save