The first step is to create a LightSwitch project.
To create a LightSwitchproject:
- On the Menu Bar in Visual Studio:
- choose File
- choose New Project
- In the New Project dialog box
- select the LightSwitch node
- then select LightSwitch Application (Visual Basic)
- in the Name field, type AddUrlToNavigationMenu as the name for your project
- click the OK button to create a solution that contains the LightSwitch project
Next, we need to add a screen.
To add the screen:
- In the Solution Explorer in:
- right-click the Screens folder
- select Add Screen (see figure 1)
- select New Data Screen
- use LaunchURL as the Screen Name
- leave Screen Data set to (none)
- click OK
Figure 1. Add new screen
- A new item has now been added to the Navigation Menu (see Figure 2)
Figure 2. LaunchURL screen added
Now we need to add a bit of code to the screen, tomake it behave the way we want, that is to display a web page, instead ofdisplaying the screen as it usually would.
To add the code:
- We're going to need a reference to System.Windows.Browser, so let's do that now.
In Solution Explorer:
-
- click the View button (see Figure 3)
- select File View
Figure3. File view
-
- right-click the Client project
- select Add Reference
- find and click System.Windows.Browser (see Figure 4)
- click OK
Figure 4. Adding System.Windows.Browser
- Switch back to Logical View
- click the View button (see Figure 5)
- select Logical View
Figure 5. Selecting Logical view
- In the Screen Designer (see Figure 6):
- click the Write Code dropdown
- select LaunchURL_Run
Figure 6. Selecting LaunchURL_Runmethod
- Add the following code into LaunchURL_Run (see Listing 1):
Please Note: The Option Strict Offoption isrequired for the line that executes the Shell.Applicationobject.
Listing 1. LaunchURL_Run method code
C#Visual Basic
Edit|Remove
using System.Runtime.InteropServices.Automation;
using System.Windows.Browser;
using Microsoft.LightSwitch.Client;
using Microsoft.LightSwitch.Threading;
namespace LightSwitchApplication
{
public classApplication
{
privatevoid LaunchURL_Run(ref bool handled)
{
//change this to launch the page you want
varuri = new Uri("http://www.lightswitchcentral.net.au",UriKind.RelativeOrAbsolute);
Dispatchers.Main.BeginInvoke(() => {
try
{
if (AutomationFactory.IsAvailable)
{
var shell =AutomationFactory.CreateObject("Shell.Application");
shell.ShellExecute(uri.ToString());
}
else if (! System.Windows.Application.Current.IsRunningOutOfBrowser)
{
HtmlPage.Window.Navigate(uri, "_blank");
}
else
{
throw newInvalidOperationException();
}
}
catch (Exception ex)
{
//handle the exception however you want
}
});
//Set handled to 'true' to stop further processing.
handled = true;
}
}
}
Option Strict Off
Imports System.Runtime.InteropServices.Automation
Imports System.Windows.Browser
Imports Microsoft.LightSwitch.Client
Imports Microsoft.LightSwitch.Threading
Namespace LightSwitchApplication
Public ClassApplication
PrivateSub LaunchURL_Run(ByRef handled As Boolean)
'change this to launch the page you want
Dimuri = New Uri("http://www.lightswitchcentral.net.au",UriKind.RelativeOrAbsolute)
Dispatchers.Main.BeginInvoke(
Sub()
Try
If AutomationFactory.IsAvailable _
Then
Dim shell =AutomationFactory.CreateObject("Shell.Application")
shell.ShellExecute(uri.ToString())
ElseIf (Not System.Windows.Application.Current.IsRunningOutOfBrowser) _
Then
HtmlPage.Window.Navigate(uri, "_blank")
Else
Throw NewInvalidOperationException()
End If
Catch ex As Exception
'handle the exception however you want
End Try
End Sub)
'Set handled to 'true' to stop further processing.
handled = True
End Sub
End Class
End Namespace
The first screen that gets added to a LightSwitchproject become the startup screen. If we leave our screen set as thestartup screen, it'll launch the web page as soon as the application runs.
So we need to fix that.
To prevent our screen fromautomatically running:
- In Solution Explorer:
- right-click the Screens folder
- select Edit Screen Navigation (see Figure 7)
Figure 7. Edit screen navigation
-
- click on the LaunchURL screen to select it
- click the Clear button
- notice that our screen is no longer the startup screen (see Figure 8)
Figure 8. No more startup screen
All that's left to do now, is to run the application.
If you've found this sample helpful, or even if youhaven't, please rate it, & feel free to give meany feedback on it.