2012-10-22 30 views
0

当主页是app.xaml,它是空白的,并且它只运行实际的mainwindow.xaml(通过使用viewmodellocator)时,我创建了一个wpf mvvm项目。 这个项目工作正常,我把它作为一个EXE运行多次。 我创建了一个类库,并构建它。如何将wpf dll附加到不同的项目

现在我有另一个项目,为此我引用了创建的dll。 我想运行wpf页面。我已经找了几天现在正确的解决方案,并看着ResourceDirectory,以及一些在论坛上提出的一样 -

 Assembly a = Assembly.Load(System.IO.File.ReadAllBytes("ExportCheck.dll"));   
     UserControl UserContrl = (UserControl)a.CreateInstance("ExportCheckInstance"); 
     UserContrl.Show(); 

,我尝试过其他的东西为好,不过,我似乎无法到只需运行我的wpf。 我非常感谢您提供的任何帮助,因为我现在很困难。 谢谢。

回答

0

我想你的ExportCheck.dll没有被引用(拷贝本地真)在你的被引用的项目?

对,如果是这样,你将不得不从Uri路径加载DLL。

var asemblyPath = @"C:\MyDevCodeBase\AssetMVVMSample\Asset1MVVMPool\bin\Debug"; 
var assemblyName = "Asset1MVVMPool" 
var myViewModel = "Asset1MVVMPool.List.ViewModels.Asset1ListViewModel, Asset1MVVMPool, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 

var newAsmbly = Assembly.LoadFrom(assemblyPath + @"\" + assemblyName + ".dll"); 
if (!string.IsNullOrEmpty(myViewModel)) 
     { 
      var type = newAsmbly.GetTypes().FirstOrDefault(
       t => t.AssemblyQualifiedName == viewModelFullName); 
      var currentViewModel 
       = Activator.CreateInstance(type) as IBaseViewModel; 
      return currentViewModel; 
     } 

编辑

对于已经提到使用复制本地True时,使用下面的代码汇编...

 var myasmbly = Assembly.GetExecutingAssembly().GetReferencedAssemblies().Where(
      asmbly => asmbly.Name == "Export.dll").FirstOrDefault(); 
     if (!string.IsNullOrEmpty(myViewModel)) 
     { 
      var type = myasmbly.GetTypes().FirstOrDefault(
       t => t.AssemblyQualifiedName == viewModelFullName); 
      var currentViewModel 
       = Activator.CreateInstance(type) as IBaseViewModel; 
      return currentViewModel; 
     } 
+0

您好,感谢您的答复, 它被称为,并且复制本地是真实的... – dusm

+0

请参阅我上面的**编辑**。 –

相关问题