locked
How to open new browser window from OOB? RRS feed

  • Question

  • I would like to open browser from OOB App.

    When I'm not running OOB I can open browser:
    System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("XXX"), "_blank");
    Monday, May 31, 2010 2:24 PM

Answers

  • Hi Lucky777,

    In a normal Silverlight application, you can open new web browser window using
    System.Windows.Browser.HtmlPage.Window.Navigate() method.
    However, this call will fail in Out of Browser mode since DOM bridge is disabled.

    Though here's a dirty alternative:

    public class MyHyperlinkButton : HyperlinkButton  

       2: {   

       3:     public MyHyperlinkButton(string navigateUri) 

       4:     {   

       5:          base.NavigateUri = new Uri(navigateUri);   

       6:          TargetName = "_blank";   

       7:     }

     

    then from code behind call :

       1: MyHyperlinkButton button = new MyHyperlinkButton("http://www.bing.com");   

       2: button.ClickMe(); 


    If this answers your question, please Mark as an answer. Thank you. 
    Monday, May 31, 2010 3:20 PM
  •  Hello,

    i hope these links may help you:

    http://csharperimage.jeremylikness.com/2010/05/silverlight-out-of-browser-oob-versions.html

    http://mokosh.co.uk/post/2009/10/08/silverlight-oob-open-new-browser-window/

    Monday, May 31, 2010 5:36 PM
  • Is it posssible to open a window from MainPage_Loaded or MainPage() methods?

    Nope, must be called from user initiated code.

    Wednesday, June 2, 2010 3:29 AM
  • Hi Lucky7777,

    sl.ayer is right. It must be called from user initiated code.
    (for security reason...)
    As I mentionned earlier, the scenario may work all right but
    it's mainly used as a workaround and ain't perfect.

    The solution was mainly built to work with
    a hyperlinkbutton as the class inherits from HyperlinkButton
    and expose ClickMe() method which can
    then call protected OnClick method of the base class:

    Sorry... It won't work with MainPage_Loaded or MainPage() methods

    Wednesday, June 2, 2010 9:30 AM

All replies

  • Hi Lucky777,

    In a normal Silverlight application, you can open new web browser window using
    System.Windows.Browser.HtmlPage.Window.Navigate() method.
    However, this call will fail in Out of Browser mode since DOM bridge is disabled.

    Though here's a dirty alternative:

    public class MyHyperlinkButton : HyperlinkButton  

       2: {   

       3:     public MyHyperlinkButton(string navigateUri) 

       4:     {   

       5:          base.NavigateUri = new Uri(navigateUri);   

       6:          TargetName = "_blank";   

       7:     }

     

    then from code behind call :

       1: MyHyperlinkButton button = new MyHyperlinkButton("http://www.bing.com");   

       2: button.ClickMe(); 


    If this answers your question, please Mark as an answer. Thank you. 
    Monday, May 31, 2010 3:20 PM
  • Very smart solution!

    But I think you mean

    base.OnClick();

    instead of

    base.ClickMe();

    Monday, May 31, 2010 4:00 PM
  • Hi MawashiKid,
    Interesting solution.

    How can I programmatically Click the button?

     
    I tried Automation, but it doesn't work
    HyperlinkButtonAutomationPeer hyperlinkButtonAutomationPeer = new HyperlinkButtonAutomationPeer(button);
    hyperlinkButtonAutomationPeer.RaiseAutomationEvent(AutomationEvents.InvokePatternOnInvoked);
    IInvokeProvider iprovider = (IInvokeProvider)hyperlinkButtonAutomationPeer;
    
    if (iprovider != null)
    iprovider.Invoke();
    
     
    Monday, May 31, 2010 4:16 PM
  • Re: kiener

    But I think you mean
    base.OnClick();
    instead of
    base.ClickMe();

    Sorry, you're right kiener that was my mistake...
    I often type and kick a bit faster than the eye...
    and I'm actually working with 3 screens right now...
    so it's highly possible I might have mixed some code in between...
    the right code should indeed be:

    MyHyperlinkButton button = new MyHyperlinkButton("http://www.bing.com");  
    button.ClickMe();

    --------------------------
    Re: Lucky777
    How can I programmatically Click the button?

    In your Xaml simply add:

    <HyperlinkButton x:Name="btnMyURL" Content="Free Beer!!!" Click="btnMyURL_Click"/>

    then in your code behind:

    private void btnMyURL_Click(object sender, RoutedEventArgs e)
    {
        MyHyperlinkButton button = new MyHyperlinkButton("
    http://www.bing.com");  
        button.ClickMe();
    }

    -----------------------------------------
    BTW Free Beer!!! is simply an old trick! It works everytime! 

    If this answers your question, please Mark as an answer. Thank you!

    Monday, May 31, 2010 5:02 PM
  •  Hello,

    i hope these links may help you:

    http://csharperimage.jeremylikness.com/2010/05/silverlight-out-of-browser-oob-versions.html

    http://mokosh.co.uk/post/2009/10/08/silverlight-oob-open-new-browser-window/

    Monday, May 31, 2010 5:36 PM
  • Hi Srinivas,

    1)http://csharperimage.jeremylikness.com/2010/05/silverlight-out-of-browser-oob-versions.html
      Jeremy's article gives a good example on how to set OOB application but does not cover how to
      open new browser window in OOB...
    2)http://mokosh.co.uk/post/2009/10/08/silverlight-oob-open-new-browser-window/ 
       There you go... the article deals with the same code mentionned above...   

    BTW The solution above is a workaround but still not the best...
    Still if you ear of a better approach I'd be glad to ear about it...

    Monday, May 31, 2010 5:49 PM
  • Hi Lucky777,

    In a normal Silverlight application, you can open new web browser window using
    System.Windows.Browser.HtmlPage.Window.Navigate() method.
    However, this call will fail in Out of Browser mode since DOM bridge is disabled.

    Though here's a dirty alternative:

    public class MyHyperlinkButton : HyperlinkButton  

     

       2: {   

     

       3:     public MyHyperlinkButton(string navigateUri) 

     

       4:     {   

     

       5:          base.NavigateUri = new Uri(navigateUri);   

     

       6:          TargetName = "_blank";   

     

       7:     }

     

    then from code behind call :

       1: MyHyperlinkButton button = new MyHyperlinkButton("http://www.bing.com");   

     

       2: button.ClickMe(); 



    If this answers your question, please Mark as an answer. Thank you. 

    Hi MawashiKid,

    When I'm addin this code to MainPage_Loaded method, it fails.
    Is it posssible to open a window from MainPage_Loaded or MainPage() methods?

    Tuesday, June 1, 2010 4:59 PM
  • Is it posssible to open a window from MainPage_Loaded or MainPage() methods?

    Nope, must be called from user initiated code.

    Wednesday, June 2, 2010 3:29 AM
  • Hi Lucky7777,

    sl.ayer is right. It must be called from user initiated code.
    (for security reason...)
    As I mentionned earlier, the scenario may work all right but
    it's mainly used as a workaround and ain't perfect.

    The solution was mainly built to work with
    a hyperlinkbutton as the class inherits from HyperlinkButton
    and expose ClickMe() method which can
    then call protected OnClick method of the base class:

    Sorry... It won't work with MainPage_Loaded or MainPage() methods

    Wednesday, June 2, 2010 9:30 AM
  • Frown I have a function which need to open new broswer in MainPage_Load event, does anyone has suggest in this scenario?

     

    Thursday, February 2, 2012 5:27 AM
  • Hello

    I have read this article many times and actually came upon a solution by opening other applications from a silverlight OOB application.

    Thus here comes my solution:

    dynamic shell = AutomationFactory.CreateObject("WScript.shell");
    try
    {
    shell.Run(@Adres, 1, true);
    }
    catch (COMException)
    {
    return;
    }
    catch (Exception)
    {

    return;

    }

    Now for the explanation:

    By making the try Catch we will by no doubt encounter some exceptions, else it wouldn't be in there. Now for the parameter (for example the text from a textbox). must be a normal string.

    How i came upon this is kinda funny, i had some troubles with opening an application path; say

    c:\Users\Me\Program Files\daApp.exe

    and i wanted to open that via Proces. Only trouble here, Proces doesn't exist, so i found a backdoor with the shell part. Off Course here u need the elevated trust *wink wink*

    after some trial and error i came to a conclusion that the network path had to be of syntax:

    "c:\Users\Me\Program Files\daApp.exe"

    The difference here would be the quotes. 

    Off course as me trying to crash the whole thing, i tried the same while inserting a webaddress 

    http://www.google.com

    and eureka, my plan failed in not crashing the app but effectively opening a webpage in internet explorer (shell takes your recommend browser so no push here)

    So there u have it, my solution and it works!

    (now i hope not someone else already found out about this but hey, effort makes it good anyway)

    Greets Schuere

    Thursday, September 27, 2012 6:13 AM
  • Try below:

        public class BrowserInSilverlight: HyperlinkButton
        {
            public InvoiceInBrowser(string url)
            {
                if (Application.Current.IsRunningOutOfBrowser)
                {
                    base.NavigateUri = new Uri(url);
                    TargetName = "_blank";
                }
                else
                {
                    var uri = new Uri(url);
                    HtmlPage.Window.Navigate(uri, "_blank");
                }
            }

            public void ClickMe()
            {
                base.OnClick();
            }
        }

    • Proposed as answer by P_K_YADAV Tuesday, July 24, 2018 2:25 PM
    Saturday, April 28, 2018 12:04 PM