Caliburn Micro和Modern UI内容加载器

时间:2015-04-28 08:34:42

标签: c# wpf caliburn.micro modern-ui

我尝试将WPF应用程序中的Caliburn Micro与现代UI结合使用。我找到一些使用caliburn内容加载器的解决方案,但它根本不起作用,这里是代码:

public class CaliburnContentLoader : DefaultContentLoader
{
   protected override object LoadContent(Uri uri)
   {
      var content = base.LoadContent(uri);
      if (content == null)
         return content;

      var vm = Caliburn.Micro.ViewModelLocator.LocateForView(content);
      if (vm == null)
         return content;

      if (content is DependencyObject)
      {
         Caliburn.Micro.ViewModelBinder.Bind(vm, content as DependencyObject, null);
      }
      return content;
   }
}

对于任何视图模型,LocateForView方法每次返回null。我认为这里的问题是IoC容器,由于某些原因我的CaliburnContentLoader中是空的。

总体来说,这是我的App XALM:

<Application x:Class="Astrea.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Astrea">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:Bootstrapper x:Key="bootstrapper" />
                    <local:CaliburnContentLoader x:Key="CaliburnContentLoader" />
                </ResourceDictionary>
                <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
                <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Dark.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

和MainWindow xaml

<mui:ModernWindow x:Class="Astrea.Views.MainWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mui="http://firstfloorsoftware.com/ModernUI"
        Title="mui" IsTitleVisible="True"         
        ContentLoader="{StaticResource CaliburnContentLoader}"
        ContentSource="/Views/ChildView.xaml">

    <mui:ModernWindow.MenuLinkGroups>
        <mui:LinkGroup DisplayName="nodes explorer">
            <mui:LinkGroup.Links>
                <mui:Link DisplayName="home" Source="/Views/ChildView.xaml" />
            </mui:LinkGroup.Links>
        </mui:LinkGroup>

我的引导程序可能有问题吗?这是代码:

namespace Astrea
{
    class Bootstrapper : BootstrapperBase
    {
        private CompositionContainer container;

        public Bootstrapper()
        {
            Initialize();
        }

        protected override void Configure()
        {
            // Add New ViewLocator Rule
            ViewLocator.NameTransformer.AddRule(
            @"(?<nsbefore>([A-Za-z_]\w*\.)*)?(?<nsvm>ViewModels\.)(?<nsafter>([A-Za-z_]\w*\.)*)(?<basename>[A-Za-z_]\w*)(?<suffix>ViewModel$)",
            @"${nsbefore}Views.${nsafter}${basename}View",
            @"(([A-Za-z_]\w*\.)*)?ViewModels\.([A-Za-z_]\w*\.)*[A-Za-z_]\w*ViewModel$"
            );

            container = new CompositionContainer(
                    new AggregateCatalog(
                    new AssemblyCatalog(typeof(IShellViewModel).Assembly),
                    AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>().FirstOrDefault()
                )
            );

            var batch = new CompositionBatch();

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

            container.Compose(batch);
        }

        public T GetInstance<T>()
        {
            string contract = AttributedModelServices.GetContractName(typeof(T));

            var sexports = container.GetExportedValues<object>(contract);
            if (sexports.Count() > 0)
                return sexports.OfType<T>().First();

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

        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;

            var exports = container.GetExportedValues<object>(contract);
            return exports.FirstOrDefault();
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            //return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
            var ret = Enumerable.Empty<object>();

            string contract = AttributedModelServices.GetContractName(serviceType);
            return container.GetExportedValues<object>(contract);
        }

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

        protected override IEnumerable<Assembly> SelectAssemblies()
        {
            return new[] {
            Assembly.GetExecutingAssembly()
        };
        }

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

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。这里的问题是MEF容器,它不能与内容加载器一起工作,或者只是我不知道如何强制它使用它。

我尝试使用随Caliburn Micro提供的Simple IoC容器而不是MEF容器,它就像一种伤害。

这是我的新引导程序:

public class HelloBootstrapper : BootstrapperBase
{
    private SimpleContainer container;

    public HelloBootstrapper()
    {
        Initialize();
    }

    protected override void Configure()
    {
        container = new SimpleContainer();
        container.Singleton<IWindowManager, WindowManager>();
        container.Singleton<IEventAggregator, EventAggregator>();
        container.PerRequest<ShellViewModel>();
        container.PerRequest<ModernWindowViewModel>();
    }

    protected override IEnumerable<Assembly> SelectAssemblies()
    {
        return new[] {
        Assembly.GetExecutingAssembly()
    };
    }

    protected override object GetInstance(Type serviceType, string key)
    {
        return container.GetInstance(serviceType, key);
    }

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

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

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

我不仅知道如何将所有View Models添加到容器中。也许有人可以提供帮助并修改此代码部分。

答案 1 :(得分:0)

我使用此代码添加 ViewModel:

 foreach(var assembly in SelectAssemblies())
   {
   assembly.GetTypes()
     .Where(type => type.IsClass)
     .Where(type => type.Name.EndsWith("ViewModel"))
     .ToList()
     .ForEach(viewModelType => _container.RegisterPerRequest(viewModelType, viewModelType.ToString(), viewModelType));
   }

该代码与 Tim Corey 为 TimcoRetail 课程创建的代码略有修改。我沿着所有的程序集骑自行车。对于每个程序集,您需要获取所有类型,然后应用两个过滤器。一个只包含类,然后只包含名称以“ViewModel”结尾的类,然后这些类都作为 PerRequest 添加到容器中。在 SelectAssemblies 中您可以添加其他程序集,例如库。