Few Differences between WPF and Silverlight

19. April 2011

 

  1. WPF is a thick Windows client platform, SilverLight is a browser based plug-in. WPF is built on top of .Net Framework and that has access to the full .Net Framework APIs. Silverlight has access to only a subset of the .Net Framework (called the CoreCLR). 

  2. Silverlight is just a subset of WPF.

  3. Silverlight is meant to for web based application (that can run out of browser), while WPF is for desktop applications.

  4. Silverlight is compatible with multiple browsers, devices and operating systems, bringing a new level of interactivity wherever the Web works.

  5. Within WPF, all visually rendering elements derive from the 'Visual' class. Within Silverlight, they do not; Both technologies, however, eventual derive from the DependencyObject class up the hierarchy.

  6. WPF supports 3 types of routed events (direct, bubbling, and tunneling). Silverlight supports direct and bubbling only (so there is no Preview___ events).

  7. There's quite a few data-binding differences. Currently, Silverlight doesn't support the binding mode, OneWayToSource. In addition, Silverlight defaults to OneWay databinding if none is set, while WPF uses the default mode specified by the dependency property.

  8. Silveright doesn't support MultiBinding.

  9. Silverlight supports the XmlDataProvider but not the ObjectDataProvider. WPF supports both.

  10. Silverlight supports browser interop, more media streaming options including timeline markers, and Deep Zoom. WPF doesn't support these features yet.

  11. Silverlight 5(Beta) got XAML debugging support, which is not available in WPF.

Silverlight, wpf ,

Debug bindings in XAML with Breakpoints

19. April 2011

    In Silverlight 5 (Beta), one of the most interesting and important enhancement is the ability to set breakpoints in XAML and debug bindings. Before Silverlight 5 (and in WPF), debugging the binding is a difficult task, there is no direct way to put debug code.

Following are the options that we use to debug the binding errors.

  1. Look at the output window for all tyeh errors and try to resolve.
  2. Write dummy converters and attach to all the bindings. and set breakpoint inside the converter.

The binding xaml breakpoints in Silverlight 5 is a more flexible, powerful, and saves the time spend in debugging.

But the sad part is that WPF is not getting this ability to set breakpoint in XAML in the next version too. Pete Brow, Developer Division Community Program Manager at Microsoft mentioned this in his post.

He also mentioned that, "While from the outside they're very similar, the Silverlight and WPF codebases are very very different. It's actually a ton of work to implement some things in WPF due to those differences. In fact, most of Silverlight is native code whereas much of WPF is managed. If it were just an easy imp, the team would have done it.".

So, We have to wait till WPF 6(?) to get the same featuter.

.Net Tips, wpf , , ,

WPF : Not Using StringFormat when the data is null.

21. March 2011

 

When you are using StringFormat in XAML, if the data is null, the oputput will be just the format text.

For example, consider the following example.

 

<TextBlock Text="{Binding Path=Amount, StringFormat=Total: {0:C}}" />

 

When the Amount is null, the output of the above binding will be "Total:" This doesn't makes any sense. So, most of teh cases, you have to hide the text or display alternative text. You can achive this with converters (IValueConverter).

There is another simple way to do it without writing any C# code using TargetNullValue. Here is how you can achieve the result.

<TextBlock Text="{Binding Path=Amount,  
TargetNullValue={x:Static System:String.Empty},  
StringFormat=Total: {0:C}}" />

Note:-

If you are using TargetNullValue in Binding, if the value is null, it will ignore the StringFormat. In some cases, you might want to display the Text with formatted string. In this case, you cannot use the above mentioned method. you have to implement your converter.

.Net Tips, .Net, wpf , ,

What is MVVM? and Why MVVM?

2. March 2011

 

 

What is MVVM?

    Model-View-ViewModel (aka Presentation Model), provides architectural solution for the Aplication with UI with complex interdependent interactions.

    Separate the UI(View) from the UI logic using a ViewModel that encapsulates the interactions and provides properties for a View to retrieve its state (and the state of its elements). 

 

Why MVVM?

1. Separates UI from Model. If model needs to change, it can without changing the view and vice versa.

2. Avoids duplicated code to update views.

3. Unit testable UI logic.

4. You can use a 'viewmodel' to aggregate parts of your model that exist in separate classes/libraries to facilitate a more fluent interface for the 'view' to deal with.

5. Data-binding support with INotifyPropertyChanged, ICommand and IvalueConverters

6. Easy to refactor

7. Extensibility.

 

interview questions, .Net, wpf ,

VS 2008 Disable XAML Design View and Always load in full XAML View

28. September 2010

    Most of the developers don't like the XAML design view in VS 2008 as it takes long time to load the desin view and some times it will crash the visual studio also.

The design view can be disabled in visual studio 2008.

1. Go to Tools-Options in menu.

2. Select Text Editor -> XAML -> Miscellaneous node in the popup.

3. Check the 'Always open documents in full XAML view' checkbox.

 

Even if this is not improving load time , try this option.

Hope It helps.

 

.Net Tips, .Net, wpf , ,

WPF FrameworkElement Width, MinWidth, MaxWidth and ActualWidth

17. September 2010

 

There are 3 Properties which specifies the Width Information for FrameworkElement (parent class for WPF Controls). Width , MinWidth and MaxWidth. As the name says, With Property specifies the Width of the Element and MinWidth and MaxWidth specifies the constraints for the Width of the element. If there is any conflict in these values, MinWidth takes higher priority and then MaxWidth, and finally if both of these values are within bounds, Width. If you Apply all these properties and if you want to check the rendered Width, then you can use ActualWidth Property (which is read only property).

In short, 

 

  • Width is the requested Width and MinWidth, MaxWidth are constraints for the Width.
  • ActualWidth is the rendered size. 

 

The same Applies to Height, MinHeight, MaxHeight and ActualHeight Properties.

 

Here is Example, 

Scenario 1:

If you set Width = 75, MinWidth = 25, MaxWidth = 100

and the calculated width (ActualWidth) = 75 (Width value is used as the value set for Width is withing the bounds of Min and Max Widths)

 

Scenario 2:

if you set Width = 25, MinWidth = 50, MaxWidth = 75, 

and the calculated width (ActualWidth) = 50 (Min Width takes precedence as the requested width is smaller than Min Width)

 

Scenario 3:

if you set Width =100, MinWidth = 50, MaxWidth = 75, 

and the calculated width (ActualWidth) = 75 (Max Width takes precedence as the requested width satisfies Min Width and is bigger than Max Width)


Hope this helps.

 

.Net Tips, Technical, wpf , , , ,

ScrollViewer default to HorizontalScrollBarVisibility and VerticalScrollBarVisibility values

17. September 2010

 

If you check the WPF(as well as Silverlight) ScrollViewer's default values for HorizontalScrollBarVisibility (Disabled) and VerticalScrollBarVisibility (Visible). 

I was curious to know Why does the ScrollViewer default to HorizontalScrollBarVisibility Disabled and VerticalScrollBarVisibility Visible, and found this page which explains the reason.

"For compatibility with the WPF defaults, of course. :) Regarding the obvious next question about why those values are the WPF defaults, I don't know. Auto/Auto would seem more generally useful to me and, in fact, the WPF ListBox (and Silverlight ListBox) overrides the ScrollViewer defaults to set Auto/Auto! But maintaining compatibility wins, so the Silverlight ScrollViewer defaults to the same values used by WPF."

 

In most of our application we need HorizontalScrollBarVisibility and VerticalScrollBarVisibility to be Auto by default. To achieve this behaviour, we can add teh following style in your applications style.

 

<Style TargetType="{x:Type ScrollViewer}">

            <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>

            <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>

      </Style>

 

 

.Net Tips, Technical, wpf , , ,

WPF TargetNullValue Binding Property

25. August 2010

    .net fromework SP1 introduced a property in BindingBase called TargetNullValue.

Usage : 

<TextBox Text="{Binding Total, TargetNullValue=0.00}" />

    The above code will set the Total property to null if the value is entered is 0.00 and if Total property is null it will display 0.00. The same functionality can be achieved by checking for null in the property getter and setters, but this is very handy and you don't have to do it in all the places where you want to achieve this behaviour.

For, more information about this property, check http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue.aspx .

 

.Net Tips, Technical, wpf , ,

Get active Window on WPF

24. August 2010

 

We can scan the list of open windows in the application and check which one of them has IsActive = true:

Application.Current.Windows.Cast<Window>().SingleOrDefault(x => x.IsActive);

If you want to get teh active window in many places , you might want to create extension method for getting active window.

public static Window GetActiveWindow( this Application currentApp)

        {

            return Application.Current.Windows.Cast<Window>().SingleOrDefault(x => x.IsActive);

        }

and you can get the active window using the following code

Application.Current.GetActiveWindow();

 

Update:

Note that the Cast<> method is a Linq method, you need to reference System.Linq namespace.

Technical, wpf ,

Visual Studio crashing on XAML Design view

4. August 2010

    If you're working with XAML in Visual Studio 2008 (SP1), either for WPF or Silverlight, you might have encountered the visual studio crashing problem when opening XAML in design view.

    The first thing to do is to get rid of the design view, if you dont need it frequently. The design view takes long time to open if XAML is complex and sometimes it is useless too.

If you try to edit the XAML file as an XML file, this will disabled IntelliSense, for some reason. So the XML editor was not an option.

alternatively, you can use the "Source Code (Text) Editor". It doesn't seem obvious based on its name, but this editor provides XAML IntelliSense, XML collapsing, and the XAML context menu.

To use this:

  • Right-click on a XAML file in the Solution Explorer
  • Select "Open With..."
  • Select "Source Code (Text) Editor"
  • Click on "Set as Default"
  • Click OK

done!

  If you want to use the default XAML editor (with its split view, navigator, etc.), you just have to select "View Designer" in the text editor's context menu or use SHIFT+F7.

 

.Net, wpf , ,