我正在研究WPF应用程序,我想整合Caliburn Micro。 在主页面中,我有一个内容展示器,它在应用程序启动时加载了UserControl(比如说,你可以看到,名称为MenuView,如下所示)。但是当我尝试使用MenuView中的交互处理MouseDown事件时,从不在MenuViewModel中调用该方法,因为可能视图模型未在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();
}
}
}
的MainView:
<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>
MenuViewModel:
public class MenuViewModel : IShell
{
public void RedirectToSapAction()
{
//...
}
}
MenuView:
<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;
}
}
}
正如您所看到的,视图被放置在一个单独的项目中,我已经做了一些技巧,以使Caliburn功能化。 问题是,这种分离是个问题吗?或者我错过了什么?
答案 0 :(得分:2)
在主视图模型中,定义类型为MenuViewModel
private MenuViewModel_selectedView;
public MenuViewModel SelectedView
{
get { return _selectedView; }
set
{
_selectedView = value;
NotifyOfPropertyChange();
}
}
您可以在构造函数中或在必要时实例化SelectedView
属性。
在您看来:
<ContentPresenter cal:View.Model="{Binding SelectedView, Mode=TwoWay}">
</ContentPresenter>