Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a69353d

Browse files
committed
Add ResourceDictionary that can load assemblies
This solves the issue where assemblies needed to be resolved by their partial name.
1 parent f13f5e3 commit a69353d

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

src/GitHub.UI/GitHub.UI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<Compile Include="Converters\CountToVisibilityConverter.cs" />
109109
<Compile Include="Converters\DefaultValueConverter.cs" />
110110
<Compile Include="Converters\StickieListItemConverter.cs" />
111+
<Compile Include="Helpers\LoadingResourceDictionary.cs" />
111112
<Compile Include="Helpers\ScrollViewerUtilities.cs" />
112113
<Compile Include="Resources.Designer.cs">
113114
<AutoGen>True</AutoGen>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.IO;
3+
using System.Windows;
4+
using System.Reflection;
5+
using System.Diagnostics;
6+
using System.Collections.Generic;
7+
8+
namespace GitHub
9+
{
10+
public class LoadingResourceDictionary : ResourceDictionary
11+
{
12+
static Dictionary<string, Assembly> assemblyDicts = new Dictionary<string, Assembly>();
13+
14+
public new Uri Source
15+
{
16+
get { return base.Source; }
17+
set
18+
{
19+
EnsureAssemblyLoaded(value);
20+
base.Source = value;
21+
}
22+
}
23+
24+
void EnsureAssemblyLoaded(Uri value)
25+
{
26+
try
27+
{
28+
var assemblyName = FindAssemblyNameFromPackUri(value);
29+
if (assemblyName == null)
30+
{
31+
Trace.WriteLine("Couldn't find assembly name in: " + value);
32+
return;
33+
}
34+
35+
var baseDir = Path.GetDirectoryName(GetType().Assembly.Location);
36+
var assemblyFile = Path.Combine(baseDir, assemblyName + ".dll");
37+
if (assemblyDicts.ContainsKey(assemblyFile))
38+
{
39+
return;
40+
}
41+
42+
if (!File.Exists(assemblyFile))
43+
{
44+
Trace.WriteLine("Couldn't find assembly at: " + assemblyFile);
45+
return;
46+
}
47+
48+
var assembly = Assembly.LoadFrom(assemblyFile);
49+
assemblyDicts.Add(assemblyFile, assembly);
50+
Trace.WriteLine("Loaded " + assemblyFile + " for " + value);
51+
}
52+
catch(Exception e)
53+
{
54+
Trace.WriteLine(e);
55+
}
56+
}
57+
58+
static string FindAssemblyNameFromPackUri(Uri packUri)
59+
{
60+
var path = packUri.LocalPath;
61+
if(!path.StartsWith("/"))
62+
{
63+
return null;
64+
}
65+
66+
var component = ";component/";
67+
int componentIndex = path.IndexOf(component, 1);
68+
if(componentIndex == -1)
69+
{
70+
return null;
71+
}
72+
73+
return path.Substring(1, componentIndex - 1);
74+
}
75+
}
76+
}

src/GitHub.UI/Helpers/SharedDictionaryManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace GitHub.UI.Helpers
66
{
7-
public class SharedDictionaryManager : ResourceDictionary
7+
public class SharedDictionaryManager : LoadingResourceDictionary
88
{
99
static readonly Dictionary<Uri, ResourceDictionary> resourceDicts = new Dictionary<Uri, ResourceDictionary>();
1010

src/GitHub.VisualStudio.UI/Helpers/SharedDictionaryManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
using System.Windows;
44
using Microsoft.VisualStudio.PlatformUI;
55
using GitHub.VisualStudio.Helpers;
6-
using GitHub.Helpers;
76

87
namespace GitHub.VisualStudio.UI.Helpers
98
{
10-
public class SharedDictionaryManager : ResourceDictionary
9+
public class SharedDictionaryManager : LoadingResourceDictionary
1110
{
1211
public SharedDictionaryManager()
1312
{

src/GitHub.VisualStudio/Helpers/SharedDictionaryManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace GitHub.VisualStudio.Helpers
77
{
8-
public class SharedDictionaryManager : ResourceDictionary
8+
public class SharedDictionaryManager : LoadingResourceDictionary
99
{
1010
static readonly Dictionary<Uri, ResourceDictionary> resourceDicts = new Dictionary<Uri, ResourceDictionary>();
1111

0 commit comments

Comments
 (0)