【问题标题】:Communicate Between Two View in WPF MVVM Only仅在 WPF MVVM 中的两个视图之间进行通信
【发布时间】:2011-10-12 08:18:51
【问题描述】:

我在 MVVM(WPF) 中有两个视图。第一个视图包含两个文本框:用户名、密码,第二个视图有两个按钮:提交和清除。两个视图现在都设置在表单上。当我按下“清除”按钮时,两个文本框都被清除,并在提交中显示用户名和密码的消息。我只使用 MVVM+WPF,而不是棱镜。

第一个视图的ModelView:

class LoginView:ViewModelBase
    {
        string _userName;
        public string  UserName 
        {
            get {return _userName ; }
            set {
                if (_userName != value)
                {
                    _userName = value;
                }
                base.OnPropertyChanged(UserName);
            }
        }

        string _Pwd;
        public string PWD
        {
            get { return _Pwd; }
            set
            {

                _Pwd = value;
                base.OnPropertyChanged(_Pwd);
            }
        }
    }

和按钮

 class ButtonHandler
    {
        private DelegateCommand _ClearData;
        public ICommand ClearCommand
        {
            get
            {
                if (_ClearData == null)
                {
                    _ClearData = new DelegateCommand(ClearText);
                }
                return _ClearData;
            }
        }
        LoginView lg = new LoginView();
        private void ClearText()
        {
            lg.UserName = "";
            lg.PWD = "";
        }
    }

并查看第一个控件的代码

   <Label Content="Login" Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left"
           FontFamily="Georgia" FontSize="24" FontWeight="UltraBold" ></Label>
    <Label Content="User Name" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
    <Label Content="Password" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
    <TextBox  Name="username" Grid.Row="1" Grid.Column="1" Margin="100,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></TextBox>
    <TextBox  Name="pwd" Grid.Row="2" Grid.Column="1" Margin="100,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=PWD,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    <Separator Grid.Row="0" Grid.Column="1" Height="5" Margin="0,40,0,0" Background="Green"></Separator>

和按钮视图

     <Button x:Name="Submit" Content="Submit" Grid.Column="1"></Button>
     <Button x:Name="Clear" Content="Clear" Grid.Column="2" 
             Command="{Binding Path=ClearCommand, Mode=OneWay, 
             UpdateSourceTrigger=PropertyChanged}" >
     </Button>

为什么它不起作用?

【问题讨论】:

  • 您能使用标点符号吗?

标签: wpf mvvm


【解决方案1】:

您没有正确使用 MVVM 模式,在这种模式下,ViewModel 不应该有对 View 的引用。命令是您的 ViewModel 的一部分,因此您对 LoginView 的引用违反了该模式。

所以你有两个输入字段和一个按钮?为此,我将拥有一个 ViewModel 和一个 View。 ViewModel 将公开两个字符串属性(用户名和密码)和一个绑定到清除按钮的命令。当命令执行时,它将清除 ViewModel 上的用户名和密码文本。然后视图将相应更新。

【讨论】:

  • ColinE 感谢感谢,实际上我是 MMVM 的新用户,我尝试在两个视图之间进行通信,所以我创建了两个视图,我知道这项工作是由单个视图完成的。
  • 旁注:在极少数情况下,ViewModel 必须引用视图。例如,在 PRISM 中使用 ViewModel-First 方法时就是这种情况。那里的引用用于设置视图的数据上下文(使用接口具有视图独立耦合仅用于设置 DataContext)。但您的主要信息是正确的:ViewModel 不应直接与视图交互。
【解决方案2】:

MVVM 的基本原理是拥有一个视图可以绑定到的类,其中包含所有应用程序逻辑。主要原因之一是拥有a separation of concerns. 因此,如果您想要一个用户名,您可以公开一个视图绑定的属性,然后当您想要登录时,您创建一个函数,使用这些绑定值提交给您的业务逻辑层您的应用程序。

这似乎是在您的示例中利用 MVVM 的一种方式:

public class LoginViewModel
{
  public string UserName {get;set;}//Implement INotifyPropertyChanged
  public string PWD {get;set;}

    private DelegateCommand _ClearData;
    public ICommand ClearCommand
    {
        get
        {
            if (_ClearData == null)
            {
                _ClearData = new DelegateCommand(ClearText);
            }
            return _ClearData;
        }
    }

    private void ClearText()
    {
        UserName = "";
        PWD = "";
    }
}

然后在您的 xaml 中:

<TextBox Text={Binding UserName} />
<TextBox Text={Binding PWD} />
<Button Command={Binding ClearCommand}/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 2010-12-18
    相关资源
    最近更新 更多