简体   繁体   English

如何在Metro中将WindowsCommands与Caliburn.Micro绑定?

[英]How to bind WindowsCommands with Caliburn.Micro in Metro?

I just added the MahApps.Metro WPF UI package to an existing WPF application, for which I use Caliburn.Micro . 我刚刚将MahApps.Metro WPF UI软件包添加到了我使用Caliburn.Micro的现有WPF应用程序中。 In my MainView I had a menu with buttons, which switched the content of the main area of the UI, as follows: MainView我有一个带有按钮的菜单,该菜单如下切换UI主要区域的内容:

<DockPanel>
     <Button DockPanel.Dock="Left" Style="{StaticResource MenuButton}" Name="ShowHomeVM">
</DockPanel>

and in the MainViewModel , I have the following method bound by naming convention, MainViewModel ,我有以下受命名约定约束的方法,

public void ShowHomeVM() {
    CentralVM = HomeVM;
}

and the CentralVM view model instance is bound to the content of the window. 并且CentralVM视图模型实例绑定到窗口的内容。 Now, with Metro I'd like to move my menu to the windows title bar as follows 现在,使用Metro,我想将菜单移到Windows标题栏,如下所示

<Controls:MetroWindow.LeftWindowCommands>
    <Controls:WindowCommands>
        <Button Name="ShowHomeVM" Content="Home"/>
    </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>

and that works, except Caliburn does not bind the button to the method in the view model anymore. 并且可以正常工作,只是Caliburn不再将按钮绑定到视图模型中的方法上。

How should one bind methods to buttons in the title bar with caliburn.micro? 如何使用caliburn.micro将方法绑定到标题栏中的按钮?

I found out that Caliburn.Micro can still help, maybe not as comfortable as binding by name, but you can use the Message.Attach from the caliburnproject namespace. 我发现Caliburn.Micro仍然可以提供帮助,也许不如按名称绑定那样舒适,但是您可以使用caliburnproject命名空间中的Message.Attach。

I did it like this: 我这样做是这样的:

xmlns:cal="http://www.caliburnproject.org"

<Controls:MetroWindow.LeftWindowCommands>
    <Controls:WindowCommands>
        <Button Content="Home" cal:Message.Attach="[Event Click] = [Action ShowHomeVM()]"/>
    </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>

Hope this helps. 希望这可以帮助。

Caliburn.Micro can't handle these situations. Caliburn.Micro无法处理这些情况。 Your only options are: 您唯一的选择是:

  1. Fork the Caliburn.Micro source and fix it yourself. 分叉Caliburn.Micro来源并自行修复。
  2. Drop Caliburn.Micro and use a different MVVM framework. 删除Caliburn.Micro,并使用其他MVVM框架。

This does indeed not go with built-in features of Caliburn.Micro. 这确实与Caliburn.Micro的内置功能不匹配。 Instead of changing the internals of the framework which was suggested by @Keith, I just wired up the buttons by hand, namely in my MainView I define buttons as follows, 我没有手动更改@Keith建议的框架内部结构,而是手动连接了按钮,即在MainView按如下所示定义按钮,

<Controls:MetroWindow.LeftWindowCommands>
    <Controls:WindowCommands>
        <Button Content="Home" Command="{Binding HomeCommand}"/>
        <Button Content="Import" Command="{Binding ImportCommand}"/>
        <Button Content="Players" Command="{Binding PlayerCommand}"/>
    </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>

Here I am binding the Command property to Commands in the MainViewModel , which looks as follows, 在这里,我将Command属性绑定到MainViewModel Commands,如下所示:

public class MainViewModel : PropertyChangedBase {

    // ... other stuff ... 

    public ICommand HomeCommand { get; private set; }
    public ICommand PlayerCommand { get; private set; }
    public ICommand ImportCommand { get; private set; }

    public MainViewModel(IEventAggregator eventAggregator) {

        // ... other stuff ... //

        HomeCommand = new RelayCommand(o => CentralVM = HomeVM, o => true);
        PlayerCommand = new RelayCommand(o => CentralVM = PlayersVM, o => true);
        ImportCommand = new RelayCommand(o => CentralVM = ImportVM, o => true);
    }
}

As for the RelayCommand class, I am using the implementation given here . 至于RelayCommand类,我正在使用此处给出的实现。 The first argument in the constructor is the action that the command should execute, the second is whether it should be able to execute it - which I want to be always true for menu buttons (for now). 构造函数中的第一个参数是命令应执行的操作,第二个参数是命令是否应能够执行该命令-我现在希望菜单按钮始终为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM