我需要帮助动态加载wpf中的东西。
程序集位于C:\..\...
等之下,我需要在xaml文件中解决它。
看看这个:pack:// application:,,, C:\..\..\myassembly.dll
; component \ Themes \ Generic.xaml
我想从位于C:\..\..\myassembly.dll
的程序集中获取generic.xaml
有什么办法在wpf中做到这一点吗?
答案 0 :(得分:0)
引用外部ResourceDictionary(XAML文件):
getEnabledAccessibilityServiceList
引用外部ResourceDictionary(DLL):
<Application.Resources>
<ResourceDictionary Source="MyResources.xaml" />
</Application.Resources>
您可以做的另一件事是使用MEF管理的可扩展性框架动态导入ResourceDictionary。
在每个包含资源的程序集中,您可以通过后面的代码和注释来导出ResourceDictionary对象:
<Application.Resources>
<ResourceDictionary Source="/MyExternalAssembly;component/MyResources.xaml"/>
</Application.Resources>
现在您需要一个组件(您的主应用程序)来解析导入。
[Export(typeof(ResourceDictionary))]
public partial class Resources : ResourceDictionary
{
public Resources()
{
InitializeComponent();
}
}
答案 1 :(得分:0)
您可以在应用程序启动时使用以下代码自定义程序集的加载...
@inject(Session)
仅当系统无法解析应用程序引用的程序集的位置时,才会触发AssemblyResolve事件。
答案 2 :(得分:0)
我遇到了与我的项目完全相同的问题。这就是我解决它的方法:
这是基本的解析器类:
public sealed class AssemblyResolveHandler
{
const string LIBRARY_PATH = @"C:\...\...\";
public static Assembly ResolveEventHandler(object sender, System.ResolveEventArgs e)
{
Assembly a = null;
string assemblyName = e.Name.Substring(0, e.Name.IndexOf(","));
//Look for the DLL assembly
a = FindAssembly(LIBRARY_PATH, assemblyName + ".dll");
if (a == null)
{ //If no DLL assembly was found, look for an EXE assembly.
a = FindAssembly(eofSharedFolder, assemblyName + ".exe");
}
return a;
}
public static Assembly FindAssembly(string appFolder, string fileName, out string logReturn)
{
Assembly rtnAssembly = null;
string targetFolder = appFolder;
//some dll's are hidden in x64 folders or x86 folders
if (Environment.Is64BitOperatingSystem)
targetFolder += "\x64";
else
targetFolder += "\x86";
string fullCurrentLocation = System.IO.Directory.GetCurrentDirectory() + @"\" + fileName;
string fullAppLocation = Path.GetFullPath(appFolder) + @"\" + fileName;
string fullAltLocation = Path.GetFullPath(targetFolder) + @"\" + fileName;
//make sure it's not in the current folder
if (File.Exists(fullCurrentLocation))
{
rtnAssembly = Assembly.LoadFrom(fullCurrentLocation);
}
else if (File.Exists(fullAppLocation))
{
rtnAssembly = Assembly.LoadFrom(fullAppLocation);
}
else if (File.Exists(fullAltLocation))
{
rtnAssembly = Assembly.LoadFrom(fullAltLocation);
}
return rtnAssembly;
}
}
然后通过进入App.xaml.cs并添加构造函数覆盖来在WPF应用程序中调用它:
public App():base()
{
//*****This is used to tell the application how to load DLLs
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolveHandler.ResolveEventHandler);
}
在构造函数中进行此调用是使其工作的唯一方法。 WPF生成在“app.OnStartup()”之前调用的函数“app.main()”,并且将始终破坏此程序集解析代码,但在生成的“main”之前将调用构造函数