This article explains global variables in LightSwitch Applications (Visual C#) in Visual Studio 2012.
The following is the procedure for using global variables in LightSwitch 2012.
Step 1
Open the Solution Explorer.
Step 2
In the Solution Explorer, right-click on the Screens and choose "Add Screen".
Step 3
The Add New Screen dialog box appears. Select the "New Data Screen" from the Screen Template, under screen information, choose "None" under screen data and provide some name to the Screen and click the "OK" button.
The Screen Designer appears.
Step 4
In the same manner we will add another New Data Screen.
Step 5
In order to add a global variable in "application.cs", right-click on the project and choose "Properties".
Step 6
Go to the Screen Navigation and click on the link "Click here to view application code".
Add a property to the "Application.cs" file.
using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
public partial class Application
{
public string varglobal
{
get;
set;
}
}
}
Step 7
In order to set the global variables for CreateScreen1, open the screen designer for CreateScreen1 and click on "Write Code". A dropdown list will appear; choose the _created() method from the list.
The code designer for CreateScreen1 appears.
using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
public partial class CreateScreen1
{
partial void CreateScreen1_Created()
{
// Write your code here.
Application.varglobal = "Global Variable Set in CreateScreen1"; // Here we are setting the value of Global Variable
Application.ShowCreateScreen2(); // Here we are redirecting the screen to CreateScreen2
}
}
}
Step 8
In order to get the global variables for CreateScreen2, open the screen designer for CreateScreen2 and click on "Write Code". A dropdown list will appear and choose the "_created()" method from the list.
The code designer for the CreateScreen2 appears.
using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
public partial class CreateScreen2
{
partial void CreateScreen2_Created()
{
// Write your code here.
this.ShowMessageBox(Application.varglobal); // displaying the value of global variable
}
}
}
Step 9
Press F5 to run the application.