Tuesday, February 21, 2012

Commanding in Silverlight MVVM

Create class named DelegateCommand contains command arguments and Executed Method.

 public delegate void DelegateCommandExecutedEventHandler(Object sender, 
                             DelegateCommandExecutedEventArgs e);

    public class DelegateCommandExecutedEventArgs : EventArgs
        public DelegateCommand Command { get; set; }
        public Object Parameter { get; set; }

        public DelegateCommandExecutedEventArgs(DelegateCommand command, Object parameter)
            Command = command;
            Parameter = parameter;

    public class DelegateCommand : ICommand

        private Boolean _canExecuteCore;
        public Boolean CanExecuteCore
            get { return _canExecuteCore; }
            set { _canExecuteCore = value; OnCanExecuteChanged(); }

        public event EventHandler CanExecuteChanged;

        protected void OnCanExecuteChanged()
            EventHandler temp = CanExecuteChanged;
            if (temp != null)
                temp(this, EventArgs.Empty);

        public event DelegateCommandExecutedEventHandler Executed;

        protected void OnExecuted(DelegateCommand command, Object parameter)
            DelegateCommandExecutedEventHandler temp = Executed;
            if (temp != null)
                temp(this, new DelegateCommandExecutedEventArgs(command, parameter));

        public DelegateCommand()
            CanExecuteCore = true;

        public Boolean CanExecute(object parameter)
            return CanExecuteCore;

        public void Execute(object parameter)
            OnExecuted(this, parameter);

Now using Command in XAML page

 <Button Content="Save"
         Command="{Binding SaveCommand}"
         Grid.Column="1" />

Execute Command From ViewModel 

public class EmployeeViewModel
    public DelegateCommand SaveCommand { get; set; } 
    public EmployeeViewModel()
        SaveCommand = new DelegateCommand();
        SaveCommand.Executed += new DelegateCommandExecutedEventHandler(SaveCommand_Executed); 
    void SaveCommand_Executed(object sender, DelegateCommandExecutedEventArgs e)
            if (e.Parameter == null || string.IsNullOrEmpty(e.Parameter.ToString()))
           //......here your code 

Monday, February 13, 2012

Calling WCF/Web Service Without Adding Service Reference

Sometimes we need to calling service without adding service reference.
Below is the code snippet.

Let Assume that your service name is ConfigurationServices and it has method getCustomerDetails.

we retrieve User Details based on UserId using getCustomerDetails method.  

var factory = new ChannelFactory<ConfigurationServices>(new BasicHttpBinding(), 
           new EndpointAddress("http://localhost/HirenWebServices/ConfigurationServices"));
var proxy = factory.CreateChannel();
var response = proxy.getCustomerDetails(
           new getCustomerDetails() { arg0 = AppConfiguration.Current.Userid });
var v = response.@return;

Friday, February 10, 2012

Referenced assembly does not have a strong name

Sometimes you got error like following When you reference 3rd party dll

Assembly generation failed -- Referenced assembly 'Test' does not have a strong name
Cause is An assembly is not signed with a strong name, the strong name could not be verified, or the strong name would not be valid without the current registry settings of the compute.

The strong name protects clients from unknowingly loading an assembly that has been tampered with. Assemblies without strong names should not be deployed outside of very limited scenarios.

An assembly without a strong name  cannot be loaded into the global assembly cache.

To Fix this, have to follow below steps.

Step 1 : Run visual studio command prompt and go to directory where your DLL located.

 For Example my DLL located in D:/hiren/Test.dll

Step 2 : Now create il file using below command.

D:/hiren> ildasm /all /out=Test.il Test.dll
(this command generate code library)

Step 3 : Generate new Key for sign your project.

D:/hiren> sn -k mykey.snk

Step 4 : Now sign your library using ilasm command.

D:/hiren> ilasm /dll /key=mykey.snk Test.il

so after this step your assembly contains strong name and signed.

Jjust add reference this new assembly in your project and compile project its running now.