Showing posts with label Controls. Show all posts
Showing posts with label Controls. Show all posts

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.

Saturday, April 24, 2010

Silverlight 4 RichTextBox lab

Got the below lab when I was looking for the usage of RichTextBox in Silverlight 4.This gives an idea how to use RichTextBox.

http://channel9.msdn.com/learn/courses/Silverlight4/NewFeatures/RichTextBox/

More labs here

Friday, January 9, 2009

Download source code of Silverlight 2 runtime and controls

http://www.microsoft.com/downloads/details.aspx?FamilyID=EB83ED4C-AC85-4DE9-8395-285628EE2254&displaylang=en

Reduce your time on .net reflector...

Happy Coding..

Friday, April 11, 2008

Styling and Templating in Silverlight 2.0

There is no easy method to edit Styles or Templates of Silverlight 2.0 controls eventhough there is Style and Templating support.The actual problem is we can't find out the default Style or Template in ordre to edit.In WPF applications it's possible by opeing Application in Expression Blend.But we should alter the templates in order to get full strength Silverlight .ie the GREAT next generation look and FEEL.

Here you can found one tool which displays default style and template of Silverlight controls.just copy the style from the tool then alter and apply to your controls and have the power of Templating and Styling.
Hope in future versions of Blend there would be support to edit templates as we are doing for WPF application.Anyway thanks to Delay for providing such a tool.

The default styles are available in MSDN also.

Wednesday, April 9, 2008

Multiline TextBox in Silverlight 2.0

Its set of workarounds only.There is no support in silverlight 2.0 to have a multiline textbox.But we should accomplish as we might be in deadline :-).It leads to workarounds.
What I did ,was to set AcceptsReturn property of TextBox.Now it will accept Returns ie EnterKey and create one more line by increasing its' Height.
Now the next problem came.ScrollBars..
I tried setting ScrollViewer.VerticalScrollBarVisibility="Visible".But it gave me runtime error.(Dont know why VS2008 showed intellisense to add ScrollViewer.VerticalScrollBarVisibility="Visible").
Again workaround.Put this TextBox in a ScrollViewer.
Finally it looks like




<ScrollViewer Height="100" Width="100" HorizontalScrollBarVisibility="Visible">

<TextBox Text="Javascripting Silverlight 2.0"

AcceptsReturn="True"

/>

ScrollViewer>



Sample located here

Thursday, March 13, 2008

Silverlight 2.0 Class / Control hierarchy

This diagram helps very much to design our classes.ie from which class we have inherit
Find it here http://advertboy.wordpress.com/2008/03/13/silverlight-control-hierarchy/