LightSwitch 添加Web导航

 

The first step is to create a LightSwitch project.

To create a LightSwitchproject:

  1. On the Menu Bar in Visual Studio:
    •  
    • choose File 
    • choose New Project
  2. 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:

  1. 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

  1. 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:

  1. 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

  1. Switch back to Logical View
    •  
    • click the View button (see Figure 5) 
    • select Logical View

Figure 5. Selecting Logical view

  1. In the Screen Designer (see Figure 6):
    •  
    • click the Write Code dropdown 
    • select LaunchURL_Run

Figure 6. Selecting LaunchURL_Runmethod

  1. 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

Copy 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:

  1. 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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值