Prism 4.1, Silverllight 5 и.Net 4.5.1 DelegateCommant для кнопки не работает
Я новичок в Призма. Я пытаюсь настроить тестовый проект на Prism. Я загружаю Prism 4.1, так как я узнал, что Prism 5 еще не работает с Silverlight 5. Моя конфигурация - вот так.
У меня есть Visual Studio 2013, Silverlight 5 и.Net 4.5.1. Основное упражнение: 1 домашняя страница, разделенная на 2 части с помощью 2-х модулей Prism. Готово и работает 1 Регион Hello, 2end Regigon world
Теперь в Hello Module я создаю 1 пользовательскую форму. Создано 1 Use.cs с INotifyPropertyChanged и т. Д. Следовал MVVM. Для созданных данных появилось в форме. Bow I bind 1 Кнопка "Отправить" и отображение даты изменения в том же регионе.
Я использовал DelegateCommand. Сейчас работает. Ошибка не отображается, но нет события.
Структура проекта выглядит следующим образом. Навигационное приложение.Net Silverlight
aprism
- Shell.xaml
- Bootstrapper.cs
aprism.web
- aprism.Hello
- HelloView.xaml
- HelloView.xamal.cs: UserControl, IHelloView
- IHelloView: IView
- IHelloViewModel: IViewModel
- HelloViewModel: ViewModelBase, IHelloViewModel
aprism.World
aprism.Business
- User.cs: INotifyPropertyChanged, IDataErrorInfo
aprism.Infrastructure
- IView
- IViewModel
- Режим просмотра
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>
Моя кнопка не запускает команду "Отправить". Помогите мне, пожалуйста. Спасибо
1 ответ
Наконец я получил это работает. После полировки моя концепция MVVM и снова Unity.
Проблема в том, что любое свойство, используемое в HelloViewModel, также должно иметь подпись в интерфейсе IHelloViewModel. И для этого свойства мне нужно реализовать I NotifyPropertyChanged тоже.
Так я и сделал вот так:
private DelegateCommand _command;
public DelegateCommand SubmitRegistrationFormCommand
{
get { return _command; }
set { _command = value; OnPropertyChanged("SubmitRegistrationFormCommand"); }
}
// This in Implementation
и в интерфейсе у меня была подпись свойства, чтобы понравиться это
DelegateCommand SubmitRegistrationFormCommand{set;get;}
Это может быть, когда Ioc Loads & Map этот интерфейс является ролевой игрой Thing as DataContext имеет тип this ViewModel после установки свойства
Первоначально не работал для меня, пока я не отобразил свой интерфейс.