简体   繁体   English

WPF-MVVM多项目结构

[英]WPF - MVVM multiple projects structure

I always developed application in MVVM in one project and separate Model, View, ViewModel wth folders. 我一直在MVVM中的一个项目中开发应用程序,并在单独的Model,View和ViewModel文件夹中进行开发。 Looking in different questions and different books I learnt that separate them in different projects will be better for different reasons so I was trying to develop a simple project to see how it works. 通过查看不同的问题和不同的书籍,我了解到由于不同的原因而将它们放在不同的项目中会更好,因此我试图开发一个简单的项目以了解其工作原理。

Firstly I created a single project with different folders, and i start the application and it works good. 首先,我创建了一个具有不同文件夹的项目,然后启动了该应用程序,它运行良好。 This how is the structured the "single project" I have omitted the Model folder. 这就是我省略了Model文件夹的“单个项目”的结构化方式。

在此处输入图片说明

After I created multiple project composed of these 3 projects: 在创建由以下3个项目组成的多个项目后:

-MVVM -MVVM

-MVVM.Views -MVVM。视图

-MVVM.ViewModels -MVVM.ViewModels

This how i structured the "multiple project" 这就是我构造“多个项目”的方式

在此处输入图片说明

I set the MVVM.Views,MVVM.ViewModels output type to Dll and not to .exe, i added the projects(MVVM.Views,MVVM.ViewModels) references in MVVM. 我将MVVM.Views,MVVM.ViewModels输出类型设置为Dll而不是.exe,我在MVVM中添加了项目(MVVM.Views,MVVM.ViewModels)引用。

But when i lunch the application i get an error 但是当我午餐申请时,我得到了一个错误

An unhandled exception of type 'System.Exception' occurred in WindowsBase.dll Informations: Could not locate any instances of contract MVVM.ViewModels.IShell. WindowsBase.dll中出现未处理的'System.Exception'类型的异常信息:无法找到合同MVVM.ViewModels.IShell的任何实例。

I'm using also Caliburn.Micro 2.0.2 for bootstrapp and this is the MefBootstrapper: 我也将Caliburn.Micro 2.0.2用于bootstrapp,这是MefBootstrapper:

public class MefBootstrapper : BootstrapperBase
{
    private CompositionContainer container;

    public MefBootstrapper()
    {
        Initialize();
    }

    protected override void Configure()
    {
        var catalog = new AggregateCatalog(
                AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
                );

        container = new CompositionContainer(catalog);

        var batch = new CompositionBatch();

        batch.AddExportedValue<IWindowManager>(new WindowManager());
        batch.AddExportedValue<IEventAggregator>(new EventAggregator());
        batch.AddExportedValue(container);

        container.Compose(batch);


        }

        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            var exports = container.GetExportedValues<object>(contract);

            if (exports.Count() > 0)
                return exports.First();

            throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
        }

        protected override void BuildUp(object instance)
        {
            container.SatisfyImportsOnce(instance);
        }

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor<IShell>();
        }
    }

This is ViewModel code : 这是ViewModel代码

namespace MVVM.ViewModels
{
    public interface IShell { }

    [Export(typeof(IShell))]
    public class MainViewModel : PropertyChangedBase, IShell
    {

    }
}

I want to understand where i wrong? 我想了解我错了吗? I missed one step? 我错过了一步? Thanks for support 感谢你的支持

I post the answer just in case someone will have my same problems. 我发布答案,以防万一有人遇到同样的问题。 After reading a lot of posts and different stuff I've found the error. 阅读了大量文章和其他内容后,我发现了错误。 I forgot to load the assemblies into the application. 我忘了将程序集加载到应用程序中。 And this is really easy just change the Configure() void here the code: 这真的很容易,只需在以下代码中更改Configure()

protected override void Configure()
{
    string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "");
    if (!Directory.Exists(pluginPath))
        Directory.CreateDirectory(pluginPath);

    var fi = new DirectoryInfo(pluginPath).GetFiles("*.dll");
    AssemblySource.Instance.AddRange(fi.Select(fileInfo => Assembly.LoadFrom(fileInfo.FullName)));

    var catalog = new AggregateCatalog(
            AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
            );

    container = new CompositionContainer(catalog);

    var batch = new CompositionBatch();

    batch.AddExportedValue<IWindowManager>(new WindowManager());
    batch.AddExportedValue<IEventAggregator>(new EventAggregator());
    batch.AddExportedValue(container);

    container.Compose(batch);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM