DLL项目和GUI项目-Caliburn Micro:模型/视图VM问题

问题描述 投票:0回答:1

我确定这个问题以前应该被问过,但是我无法确切找到我想要的东西;

请考虑以下内容:

- Solution
-- Class Library Project [Caliburn.Micro] Referenced
--- [Models] Folder
---- LogEntryModel.cs
-- WPF GUI Project [Caliburn.Micro] Referenced
--- [ViewModels] Folder
--- [Views] Folder
---- LogEntryView.xaml
---- ShellView.xaml

所以,我有2个项目,一个项目包含模型,一个项目包含ViewModels和Views;这是我的引导程序:

    public class AppBootstrapper : BootstrapperBase
    {
        private CompositionContainer container;

        public AppBootstrapper()
        {
            Initialize();
        }

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

        /// <summary>
        ///     By default, we are configured to use MEF
        /// </summary>
        protected override void Configure()
        {
            var catalog =
                new AggregateCatalog(
                    AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());

            this.container = new CompositionContainer(catalog);

            var batch = new CompositionBatch();

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

            this.container.Compose(batch);
        }

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

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

            if (exports.Any())
            {
                return exports.First();
            }

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

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            var startupTasks =
                GetAllInstances(typeof(StartupTask))
                .Cast<ExportedDelegate>()
                .Select(exportedDelegate => (StartupTask)exportedDelegate.CreateDelegate(typeof(StartupTask)));

            startupTasks.Apply(s => s());

            DisplayRootViewFor<IShell>();
        }

    }

现在,当我尝试使用绑定到列表框的LogEntryModel时,我显示Cannot find view for Project.Models.LogEntryModel.

  • 我想我需要'告诉'Caliburn在我的班级图书馆项目中寻找模型(如何)
  • 我应该在类库中引用Caliburn.Micro吗? (因为这是GUI的东西?)
  • 我的ViewModel应该在哪里,在ClassLibrary或GUI项目中?
c# wpf caliburn.micro
1个回答
0
投票

如果要添加新规则来链接viewModel和视图,则必须使用ViewLocator

一些样品:

        ViewLocator.NameTransformer.AddRule(@"ViewModel", @"ViewX");
        ViewLocator.AddNamespaceMapping("Cockpit.Core.Plugins.Plugins.Properties", "Cockpit.General.Properties.Views");
© www.soinside.com 2019 - 2024. All rights reserved.