2

I am developing a windows phone application. In a page I placed a button and on this button click I open the webbrowser and redirects to our website pages for some processes. There are many webpages. In the last webpage we added a button "Close". In the close button click I want to close the webbrowser and open the application back with the last state before opening the webbrowser. How can I do this ?

Thanks.

2 Answers 2

6

I will refer to the button in the app as "app button" and the button in the web page as "close button".

Add a WebBrowser control called webBrowser1 to the Windows Phone app. Make it cover the entire screen, and set it's Visibility property to Collapsed.

On the app button's click event, use

webBrowser1.Visibility = System.Windows.Visibility.Visible;
webBrowser1.Navigate(new Uri("http://yourwebsite.com/page");

to show the web browser and navigate it to the first page on your website.

Make the close button on the last page of your site navigate to a new page on your site, called "close.html" or whatever you want. In javascript, this would look like

<Button onclick="window.location.href='http://yourwebsite.com/close.html';">

Back in the app: On webBrowser1's Navigating event use,

if (e.Uri.ToString().Contains("close.html"))
{
    webBrowser1.Visibility = System.Windows.Visibility.Collapsed;
}

When the you click the button on the last page of your site, it navigates to "close.html". When this happens, the Web Browser's Navigating event fires. Since this event fires every time you change pages, you need to check to see if the new url contains "close.html", the page your close button is navigating to. If it does, the Web Browser will be hidden and you will see your app again.

.

(In VB, the code would be )

webBrowser1.Visibility = System.Windows.Visibility.Visible
webBrowser1.Navigate(New Uri("http://yourwebsite.com/page")

And

If e.Uri.ToString.Contains("close.html")
    webBrowser1.Visibility = System.Windows.Visibility.Collapsed
End If
1

Edit: I was thinking in generic terms when i wrote the answer and did not remember that you are asking specifically about the webbrowser (hence, using a webbrowsertask launcher). Thank you @Claus for point out the OAuth situation. So, i am amending my answer to explain that it is possible and also mention an issue with using the launcher as there is no way guarantee to return back to a given point in your launcher app (as it is with choosers due to the availability of callbacks).

It is not possible to achieve this in general terms. That is, there is an application A which opens another application B and from application B you would like to close-it-and-open-A. There are many reasons why i think it is not possible: - How does one get the address/reference to the application A. No API for that at the moment. - There is no content-handler/plugin where a 3rd party can register an app with the web-browser. - Most importantly, security, security, security. This would open doors for attacks from the web.

However, for your requirement of application B being a web-browser, it is possible to use a task launcher WebBrowserTask. As @claus suggests, you could have Window.close() javascript in your last page to close the browser and hence reveal the app underneath it (hopefully, A). The problem here is that if, the user opens an app (let's call it C) after the browser has launched (and before the browser is closed), and the user does not close C, then when the browser gets closed, the user will be returned to C and not to the launcher App! This is not what you want based on your requirement.

So, if you would like to achieve the kind of effect you are describing in your question, it is best that you embed the Web-browser in your application (as a full-screen app) and from that vantage point you can interact between the web-browser (control) and the (host) app via Javascript.

Hopefully, this helps.

2
  • As a example, OAuth utilizes a "stop URI" for when the user been authenticated successfully. A embedded web-browser is a good solution if this is necessary. Sep 8, 2012 at 21:22
  • @ClausJørgensen you are correct. I wonder why i was thinking in generic terms when answering the question. It is clearly possible :) However, i see an issue with the out-of-app navigation as there is a possibility for the user to navigate to another app (C) after the browser is launched and before it is closed. It that app C is in the background, then when the browser closes it will return to it and not the app A. So, for the requirement of returning to the launcher app, i think that an embedded webbrowser control would ensure that.
    – Gros Lalo
    Sep 9, 2012 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.