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

Monday, 25 May 2015

Crystal report shows a blank page in ASP.net and Visual Studio 2012


Crystal report shows a blank page in ASP.net and Visual Studio 2012


 Hi Friends,

In my last project I was working on a one crystal report project and I am too much frustrated with crystal report with Visual Studio 2012.  When I run my report. It displays a vacant page in a Browser,instead of a report. . Sometimes asp.net web page shows blank page although it is consist of Crystal report viewer.
Cause of Problem
  • Using client tools (debug window of your browser) or server tool (IIS log) you will find that some files (crv.js , style.css ) are not served;
  • these files are placed by CR installer in wwwroot\aspnet_client folder but they cannot be reached;
  • If you installed your application in a website different from Default WebSiteaspnet_clientfolder is not placed inside that website
Solution
The solution is to work on IIS this way:
  • Copy aspnet_client folder from c:\inetpub\wwwroot folder to the new website root folder.
or

  • Create a virtual directory called aspnet_client that points to c:\inetpub\wwwroot inside the new website

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

Action, Func and Predicate Delegate in C#



Action, Func and Predicate Delegate in C#

Delegate is a very powerful feature available in the .NET Framework. In this article, we will explore delegate & its new features which are being introduced in the framework. I will be using Visual Studio 2013 for coding sample code, assuming that you do have basic knowledge of .NET C# code.
I will be explaining the following flavors in these articles:

Action
Ø  Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.
Ø  When you want a delegate for a function that may or may not take parameters and does not return a value.
Ø  Action is more commonly used for things like List<T>.ForEach: execute the given action for each item in the list..
Ø  I use these often for anonymous event handlers:
Ø  Example
                    button1.Click += (sender, e) => {/* Do Some Work */}

Func

  •  Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).
  •  When you want a delegate for a function that may or may not take parameters and returns a value.
  • The most common example would be Select from LINQ:
  • Example

                    var result = someCollection.Select( x => new { x.Name, x.Address });

Predicate is a special kind of Func often used for comparisons.

  •  Predicate is a delegate that takes generic parameters and returns bool
  • When you want a specialized version of a Func that takes evaluates a value against a set of criteria and returns a boolean result (true for a match, false otherwise).
  • Though widely used with Linq, Action and Func are concepts logically independent of Linq. C++ already contained the basic concept in form of typed function pointers.
  •   Predicate<T> is a delegate that takes a T and returns a bool.
    It's completely equivalent to Func<T, bool>.
  • Predicate is just a special cased Func<T, bool> really, introduced before all of the Func and most of the Action delegates came along
  • Predicate is mostly used in List<T> for methods like FindAll and RemoveAll.



 
Here is a small example for Action and Func without using Linq:


class Program
{
    static void Main(string[] args)
    {
        Action<int> myAction = new Action<int>(DoSomething);
        myAction.Invoke(123);           // Prints out "123"

        Func<int, double> myFunc = new Func<int, double>(CalculateSomething);
        Console.WriteLine(myFunc(5));   // Prints out "2.5"
    }

    static void DoSomething(int i)
    {
        Console.WriteLine(i);
    }

    static double CalculateSomething(int i)
    {
        return (double)i/2;
    }
}


Conclusion:

The difference between Func and Action is simply whether you want the delegate to return a value (use Func) or doesn't (use Action).


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