LightSwitch Trick: Display Loading Message while a Screen is Loading
In the LightSwitch apps that I create, I have a common pattern where I display a “Loading” message in the title bar of a screen while it is loading and then set it to its actual screen name once it’s finished. In particular, this is useful for screens whose title is dependent on the data that is loaded by the screen, such as a customer detail screen whose title is the name of the customer.
This is just a nice little tweak to the UI that’s quite simple to implement. Here’s how:
Ensure that focus is set to your screen’s content tree and choose the InitializeDataWorkspace event from the Write Code dropdown button:
In the method stub that gets generated, add the following code (replacing CustomerDetail with your actual screen name):
C#:
partial void CustomerDetail_InitializeDataWorkspace(
List<IDataService> saveChangesTo)
{
this.DisplayName = "Loading...";
}
VB:
Private Sub CustomerDetail_InitializeDataWorkspace(saveChangesTo _
As System.Collections.Generic.List(Of Microsoft.LightSwitch.IDataService))
Me.DisplayName = "Loading..."
End Sub
And that’s all you have to do if you are using a Details screen template. That’s because for a Details screen, LightSwitch automatically generates code for the entity property’s Loaded event to update the display name based on the entity’s data.
The InitializeDataWorkspace event is used because that’s the very first event that the screen raises. So within that event, the screen’s display name is immediately set to “Loading…”. Then, once the data is loaded, code runs in the Loaded event that explicitly sets the screen’s display name to the desired value.
If you wanted to apply this technique to a non-Details screen, you would do the same thing as above but additionally you would need to implement the Loaded event for one of your screen’s entity or collection properties that would set the DisplayName of the screen. For example, here’s how I’ve done it for my Customers Grid screen:
C#:
partial void Customers_Loaded(bool succeeded)
{
this.DisplayName = "Customers";
}
VB:
Private Sub Customers_Loaded(succeeded As Boolean)
Me.DisplayName = "Customers"
End Sub
The end result is that you see this while the screen is loading:
and this once it’s finished loading:
Comments
- Anonymous
February 15, 2012
Excellent! So simple, yet effective. Thanks