Pages

C3 Health Services

Visit Official Website 9278982994

Expert Healthcare at Your Doorstep

Monday, 6 May 2013

Difference Between Dependency Property and Normal CLR Property in WPF

Difference Between Dependency Property and Normal CLR Property in WPF

The Normal CLR property and the dependency property look quite similar but the dependency property is more powerful and has more features. In WPF, dependency properties are used in Animation, Styles, Triggers, Templates, Validation, Data Binding, Layout etc.

Syntax Difference Between Dependency Property and Normal CLR Property

Syntax of Normal CLR Property

private int count;
public int Count
{
   get
   {
      return count;
   }
   set
   {
      count = value;
   }
}

Syntax of Dependency Property

public static DependencyProperty PageSizeProperty =
DependencyProperty.RegisterAttached("PageSize",
typeof(int), typeof(AttachedPropertySample),
             new PropertyMetadata(25,
             new PropertyChangedCallback(OnPageSizePropertyChanged)));

public int PageSize
{
    get
    {
        return (int) GetValue(PageSizeProperty);
    }
    set
    {
        SetValue(PageSizeProperty, value);
    }
}

In Built Change Notification

Dependency provides change notification when its value has been changed. You can specify Call Back while registering dependency property so user will get notification. This is mainly used in Data Binding. But CLR property has not any inbuilt change notification. So if you want your normal CLR proprety also notify changes, you have to implement INotifyPropertyChanged.

Value Resolution

CLR property reads value directly from private member while dependency property dynamically resolves value when you call GetValue() method of dependency property. The GetValue and SetValue methods are inherited from Dependency Object.

Value Inheritance

If you specify dependency property to top element it will be inherited to all child elements until child element specifically override the property. Dependency property value is resolved at runtime when you call GetValue() method. 

Suggestion: If your property will commonly be the source of a databinding (e.g. providing the Text for a TextBlock), I would recommend using a standard CLR property and having the containing class implement INotifyPropertyChanged.

Saturday, 4 May 2013

What is the difference between WPF and Silverlight?


What is the difference between WPF and Silverlight?

This article focuses on the difference between Silverlight and WPF technologies. Silverlight and Windows Presentation Foundation (WPF) both allow you to develop rich user experience applications based on XAML and the .NET Framework. For a period of time, Silverlight was called WPF/E, with the ‘E’ standing for ‘everywhere’. But there are some differences between the two technologies. Lets try to find out.

1. WPF is meant for developing rich applications for desktop platform while Silverlight is meant for developing rich browser based internet applications.

2. WPF is essentially the replacement to Winforms while Silverlight was introduced to compete with Adobe Flash.

3. Silverlight is a sub set of WPF in terms of features and functionality. WPF is based on the desktop CLR which is the full version of the CLR. Silverlight is based on a much smaller and more compact CLR which provides a great experience but does not have the full breadth of CLR features but only CoreCLR features.

4. You can use Silverlight applications regardless of the operating system you use, while WPF applications are restricted to later versions of the Windows operating system. For a WPF browser application, you need the .NET Framework to be installed in the client location while for Silverlight, you need only the plug-in. So in other words, WPF browser applications are OS dependent while Silverlight is not. A Silverlight plug-in can run in OSs other than Windows while we all know the .NET Framework only runs in Windows.

5. WPF supports 3 types of routed events (direct, bubbling, and tunneling). Silverlight supports direct and bubbling only.

Difference between Window and Page Controls in WPF?


Difference between Window and Page Controls in WPF?

In this article on "Difference between Window and Page Controls in WPF", I will try to explain the difference between window and page controls used in WPF. Both controls have different usage depending on different requirements of the application. Lets try to understand this difference.

1. Window Control (Window.xaml) are used for Windows Application while Page Control (Page.xaml) is used for making Browser Hosted Applications. Page can be implemented as root element in Xaml file and can contain single element similar to window.

<Page x:Class="WpfApplication1.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Page1">
      Welcome to WPF Page Navigation.
</Page>

Similar to normal xaml file you can specify page name StartupUrl inside App.xaml to open specific page.

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Page1.xaml">
</Application>

2. Page Control can be contained in Window Control but vice versa is not possible

You can use Page control within the Window control using NavigationWindow and Frame controls. Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications. So if you host Page in NavigationWindow, you will get the navigation implementation built-in. Pages are intended for use in Navigation applications (usually with Back and Forward buttons, e.g. Internet Explorer). 

WPF provides support for browser style navigation inside standalone application using Page class. User can create multiple pages, navigate between those pages along with data.There are multiple ways available to Navigate through one page to another page.

Frame class provides inbuilt support NavigationFramework. Let’s have a look on below code.

<StackPanel>
    <TextBlock Margin="10">This is Main Window.</TextBlock>
    <Frame Margin="10"
        Source="Page1.xaml"
            JournalOwnership="OwnsJournal"></Frame>
</StackPanel>

Thursday, 2 May 2013

When and Why to Use UNITY Framework in .NET? Inversion of Control (IOC) and Dependency Injection(DI)

When and Why to Use UNITY Framework in .NET? Inversion of Control (IOC) and Dependency Injection(DI)

UNITY Framework is a framework of Microsoft to achieve Inversion of Control / Dependency Injection Design Pattern. IOC is a principle while DI is a way of implementing IOC. Unity is described as a lightweight, extensible dependency injection container. Lets see when and why to use Unity Framework?

UNITY Framework removes Tight Coupling of Classes and Objects and make them loosely coupled. UNITY Framework facilitates the design and implementation of loosely coupled, reusable, and testable objects in your software designs by removing dependencies. Using dependency injection will make the application  more flexible to changes and more maintainable.  Instead of compile time dependencies, UNITY Framework offers runtime loading and initialization of components, which makes solutions load on demand.

Example of Dependency and Tight Coupling

Consider the below example. We have a Customer class which contains an Address class object. The biggest issue with the code is tight coupling between classes. In other words the customer class depends on the address object. So for any reason address class changes, it will lead to change and compiling of Customer class also. So, if for any reason the address object is not able to create, the whole customer class will fail in the constructor initialization itself. 

public class Customer
{
      private Address address;
      public Customer()
      {
           address = new Address();
      }
}

Ways to implement Dependency Injection

In DI, we have four broader ways to implement Dependency Injection:

1. Constructor way
2. Exposing setter and getter
3. Interface implementation
4. Service locator


Why to use Unity?

1. It provides simplified object creation, especially for hierarchical object structures and dependencies, which simplifies application code.

2. It supports abstraction of requirements; this allows developers to specify dependencies at run time or in configuration and simplify management of crosscutting concerns.

3. It increases flexibility by deferring component configuration to the container.

4. It has a service location capability; this allows clients to store or cache the container. This is especially useful in ASP.NET Web applications where developers can persist the container in the ASP.NET session or application.

When to Use Unity?

1. You have dependencies between objects.

2. The dependencies are complex and you need a way to abstract them.

3. You want to be able to change your dependencies at runtime.

4. You want to use constructors, properties or method calls injections.


Further Reading: 

http://www.c-sharpcorner.com/uploadfile/questpond/design-pattern-inversion-of-control-and-dependency-injection/
http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/inversion-of-control/
http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/unity-framework/
http://msdn.microsoft.com/en-us/library/ff649614.aspx

How to count number of data members in a C# class at runtime?

How to count number of data members in a C# class at runtime?
 
While development of one of my C# .NET project, I felt the need to count the number of data members at runtime and do something with them. I used typeof(ClassName).GetFields().Length to solve the problem like following.
 
using System;

public class Test
{
        public static void Main()
        {
                var count = typeof(ABC).GetFields().Length;
                Console.WriteLine(count);
        }
}

static class ABC
{
  public static int A;
  public static int B;
  public static int C;
}
 
If you have any better approach to solve the above problem, please update me.

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.