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);
            }
        }
    }
  • 相关阅读:
    显示/隐藏Mac下的隐藏文件
    遍历指定文件下所有文件,删除指定后缀文件
    ssh公钥
    设置状态栏(statusbar)的样式
    Silverlight上传下载三种方法解析(三)
    Silverlight上传下载三种方式解析(二)
    Silverlight 上传下载之三种方式解析
    一个简单的存储过程代码生成器
    .net 程序发生了一个不可捕获的异常
    n取的r的组合数问题
  • 原文地址:https://www.cnblogs.com/amw2738/p/3733238.html
Copyright © 2011-2022 走看看