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);
            }
        }
    }
  • 相关阅读:
    AngularJS 用 $http.jsonp 跨域SyntaxError问题
    jQuery调用WCF需要注意的一些问题
    WCF 添加 RESTful 支持,适用于 IIS、Winform、cmd 宿主
    搭建个人博客-hexo+github
    Git错误提示之:fatal: Not a git repository (or any of the parent directories): .git
    C# WCF 完整实例,winform 窗体作为 宿主
    【WCF】HTTP 无法注册 URL 进程,不具有此命名空间的访问权限
    Jsoncpp Compiler、Programming
    CVE-2015-1328 Ubuntu 12.04, 14.04, 14.10, 15.04 overlayfs Local Root
    Linux UserSpace Back-Door、SSH/PAM Backdoor/Rootkit、SSH Session Hijacking技术研究
  • 原文地址:https://www.cnblogs.com/amw2738/p/3733238.html
Copyright © 2011-2022 走看看