我是Prism的新手。我正在尝试建立一个关于Prism的测试项目。我下载了Prism 4.1,因为我发现Prism 5不适用于Silverlight 5。所以我的配置是这样的。
我是visual studio 2013,Silverlight 5和.Net 4.5.1。基本练习1主页分为2部分,2个Prism模块我跟随Hello世界示例。完成和工作1地区你好,2end Regigon世界
现在在Hello Module中我创建了1个用户表单。使用INotifyPropertyChanged等创建了1个Use.cs。跟随MVVM。对于以表格形式出现的已创建数据。 Bow我绑定1提交按钮并在同一区域显示更改日期。
我使用了DelegateCommand。工作中 。 没有显示错误但没有触发事件。
项目结构就像这样 .Net Silverlight导航应用程序
aprism
aprism.web
aprism.World
aprism.Business
aprism.Infrastructure
public interface IView
{
IViewModel ViewModel { get; set; }
}
public interface IViewModel
{
IView View { get; set; }
}
public class ViewModelBase :IViewModel ,INotifyPropertyChanged
{
public ViewModelBase(IView view) {
View = view;
View.ViewModel = this;
}
public IView View
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}
}
public class HelloViewModel : ViewModelBase, IHelloViewModel
{
public DelegateCommand SubmitRegistrationForm { get; set; }
public HelloViewModel(View.IHelloView view):base(view)
{
this.View = view;
this.View.ViewModel = this;
this.HelloText = "Prism Hello..";
CreateUse();
//User.PropertyChanged +=User_PropertyChanged;
this.SubmitRegistrationForm = new DelegateCommand(Save, CanSave);
}
/* private void User_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
SubmitRegistrationForm.RaiseCanExecuteChanged();
}*/
private bool CanSave()
{
return true;
}
private void Save()
{
User.DateUpdated = DateTime.Now;
}
#region IHelloViewModel Members
public string HelloText { get; set; }
private User _user;
public User User {
get { return _user; }
set
{
_user = value;
OnPropertyChanged("User");
}
}
private void CreateUse() {
User = new User()
{
Username="Anand",
Email = "akirti.iitk@gmail.com",
Password = "delasoft",
ConfirmPassword = "delasoft"
};
}
/* public Infrastructure.IView View
{
get;
set;
}
*/
#endregion
}
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="Hpmsprism.Hello.View.HelloView"
mc:Ignorable="d"
xmlns:local="clr-namespace:Hpmsprism.Business;assembly=Hpmsprism.Business"
xmlns:Commands="clr-namespace:Microsoft.Practices.Prism.Commands;assembly=Microsoft.Practices.Prism"
d:DesignHeight="500" d:DesignWidth="500">
<UserControl.Resources>
<local:User x:Key="cUser"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{Binding HelloText}" FontSize="26" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="10"/>
<TextBlock HorizontalAlignment="Left" Margin="98,35,0,0"
TextWrapping="Wrap" Text="User Registration"
VerticalAlignment="Top" FontSize="20"/>
<sdk:Label HorizontalAlignment="Left"
Height="28" Margin="110,96,0,0"
VerticalAlignment="Top" Width="90" Content=" User Name : "/>
<sdk:Label HorizontalAlignment="Left"
Height="28" Margin="110,129,0,0"
VerticalAlignment="Top" Width="90" Content=" Email : "/>
<sdk:Label HorizontalAlignment="Left"
Height="28" Margin="110,157,0,0"
VerticalAlignment="Top" Width="90" Content=" Password : "/>
<sdk:Label HorizontalAlignment="Left"
Height="28" Margin="110,204,0,0"
VerticalAlignment="Top" Width="124" Content=" Confirm Password : "/>
<Button Content="Register" HorizontalAlignment="Left" Margin="342,301,0,0"
VerticalAlignment="Top" Width="75" x:Name="SubmitRegisterForm" Commands:Click.Command="{Binding Path=SubmitRegistrationFormCommand}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="247,92,0,0"
TextWrapping="Wrap" Text="{Binding User.Username,Mode=TwoWay,ValidatesOnDataErrors=True}" VerticalAlignment="Top" Width="170" x:Name="UserName"
/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="247,125,0,0"
TextWrapping="Wrap" Text="{Binding User.Email,Mode=TwoWay,ValidatesOnDataErrors=True}" VerticalAlignment="Top" Width="170" x:Name="Email"/>
<PasswordBox HorizontalAlignment="Left" Margin="247,157,0,0"
VerticalAlignment="Top" Width="170" x:Name="Password"
Password="{Binding User.Password,Mode=TwoWay,ValidatesOnDataErrors=True}"/>
<PasswordBox HorizontalAlignment="Left" Margin="247,200,0,0"
VerticalAlignment="Top" Width="170" x:Name="ConfirmPassword"
Password="{Binding User.ConfirmPassword,Mode=TwoWay,ValidatesOnDataErrors=True}"/>
<sdk:Label HorizontalAlignment="Left" Height="28" Margin="110,257,0,0"
VerticalAlignment="Top" Width="120" Content=" Date Updated :"/>
<TextBlock HorizontalAlignment="Left" Margin="230,257,0,0" TextWrapping="Wrap"
Text="{Binding User.DateUpdated}" VerticalAlignment="Top" x:Name="DateUpdated"/>
</Grid>
</UserControl>
我的按钮不会触发提交命令。 请帮我。 谢谢
答案 0 :(得分:0)
最后我得到了它的工作。抛光我的MVVM概念&amp;再次团结。
问题是HelloViewModel中使用的任何属性也应该在IHelloViewModel接口中具有签名。对于这个属性我也需要实现INotifyPropertyChanged。
所以我这样做了:
private DelegateCommand _command;
public DelegateCommand SubmitRegistrationFormCommand
{
get { return _command; }
set { _command = value; OnPropertyChanged("SubmitRegistrationFormCommand"); }
}
// This in Implementation
并且在界面中我有一个喜欢这个的属性签名
DelegateCommand SubmitRegistrationFormCommand{set;get;}
这可能是当Ioc Loads&amp;映射此接口是角色扮演Thing,因为DataContext是在设置属性后键入此ViewModel
最初在我没有映射我的界面之前没有为我工作。