- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习带有 MVVM 设计模式的 WPF,并试图了解如何在背后的代码之外完成一些事情。
我有一个登录页面,如下图所示。
我有一个从 http://www.wpftutorial.net/PasswordBox.html 获取的密码控制.
我现在为了简单的理解,想问你我的代码是否在正确的类/设置中正确地遵守 MVVVM 规则和关注点分离。
我目前有一个 if 语句来检查详细信息是否与名称字符串和密码字符串匹配。
代码在后面的代码中。我只是想知道这对于 MVVM 是否正确。我想知道您如何在 ViewModel 中实现这一点?
private void OK_Click(object sender, RoutedEventArgs e)
{
if (emp.Name == "ep" && emp.Password == "pass")
{
MessageBox.Show("namd and Pw accepted");
//open new page
var HomeScreen = new HomeScreen();
HomeScreen.Show();
}
else
{
//deny access
MessageBox.Show("Incorrect username and password");
}
}
最佳答案
不要在后面的代码中实现按钮单击处理程序,而是使用 ICommand
并将其绑定(bind)到 XAML 中的按钮事件。
这是一个非常棒的教程,让我开始使用 MVVM:
WPF Apps With The Model-View-ViewModel Design Pattern
[编辑添加示例代码]
这是一个简单的代码示例,可以完成您的示例所做的工作,但采用 MVVM 风格,根本没有任何代码隐藏代码。
1) 创建一个新的 WPF 解决方案,对于这个小示例,我将其简单命名为“WpfApplication”。
2) 编辑自动创建的MainWindow.xaml
的代码:
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clr-namespace:WpfApplication"
Title="MainWindow" Height="234" Width="282">
<!-- Create the ViewModel as the initial DataContext -->
<Window.DataContext>
<viewModel:MainWindowViewModel />
</Window.DataContext>
<Grid>
<TextBox Height="23"
HorizontalAlignment="Left"
Margin="70,31,0,0"
Name="textBox1"
VerticalAlignment="Top"
Width="120"
Text="{Binding Path=Name}"/>
<TextBox Height="23"
HorizontalAlignment="Left"
Margin="70,72,0,0"
Name="textBox2"
VerticalAlignment="Top"
Width="120"
Text="{Binding Path=Password}" />
<Label Content="Name"
Height="28"
HorizontalAlignment="Left"
Margin="22,29,0,0"
Name="label1"
VerticalAlignment="Top" />
<Label Content="PW"
Height="28"
HorizontalAlignment="Left"
Margin="22,70,0,0"
Name="label2"
VerticalAlignment="Top" />
<Button Content="OK"
Height="23"
HorizontalAlignment="Left"
Margin="70,119,0,0"
Name="button1"
VerticalAlignment="Top"
Width="120"
Command="{Binding Path=LoginCommand}"
CommandParameter="{Binding Path=.}"
/>
</Grid>
</Window>
namespace WpfApplication
{
using System;
using System.Windows;
using System.Windows.Input;
/// <summary>
/// Checks the user credentials.
/// </summary>
public class LoginCommand : ICommand
{
/// <summary>
/// Defines the method to be called when the command is invoked.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
public void Execute(object parameter)
{
MainWindowViewModel viewModel = parameter as MainWindowViewModel;
if (viewModel == null)
{
return;
}
if (viewModel.Name == "ep" && viewModel.Password == "pass")
{
MessageBox.Show("namd and Pw accepted");
//open new page
var HomeScreen = new HomeScreen();
HomeScreen.Show();
}
else
{
//deny access
MessageBox.Show("Incorrect username and password");
}
}
/// <summary>
/// Defines the method that determines whether the command can execute in its current state.
/// </summary>
/// <returns>
/// true if this command can be executed; otherwise, false.
/// </returns>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
public bool CanExecute(object parameter)
{
// Update this for your application's needs.
return true;
}
public event EventHandler CanExecuteChanged;
}
}
namespace WpfApplication
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class MainWindowViewModel : INotifyPropertyChanged
{
#region Implementation of INotifyPropertyChanged
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Signal that the property value with the specified name has changed.
/// </summary>
/// <param name="propertyName">The name of the changed property.</param>
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion Implementation of INotifyPropertyChanged
#region Backing Fields
/// <summary>
/// Gets or sets the value of Name.
/// </summary>
private string name;
/// <summary>
/// Gets or sets the value of Password.
/// </summary>
private string password;
/// <summary>
/// Gets or sets the value of LoginCommand.
/// </summary>
private LoginCommand loginCommand;
#endregion Backing Fields
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
/// </summary>
public MainWindowViewModel()
{
this.loginCommand = new LoginCommand();
}
#endregion Constructor
#region Properties
/// <summary>
/// Gets or sets the name of the user.
/// </summary>
public string Name
{
get
{
return this.name;
}
set
{
if (this.name == value)
{
return;
}
this.name = value;
this.OnPropertyChanged("Name");
}
}
/// <summary>
/// Gets or sets the user password.
/// </summary>
public string Password
{
get
{
return this.password;
}
set
{
if (this.password == value)
{
return;
}
this.password = value;
this.OnPropertyChanged("Password");
}
}
/// <summary>
/// Gets or sets the command object that handles the login.
/// </summary>
public ICommand LoginCommand
{
get
{
return this.loginCommand;
}
set
{
if (this.loginCommand == value)
{
return;
}
this.loginCommand = (LoginCommand)value;
this.OnPropertyChanged("LoginCommand");
}
}
#endregion Properties
}
}
HomeScreen
它将由 LoginCommand 打开到解决方案。 :-)
关于c# - MVVM 模式查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21626025/
我已经对这个主题进行了一些研究,并且已经在少数应用程序中使用了 MVVM 模式。 我问这个问题是因为有时 MVVM 被称为设计,有时被称为架构模式。 在大多数情况下,MVVM 模式称为设计模式。但是就
我开始使用 MVVM Light 版本 4,但我无法理解: 为什么要使用 DataService 和 IDataService? 我应该为模型中的任何类创建数据服务吗? 最佳答案 首先 - 像往常一样
是否可以采用MVVM在一个平台(如 windows phone)中设计模式并以可移植到其他平台(如 android 和 iOS)的方式实现代码的数据绑定(bind)? 或者我最好问问MVVM设计模式在
使用 avalondock在 MVVM 环境中似乎相当具有挑战性。一旦我从 shellview 中分离 DocumentPane,我就会丢失相应的数据上下文并且我的 View 是空的。重新连接时,它会
我对避免背后代码中的代码的方法很感兴趣。 在我看来,有些情况下代码必须放在代码后面。 例如:我有一个未定义列数的网格。无法绑定(bind)列。所以最简单的方法是在后面的代码中生成列。 对于这种情况,我
我熟悉MVVM。实际上,我在SL4中进行了大部分学习。但是,由于最近的需求,我必须使用SL3。我试图将MVVM Light v3与SL3结合使用并利用命令。问题是在SL3中没有按钮的Command属性
UI逻辑在WindowsRT MVVM应用程序中应该在哪里?将其放到ViewModel上真的很“胖”,我想我们失去了MVVM模式的优势之一-在设计人员和程序员之间分配工作变得非常困难。但是,我创建了一
您好,我有 3 个关于 MVVM 模型的问题。 有没有办法绕过那个多余的PropertyChanged("PropName"); 将 POCO 对象包装到 WPF 的最佳方法是什么 INotifyPr
我正在使用 MVVM 模型做一个 Silverlight,我发现很难通过 MVVM 进行事件处理,尤其是事件处理程序在 View 中进行了大量更改,例如启用和禁用按钮、更新媒体元素功能和位置。我还是
我有一个测试应用程序来测试 windows phone 8.1 上的导航,我可以从主页到第二页进入第二页。 问题是,当我单击后退按钮时,我返回桌面屏幕并且应用程序进入后台,所以我必须按住后退按钮才能返
我正在尝试使用并选择好的MVVM Framework,并且其中有很多,因此选择确实很困难。 我想知道其中的2个-CinchV2(Sacha Barber)和MVVM Light Toolkit(Lau
我完全不熟悉Windows 8开发,现在遇到使用MVVM Light混合触摸和键盘导航的问题。 所以,我有个 View 模型的列表,在网格 View 和只要选择其中的一个,导航到选定的 View 模型
我最近下载了MVVMExtraLite,并且有一个名为 Mediator 的帮助程序。我听说过 Messenger (在MVVM Light中)。有什么区别吗? 最佳答案 他们使用相同的模式,即调解员
我正在尝试学习MVVM,并且在区分模型和 View 模型方面有些挣扎。 如果有人可以回答这两个问题,那么对我来说将大有帮助: 说我有一个Objects类,这是一个包含Object的多个Observab
我已经在网上进行了一些研究,并且得出了一些矛盾的答案。这是我的情况: 我有一个引用ClientViewModel的EditClient View ,还有一个还引用ClientViewModel的Add
我正在使用带有 ModelView-First 方法的 MVVM 模式。到目前为止,这工作正常。 现在我有一个用户控件( View ),它应该根据位于我的 ViewModel 中的属性显示各种内容。
我必须创建一个对话框,其中必须在运行时生成列,之前我使用的是 WPF 数据网格,因此在运行时生成列不是问题。现在我必须使用 View 模型,我需要为要在 View 中显示为列的任何字段具有属性。列数在
所以我目前正在使用 Xamarin.Forms 开发一个应用程序。 Xamarin Forms 使用 MVVM 模式,我觉得使用这种模式有点舒服,但我确实有一些问题。为了简单起见,我将使用一个单页应用
是否有在MVVM应用程序中使用Autofac的示例?我不确定在MVVM环境中如何控制生命周期和对象处置。 我知道我可以创建一个生命周期并从其下解决,但这确实更像是服务定位器模式而不是IoC模式。 最佳
我想我遗漏了一些简单的东西,但我找不到任何例子来说明如何做到这一点......另外,如果我使用的某些术语是错误的,请原谅我。 我只想使用绑定(bind)到 Kendo Observable 对象的 H
我是一名优秀的程序员,十分优秀!