Change Application Title Programmatically in LightSwitch/Silverlight

May 5, 2012  |  LightSwitch, Silverlight

When running Out-Of-Browser, the System.Windows.Application class surfaces the MainWindow.Title in Silverlight 5 (and thus LightSwitch for Visual Studio 11).  For In-Browser in either Silverlight 4 or 5 (and LightSwitch 2011), you can use the DOM bridge to set the page title.

if (System.Windows.Application.Current.IsRunningOutOfBrowser)
  System.Windows.Application.Current.MainWindow.Title = title;
else
  System.Windows.Browser.HtmlPage.Document.SetProperty("title", title);

 

For LightSwitch for Visual Studio 11 apps, right-click on your LS project in the solution explorer and choose “View Application Code (Client)” and add this method:

public void SetTitle(string title)
{
  Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
  {
    if (System.Windows.Application.Current.IsRunningOutOfBrowser)
      System.Windows.Application.Current.MainWindow.Title = title;
    else
      System.Windows.Browser.HtmlPage.Document.SetProperty("title", title);
  });
}

For LightSwitch 2011 apps, you can only do this In-Browser. Also you’ll need to add a reference to System.Windows.Browser in your client subproject.

public void SetTitle(string title)
{
  Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
  {
    if (!System.Windows.Application.Current.IsRunningOutOfBrowser)
      System.Windows.Browser.HtmlPage.Document.SetProperty("title", title);
  });
}

Now you can call this.Application.SetTitle(“My New Title”); from any of your screens.


2 comments on “Change Application Title Programmatically in LightSwitch/Silverlight

  1. Dave Vorgang on said:

    Thanks so much. I’ve been wanting to change the title for almost a year now.

    Your a jewel.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

HTML tags are not allowed.