Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Tuesday, 15 September 2015

Close a WPF Window using MVVM

Close a WPF Window using MVVM


In MVVM (Model – View – ViewModel) design pattern, the ViewModel is a heart of View and this class that typically contains the values you want to display in your View  . ViewModel shouldn’t know anything about the View.


 In this article I will tell you different ways for closing form in WPF


1.   Using traditional Code Behind Close event


private void Close_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}


As you can see, it’s quite simple – only one line of code that I had to write


2.   Using MVVM design pattern

In the View of XAML my button:

<Button Content="Close" Command="{Binding CloseCommand}"/>

In the ViewModel:

public bool CanClose { get; set; }

private RelayCommand closeCommand;
public ICommand CloseCommand
{
    get
    {
        if(closeCommand == null)
        (
            closeCommand = new RelayCommand(param => Close(), param => CanClose);
        )
    }
}

public void Close()
{
    this.Close();
}


Problem with this solution


All we’ve done here is move the close button event handler from the code-behind file to the ViewModel. The problem we run into, As you may be aware, the this keyword refers to the class in which it exists. This was fine in the code-behind of the Window, as this then referred to the Window, and this.Close() would successfully call the window’s Close() method. But now that we’ve moved the method into the ViewModel, this no longer refers to the Window (View), but rather the ViewModel – which doesn’t have a Close method.



3.   The Action object.

We will add an Action property to the ViewModel, but define it from the View’s code-behind file. This will let us dynamically define a reference on the ViewModel that points to the View.

Creating Action

public Action CloseAction { get; set; }

public View()
{
    InitializeComponent() // this draws the View
    ViewModel vm = new ViewModel(); //this creates an instance of ViewModel
    this.DataContext = vm; // this sets the ViewModel as the DataContext for the View
    if ( vm.CloseAction == null )
        vm.CloseAction = new Action(() => this.Close());
}

we’ve assigned a delegate to the CloseAction Action property that we can simply invoke whenever we want to close the Window.

Calling in ViewModel

public void Save()
{
   
    CloseAction(); // Invoke the Action previously defined by the View
}


Using the method described here, we can define the Close method with a single line on the ViewModel, two lines of code on the View, and invoke the Close method with a single line making a call to CloseAction().

Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


 Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

Sunday, 5 April 2015

Interview Questions & Answers for Prism WPF



Interview  Questions & Answers for Prism WPF

1. Define PRISM

This a framework used for developing complex applications for WPF, Silverlight or Windows Phone. The framework utilizes MVVM, Command patterns, DI, and Separation of Concerns to get loose coupling.

2. What are the benefits of PRISM?

Modular development: - As we are developing components as independent units we can assign these units to different developers and do modular parallel development. With parallel development project will be delivered faster.

High reusability: - As the components are developed in individual units we can plug them using PRISM and create composed UI in an easy way.


3. What is Bootstarpper?

   Bootstrapper is a class starts the shell. For example bootstrapper for a prism MEF application
   which inherits from MefBootstrapper.

Example

 class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void InitializeShell()
        {
            Application.Current.MainWindow.Show();
        }
    }

4. Is PRISM a part of WPF?
    No, PRISM is a separate installation.

5. Does it provide dependency injection? Does it relate to MEF at all in this way?

Yes. It originally included Unity, but the latest release includes using MEF for DI.


6. What is Module?


    Modules are classes that implement the IModule interface. A module in Prism is simply a loosely coupled functional unit in form of a class library project that typically represents a set of related concerns and includes a collection of related components, such views, view models, models and other classes.

Example

[Module(ModuleName = "TestModule", OnDemand = false)]
[ModuleDependency("ModuleDockModule")]
public class TestModuleModule : IModule
{ ...


7. How many ways modules can be registered?


   Modules can be registered from  -
  • Directly inside code
  • Using Configuration
  • Using Directory Inspection

8. What is Module Dependency


   Module can have dependency on other module. Prism provides module dependency management.
We need to set DependsOn property in a ModuleInfo class instance before adding module. DependsOn requires a collection of module names.
Example: 

[Module(ModuleName = ModuleNames.ModuleA)]    
[ModuleDependency(ModuleNames.ModuleD)]
public class ModuleAModule : IModule
{


9. What is Container or Dependency Injection Container?

   Core Prism library is container agnostic. The following are examples of containers that are used as
   dependency injection containers in Prism.
  • ModularityWithMef
  • ModularityWithUnity 



10. How do you register modules?
    Bootstrapper override CreateModuleCatalog and ConfigureModuleCatalog methods.


11. By default does MEF create singleton instance of objects?
   Yes, by default MEF creates singleton instance.

12. How to specify so that MEF does not create singleton instance?

    Decorate with attribute to indicate that the created instance is not shared.
    [PartCreationPolicy(CreationPolicy.NotShared)]



Happy Programming!!
Don’t forget to leave your feedback and comments below!


Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------