zoukankan      html  css  js  c++  java
  • Prism--MVVM 之Command

    最近在做个项目,用到了MVVM模式。

    发现在用DelegateCommand时,用到了CanExecute,不能实时更新,查了很多资料都没有这方面的。

    经过仔细研究prism源码,发现以下解决方案:

    下面是界面,很简单,一个textbox,一个button。实现的功能是

    按下button时,显示textbox里的内容。

    主要是当textbox内容为空时,button是不能使用的

    <Grid>
            <TextBox  Margin="54,35,193,40" Name="textBox1"  />
            <Button Command="{Binding ShowMessage}" CommandParameter="{Binding Text, ElementName=textBox1}" Content="Button"  Margin="200,35,53,38"  />
        </Grid>
    public ICommand ShowMessage
            {
                get
                {
                    return new DelegateCommand<string>(
                        (str) =>
                        {
                            MessageBox.Show(str);
                        }, 
                        (str) =>
                        {
                            return !string.IsNullOrEmpty(str);
                        }
                    );
                }
            }

    运行起来,好像是没有什么问题,但当我们在textbox里填写内容时,发现button不会使能,这就有问题了。

    也就是说,当textbox内容改变时,CanExecute不知道。

    而怎么样才能知道呢,这里我们就要用到prism中的RaiseCanExecuteChanged这个方法了。

    具体的代码以下:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="150" Width="373">
        <Grid>
            <TextBox Text="{Binding Message,UpdateSourceTrigger=PropertyChanged}" Margin="54,35,193,40" Name="textBox1"  />
            <Button Command="{Binding ShowMessage1}"  Content="Button"  Margin="200,35,53,38"  />
        </Grid>
    </Window>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.Prism.ViewModel;
    using System.Windows.Input;
    using Microsoft.Practices.Prism.Commands;
    using System.Windows;
    
    namespace WpfApplication1
    {
        class mainWindowViewModel:NotificationObject
        {
            private string message;
    
            public string Message
            {
                get { return message; }
                set
                {
                    message = value;
                    RaisePropertyChanged("Message");
                    ShowMessage1.RaiseCanExecuteChanged();
                }
            }
    
    
            public DelegateCommand ShowMessage1 {get;private set;}
           
    
            public ICommand ShowMessage
            {
                get
                {
                    return new DelegateCommand<string>(
                        (str) =>
                        {
                            MessageBox.Show(str);
                        }, 
                        (str) =>
                        {
                            return !string.IsNullOrEmpty(str);
                        }
                    );
                }
            }
    
            public mainWindowViewModel()
            {
    
                ShowMessage1 = new DelegateCommand(onExecute,onCanExecute);
                
            }
    
            private void onExecute()
            {
                MessageBox.Show(Message);
            }
    
            private bool onCanExecute()
            {
                return !string.IsNullOrEmpty(message);
            }
        }
    }
  • 相关阅读:
    异步文档树解决方案
    兼容IE低版本的文件上传解决方案
    CentOS-常用命令(版本:7.x)
    搭建Nexus3私服(含使用说明,支持CentOS、Windows)
    CentOS-搭建MinIO集群
    GitLab升级(yum安装版v11.11.8~12.0.12)
    yum安装GitLab-v11.11.8(git私服)
    Docker中容器的备份和恢复(可迁移)
    Nexus3配置yum私服
    局域网连接数据慢的方法汇总
  • 原文地址:https://www.cnblogs.com/amw2738/p/3733238.html
Copyright © 2011-2022 走看看