Prism 4.1,Silverlight 5和.Net 4.5.1 DelegateCommant对于按钮不起作用 [英] Prism 4.1 , Silverllight 5 and .Net 4.5.1 DelegateCommant for Button Not working

查看:85
本文介绍了Prism 4.1,Silverlight 5和.Net 4.5.1 DelegateCommant对于按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Prism的新手.我正在尝试在Prism上设置一个测试项目.我下载了Prism 4.1,因为我发现Prism 5不适用于Silverlight 5 Yet.所以我的配置是这样的.

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.

我有Visual Studio 2013,Silverlight 5和.Net 4.5.1.一个基本练习1主页分为2个部分,其中包含2个Prism模块,下面是Hello World示例.完成并正在工作1个地区您好,第2个Regigon世界

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

现在在"Hello模块"中,我创建1个用户表单.使用INotifyPropertyChanged等创建了1个Use.cs.对于创建的数据,以表格形式显示.鞠躬,我绑定了1个提交"按钮,并在同一区域显示更改日期.

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.

我使用了DelegateCommand.工作中 . 没有显示错误,但没有触发事件.

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

项目结构是这样的 .Net Silverlight导航应用程序

Project Structure is Like this .Net Silverlight Navigation Application

  • 世界主义

  • aprism

  • Shell.xaml
  • Bootstrapper.cs

aprism.web

aprism.web

  • aprism.Hello
  • HelloView.xaml
  • HelloView.xamal.cs:UserControl,IHelloView
  • IHelloView:IView
  • IHelloViewModel:IViewModel
  • HelloViewModel:ViewModelBase,IHelloViewModel

aprism.World

aprism.World

aprism.Business

aprism.Business

  • User.cs:INotifyPropertyChanged,IDataErrorInfo

aprism.Infrastructure

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

推荐答案

最后,我明白了.抛光我的MVVM Concept&之后再次团结.

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

问题是HelloViewModel中使用的任何属性也应该在IHelloViewModel接口中也具有签名.对于这个属性,我也需要实现INotifyPropertyChanged.

所以我做到了:

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;}

这可能是在Ioc加载&设置属性后,映射此接口是角色扮演的角色,因为DataContext属于此ViewModel

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

最初,在我没有映射接口之前,它对我不起作用.

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

这篇关于Prism 4.1,Silverlight 5和.Net 4.5.1 DelegateCommant对于按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆