0

i've created a wpf mvvm project, when the main page is app.xaml, which is blank, and it only runs the actual mainwindow.xaml (by using viewmodellocator). this project works fine, i've run it multiply times as an exe. i've created a class library, and built it.

Now i have another project, which for it i referenced the dll created. i would like just run the wpf page. i've been looking for few days now for the right solution, and looked into ResourceDirectory, and something presented in the forum like -

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

and i tried other stuff as well, however, i can't seem to just run my wpf. i would much appreciated any help you can offer, since i'm so stuck at the moment. thanks.

dusm
  • 1,207
  • 4
  • 18
  • 24

1 Answers1

0

I guess your ExportCheck.dll is notn been referred (Copy Local True) in your referred project?

Right, if so, you would have to load the DLL from Uri path.

 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;
        }

EDIT

For already referred assembly using Copy Local True, use following code...

      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;
        }
WPF-it
  • 19,625
  • 8
  • 55
  • 71