Monday, August 15, 2011

Changing Application.RootVisual at runtime

If you are a Silverlight Island programmer, this will not applicable to you because you will never face a situation which demands to change the Application.RootVisual property. Don’t confuse the word Island programmer ,it simply refers to the programmers who are creating small sized Silverlight applications mainly with some animations to fit into a small portion of HTML page which may be static or emitted by technologies such as ASP.Net or PHP.

Sometimes I feel that the Silverlight is more suited to Island programs than full window business apps. There are so many reasons.One is its rich user interface capabilities.This is more needed for glazy island apps than business apps. When we talk about great user experience there is always some cost attached to it which reduces performance. The performance is more important when we come to business apps than the user interface. In some of the tests we could see that the UI rendering takes more time than the WCF service in the whole display cycle. This happens when the data elements are huge in number but small and has complex data templates.

Ok.Coming back to changing Application.RootVisual multiple times at runtime. There may be scenarios where you need to show your authentication UI first and then load your application XAPs and the actual UI pages. In this case you need to change the RootVisual for sure.But this is not supported by the Silverlight runtime. First of all if you simply change, it will not change.You will get the below exception when you try to set null first and assign another visual after that.

Invalid value set for application's RootVisual

ie the below code will result into System.InvalidOperationException' in System.Windows.dll

'This will result in System.InvalidOperationException occurred in System.Windows.dll
Application.Current.RootVisual = Nothing
Application.Current.RootVisual = New TextBox With {.Text = "changed"}




So what is the simple solution.Keep a ContentControl as your RootVisual and set your Login UI and Application UI pages as its content.In the Application_Startup event set a ContentControl as your RootVisual then add initial UI to that. Later cast the RootVisual as ContentControl and change its Content property.


Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
'Initial UI.Here its button
Dim btnChangeRootVisual As New Button() With {.Content = "Change RootVisual"}
AddHandler btnChangeRootVisual.Click, AddressOf btnChangeRootVisual_Click

'ContentControl as RootVisual
Me.RootVisual = New ContentControl() With {.Content = btnChangeRootVisual}
End Sub
Private Sub btnChangeRootVisual_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
'Get the ContentControl and assign new UI to its content property
Dim rootContentControl As ContentControl = DirectCast(Application.Current.RootVisual, ContentControl)
rootContentControl.Content = New TextBox With {.Text = "changed"}
End Sub




This is just a hack for real business application programmers of Silverlight.

Thursday, December 30, 2010

TestComplete now supports Silverlight 4

Today I got chance to work with TestComplete to automate a Silverlight application.

http://www.automatedqa.com/blogs/post/10-12-09/TestComplete-8-10-Enhanced-Support-for-Silverlight-4-Applications.aspx

If you need to get the support for Silverlight object recognition you need to patch your xap with a TestComplete utility. The patch just adds 2 dlls into your xap and updates the AppManifest.xaml file.

We have an application which uses same code base for WPF as well as Silverlight .Hope our WPF test scripts will execute for testing Silverlight version as well.