Prism 4.1 , Silverllight 5 and .Net 4.5.1 DelegateCommant for Button Not working

大兔子大兔子 提交于 2019-12-12 02:54:02

问题


I'm new to Prism. I'm trying to setup a test project on Prism. I download Prism 4.1 as I found out Prism 5 doesn't work with Silverlight 5 Yet. so My configuration is Like this.

I've visual studio 2013 , Silverlight 5 and .Net 4.5.1. A basic exercise 1 Homepage divided in to 2 parts with 2 Prism module I followed for Hello world example. Done and working 1 Region Hello, 2end Regigon world

Now in Hello Module I create 1 User form. Created 1 Use.cs with INotifyPropertyChanged etc. Followed MVVM. For created Data Appeared in form. Bow I bind 1 Submit button and display change date on the same region.

I used DelegateCommand. Now working . No Bug shown But No firing of event.

Project Structure is Like this .Net Silverlight Navigation Application

  • 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
    • ViewMode
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>

My Button Doesn't fire Submit Command. PLease Help me. Thank you


回答1:


Finally I got It working. After Polishing my MVVM Concept & Unity Again.

Problem is Any Property used in HelloViewModel should have the signature in IHelloViewModel Interface too. And for this Property I Need to Implement INotifyPropertyChanged too.

So I did Like this :

private DelegateCommand _command;
    public DelegateCommand SubmitRegistrationFormCommand
    {
        get { return _command; }
        set { _command = value; OnPropertyChanged("SubmitRegistrationFormCommand"); }
    }
// This in Implementation

and In Interface I had a Property Signature to Like this

DelegateCommand SubmitRegistrationFormCommand{set;get;}

This may be When Ioc Loads & Map this interface is the role play Thing as DataContext is of Type this ViewModel after Setting the Property

http://social.msdn.microsoft.com/Forums/en-US/531eb18f-3060-4adf-a44e-8dffe0fcbd07/prism-41-silverlight-5-with-net-451-delegatecommand-not-working?forum=wpf

Initially Didn't work for me until I didn't mapped my Interface.



来源:https://stackoverflow.com/questions/24477864/prism-4-1-silverllight-5-and-net-4-5-1-delegatecommant-for-button-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!