MVVM:在调用命令后运行仅查看代码 [英] MVVM: run view-only code after calling command

查看:119
本文介绍了MVVM:在调用命令后运行仅查看代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的


  • 使用MVVM模式

  • 用XAML编写的视图

  • ViewModel中的命令 MyCommand ,可从视图中的多个位置调用

  • 在视图中操作的方法 DoSthInView ,在代码隐藏中定义

  • using MVVM pattern
  • a view written in XAML
  • a command MyCommand in the ViewModel which gets called from several places in the view
  • a method DoSthInView that operates on the view, defined in codebehind

我的目标:

无论何时执行命令,我都想调用 DoSthInView ,无论哪个控件执行了命令。

Whenever the command is executed, I want to call DoSthInView, no matter which control executed the command.

问题:

由于在MVVM中ViewModel不知道View,因此无法从ViewModel调用 DoSthInView 。那么如何称呼此代码?

Since in MVVM the ViewModel does not know the View, I cannot call DoSthInView from the ViewModel. So how do call this code?

自己的想法:

要少一些抽象,这是我的用例:我们有一个Button和一个TextBox。该命令获取当前在TextBox中的文本,并将其写入模型数据中的某个位置。完成本文后,我希望为出现和淡出的绿色复选标记设置动画(这是 DoSthInView ),以便用户可以直观地确认数据已更新。

To be less abstract, this is my use case: We have one Button and one TextBox. The command takes the text which is currently in the TextBox and writes it somewhere into the model data. When this writing is done, I want to animate a green checkmark appearing and fading out (this is DoSthInView), so that the user gets a visual confirmation that the data was updated.

有两种运行命令的方式:

There are two ways of running the command:


  1. 单击按钮

  2. 在TextBox处于焦点状态时按 Enter

对于按钮,我知道一种方法调用 DoSthInView

For the Button I know a way to call DoSthInView:

<Button Content="run command" Command="{Binding MyCommand}" Click={Binding DoSthInView}" />

对于文本框,我有一个KeyBinding来按Enter键:

For the TextBox, I have a KeyBinding to take the Enter key:

<TextBox>
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding MyCommand}" Key="Enter" />
    </TextBox.InputBindings>
</TextBox>

但是InputBindings似乎不支持事件,仅支持命令。不知道如何调用 DoSthInView

But InputBindings seem not to support events, only commands. So here I have no idea how to call DoSthInView.

但是即使我找到了调用的方法从输入绑定(类似于Button)中找到DoSthInView ,感觉不对。我正在寻找一种方式说只要执行 MyCommand ,就运行 DoSthInView ,这样,并非每个 MyCommand 必须单独照顾它,但是只有一个地方可以处理。也许可以在根FrameworkElement中完成此操作?

But even if I found a way to call DoSthInView from within the input binding (analog to the Button), it wouldn't feel right. I am looking for a way to say "whenever MyCommand is executed, run DoSthInView" So that not every caller of MyCommand has to care for it individually, but there is just one place to handle that. Maybe this can be done in the root FrameworkElement?

推荐答案

基于Leonid Malyshev的暗示,这里是使用事件的一种非常干净的解决方案(关于对View和ViewModel进行分离的干净方法:

Based on Leonid Malyshev's hint here is a quite clean solution using an event ("clean" regarding seperation of View and ViewModel):

ViewModel代码:

ViewModel code:

public class MyViewModel
    {
    ...
    public event Action MyCommandExecuted;
    public DelegateCommand MyCommand; // consists of MyCommandExecute, MyCommandCanExecute
    ...
    private void MyCommandExecute()
    {
        ... // actual command code
        MyCommandExecuted.Invoke();
    }
    ...
}

查看背后的代码:

public partial class MyView : Window
{
    public MyView(MyViewModel vm)
    {
        InitializeComponent();
        DataConext = vm;
        vm.MyCommandExecuted += DoSthInView();
    }
    ...
    private void DoSthInView()
    {
        ...
    }
    ...
}

这篇关于MVVM:在调用命令后运行仅查看代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆