How to close a screen from another screen in Microsoft Ligthswitch?

This may sound simple but the solution code is not.  To close a screen from another screen you need to write three lines of code including one dispatcher.  The steps are as follows:

1. Create a variable IActiveScreen

Dim ChosenScreen As Microsoft.LightSwitch.Client.IActiveScreen

2. Query the active screens.  In my code sample below the screen name that I will be closing is sysTransaction.

ChosenScreen = Application.ActiveScreens.Where(Function(a) TypeOf a.Screen Is sysTransaction).FirstOrDefault()

3. Now make a dispatcher to execute an event.  Our event is to close the chosen screen.

ChosenScreen.Screen.Details.Dispatcher.BeginInvoke(Sub()                                                                  DirectCast(ChosenScreen.Screen, sysTransaction).Close(False)
End Sub)

That’s it.  By the way, I haven’t tried this code on multiple screen instances, since most of my screen are set to single instant only.

Furthermore you can also use the dispatcher to refresh the choosen screen.


4 thoughts on “How to close a screen from another screen in Microsoft Ligthswitch?

  1. VS202.2 this code is not working for me can you respond?
    Screen to close: PasswordRecords (Display Name is “Password Records”)

    Microsoft.LightSwitch.Client.IActiveScreen ChosenScreen = default(Microsoft.LightSwitch.Client.IActiveScreen);
    ChosenScreen = Application.ActiveScreens.Where((a) => a.Screen.DisplayName == “Password Records”).FirstOrDefault();

    1st: I had to modify the Lambda:
    // error: object does not contain a definiton for Screen and no extension method exists
    (System.Object a) => a.Screen is sysTransaction

    2nd: I have no idea how to rectify this error
    // error: The best overloaded match for BeginInvoke(System.Action) has some invalid arguments

    // My C# converts back to your VB as this…
    Dim ChosenScreen As Microsoft.LightSwitch.Client.IActiveScreen = Nothing
    ChosenScreen = Application.ActiveScreens.Where(Function(a) a.Screen.DisplayName = “Password Records”).FirstOrDefault()
    ChosenScreen.Screen.Details.Dispatcher.BeginInvoke(DirectCast(ChosenScreen.Screen, PasswordRecordEditor).Close(False))

    I tried the code in a screen called CreateNewPasswordRecord that needs to close the PasswordRecord screen while the actual record is being created. Furthermore I do not know which event handler in CreateNewPasswordRecord the code should be placed within. I assume CreateNewPasswordRecord_Activated() but your comments are what?

    Can you help me (us) figure this out?

    1. Below is my sample code to close all screen except for the screen with name is “Home”

      Dim screens = From sc In Application.ActiveScreens
      Where (sc.Screen.DisplayName “Home”)
      Select sc

      For Each sc In screens
      End Sub)

  2. Thank you for being –very– helpful as I’m sure closing screens is going to become increasingly common.

    The last provided snippet works in a VB project I created to test it out but get this, every code converter I could find on the WWW (four different code converters) mangles the VB at the where statement and they all completely whack the select statement out of existence. I guess they don’t do so well with LINQ.

    Once I add the select statement back and formatted the LINQ statement to what I thought should be the correct form the C# Intellisense whines about not knowing sc in the current context.

    For others who may be reading this the following is what the VB to C# converters generate from the last provided VB snippet noting I cleaned up the mangled formatting and added the select statement where it should be:

    () =>
    dynamic screens = from sc in Application.ActiveScreens
    where (sc.Screen.DisplayName == “Create New Password Records”)
    select sc;
    foreach (object sc_loopVariable in screens)
    sc = sc_loopVariable;

    I’m going to take the rest of this to the LightSwitch forums where there might be some C# developers that can figure out the correct LINQ. If there’s anything you can add… otherwise again, thank you for helping out and writing Microsoft’s documentation for them. lol

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s