Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将wpf dll附加到其他项目_Wpf_Xaml_Dll_Class Library - Fatal编程技术网

如何将wpf dll附加到其他项目

如何将wpf dll附加到其他项目,wpf,xaml,dll,class-library,Wpf,Xaml,Dll,Class Library,我已经创建了一个wpf mvvm项目,当主页为app.xaml(为空)时,它只运行实际的mainwindow.xaml(通过使用viewmodellocator)。 这个项目运行得很好,我以exe的形式多次运行它。 我创建了一个类库,并构建了它 现在我有了另一个项目,我引用了为它创建的dll。 我只想运行wpf页面。几天来,我一直在寻找正确的解决方案,并查看了ResourceDirectory,以及论坛上介绍的一些内容,如- Assembly a = Assembly.Load(

我已经创建了一个wpf mvvm项目,当主页为app.xaml(为空)时,它只运行实际的mainwindow.xaml(通过使用viewmodellocator)。 这个项目运行得很好,我以exe的形式多次运行它。 我创建了一个类库,并构建了它

现在我有了另一个项目,我引用了为它创建的dll。 我只想运行wpf页面。几天来,我一直在寻找正确的解决方案,并查看了ResourceDirectory,以及论坛上介绍的一些内容,如-

        Assembly a = Assembly.Load(System.IO.File.ReadAllBytes("ExportCheck.dll"));           
        UserControl UserContrl = (UserControl)a.CreateInstance("ExportCheckInstance");
        UserContrl.Show();
我也尝试了其他的东西,但是,我似乎不能仅仅运行我的wpf。 我非常感谢你能提供的任何帮助,因为我现在太困了。
谢谢

我猜您的
ExportCheck.dll
在您的引用项目中没有被引用(复制本地True)

好的,如果是这样,您必须从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;
        }