WPF Caliburn集成-交互问题

WPF Caliburn集成-交互问题,wpf,caliburn.micro,interaction,caliburn,Wpf,Caliburn.micro,Interaction,Caliburn,我正在开发一个WPF应用程序,我想集成Caliburn Micro。 在主页中,我有一个内容演示器,当应用程序启动时,它会加载一个UserControl(比方说,您可以在下面看到名为MenuView)。但是,当我尝试在MenuView中使用交互处理MouseDown事件时,MenuViewModel中从未调用该方法,因为viewmodel可能未在Caliburn中注册 以下是应用程序结构: MainViewModel: public class MainViewModel : Property

我正在开发一个WPF应用程序,我想集成Caliburn Micro。 在主页中,我有一个内容演示器,当应用程序启动时,它会加载一个UserControl(比方说,您可以在下面看到名为MenuView)。但是,当我尝试在MenuView中使用交互处理MouseDown事件时,MenuViewModel中从未调用该方法,因为viewmodel可能未在Caliburn中注册

以下是应用程序结构:

MainViewModel:

public class MainViewModel : PropertyChangedBase, IShell
{
    public MainViewModel()
    {
        SelectedView = new MenuView();
    }

    private UserControl _selectedView;
    public UserControl SelectedView
    {
        get { return _selectedView; }
        set
        {
            _selectedView = value;
            NotifyOfPropertyChange();
        }
    }
}
public class MenuViewModel : IShell
{
    public void RedirectToSapAction()
    {
        //...
    }
}
主视图:

<Window x:Class="UITablet.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" WindowState="Maximized" WindowStyle="ThreeDBorderWindow">
    <StackPanel VerticalAlignment="Bottom">   
        <ContentPresenter Content="{Binding SelectedView, Mode=TwoWay}" />
    </StackPanel>
</Window>
菜单视图:

<UserControl x:Class="UI.Tablet.Views.MenuView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             xmlns:cal="http://www.caliburnproject.org"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Button x:Name="TestButton" Width="100" Height="20" Content="Click me!">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDown">
                    <cal:ActionMessage MethodName="RedirectToSapAction" >
                    </cal:ActionMessage >
                </i:EventTrigger>

            </i:Interaction.Triggers>
        </Button>

    </Grid>
</UserControl>

最后是AppBootstrapper

using System.IO;
using System.Linq;
using System.Reflection;
using WpfApplication1.ViewModels;
using System;
using System.Collections.Generic;
using Caliburn.Micro;

namespace WpfApplication1 
{
    public class AppBootstrapper : BootstrapperBase 
    {
        SimpleContainer container;

        public AppBootstrapper() 
        {
            Initialize();
        }

        protected override void Configure() 
        {

            ViewLocator.AddSubNamespaceMapping("WpfApplication1.ViewModels", "UITablet.Views");

            container = new SimpleContainer();

            container.Singleton<IWindowManager, WindowManager>();
            container.Singleton<IEventAggregator, EventAggregator>();
            container.PerRequest<IShell, MainViewModel>();
        }

        protected override object GetInstance(Type service, string key) {
            var instance = container.GetInstance(service, key);
            if (instance != null)
                return instance;

            throw new InvalidOperationException("Could not locate any instances.");
        }

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

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

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

        protected override IEnumerable<Assembly> SelectAssemblies()
        {
            var assemblies = new List<Assembly>();
            assemblies.AddRange(base.SelectAssemblies());
            //Load new ViewModels here
            string[] fileEntries = Directory.GetFiles(Directory.GetCurrentDirectory());

            assemblies.AddRange(from fileName in fileEntries where fileName.Contains("UITablet.dll") select Assembly.LoadFile(fileName));

            return assemblies;
        }
    }
}
使用System.IO;
使用System.Linq;
运用系统反思;
使用WpfApplication1.ViewModels;
使用制度;
使用System.Collections.Generic;
使用Caliburn.Micro;
命名空间WpfApplication1
{
公共类AppBootstrapper:BootstrapperBase
{
简单容器;
公共AppBootstrapper()
{
初始化();
}
受保护的覆盖无效配置()
{
ViewLocator.AddSubNamespaceMapping(“WpfApplication1.ViewModels”、“UITablet.Views”);
容器=新的SimpleContainer();
container.Singleton();
container.Singleton();
container.PerRequest();
}
受保护的覆盖对象GetInstance(类型服务,字符串键){
var instance=container.GetInstance(服务,键);
if(实例!=null)
返回实例;
抛出新的InvalidOperationException(“找不到任何实例”);
}
受保护的重写IEnumerable GetAllInstances(类型服务){
返回容器。GetAllInstances(服务);
}
受保护的覆盖空洞堆积(对象实例){
容器。堆积(实例);
}
启动时受保护的覆盖无效(对象发送方,System.Windows.StartupEventArgs e){
DisplayRootViewFor();
}
受保护的重写IEnumerable SelectAssemblys()
{
var assemblies=新列表();
assemblies.AddRange(base.selectAssemblys());
//在此加载新的ViewModels
string[]fileEntries=Directory.GetFiles(Directory.GetCurrentDirectory());
assemblies.AddRange(从fileName.Contains(“UITablet.dll”)所在文件项中的fileName选择Assembly.LoadFile(fileName));
返回组件;
}
}
}
如你所见,视图被放置在一个单独的项目中,为了使Caliburn功能正常,我做了一些技巧。
问题是,这种分离就是问题所在?还是我遗漏了什么?

在主视图模型中,定义类型为
MenuViewModel

private MenuViewModel_selectedView;
public MenuViewModel SelectedView
{
    get { return _selectedView; }
    set
    {
        _selectedView = value;
        NotifyOfPropertyChange();

    }
}
您可以在构造函数中实例化
SelectedView
属性,也可以在必要时实例化

在你看来:

 <ContentPresenter cal:View.Model="{Binding SelectedView, Mode=TwoWay}">

        </ContentPresenter>

我想这篇文章会对你有所帮助:
http://stackoverflow.com/questions/9580325/does-caliburn-micro-play-nicely-with-user-controls