【问题标题】:WPF Caliburn integration - Interaction issueWPF Caliburn 集成 - 交互问题
【发布时间】:2015-07-05 14:12:09
【问题描述】:

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

这是应用程序结构:

主视图模型:

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

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

主视图:

<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>

菜单视图模型:

public class MenuViewModel : IShell
{
    public void RedirectToSapAction()
    {
        //...
    }
}

菜单视图:

<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 正常工作。 问题是,这种分离有什么问题吗?还是我错过了什么?

【问题讨论】:

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

标签: wpf caliburn.micro interaction caliburn


【解决方案1】:

在您的主视图模型中,定义MenuViewModel 类型的属性

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

    }
}

您可以在构造函数中或在必要时实例化 SelectedView 属性。

在你看来:

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

        </ContentPresenter>

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2021-08-05
    • 2023-03-06
    • 2011-11-11
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    相关资源
    最近更新 更多