viernes, 30 de agosto de 2013

Lightswitch - Open a Web Page

This code show how to open an external web page. Evaluating if is an Desktop or Web Apllication.


using System.Runtime.InteropServices.Automation;
using System.Windows.Browser;
using Microsoft.LightSwitch.Client;
using Microsoft.LightSwitch.Threading;

var uri = new Uri("http://google.com", UriKind.RelativeOrAbsolute);
Dispatchers.Main.BeginInvoke(() =>
{
   try{              
          // If is a Desktop application
           if (AutomationFactory.IsAvailable){
                 var shell = AutomationFactory.CreateObject("Shell.Application");
                 shell.ShellExecute(uri.ToString());
            }
          // If is a Web application
             else if (!System.Windows.Application.Current.IsRunningOutOfBrowser){
                  HtmlPage.Window.Navigate(uri, "_blank");
            }
             else{
                  throw new InvalidOperationException();
             }

   }
   catch (Exception ex){
      //handle the exception however you want  
   }
});


In case you want to open in the same page, use:

                  HtmlPage.Window.Navigate(uri, "_self");

No hay comentarios:

Publicar un comentario