简体   繁体   中英

WP8 - How to create and open a WebBrowser in Windows Phone 8 only through C#

I am new to windows Phone development, so please excuse my ignorance. I need to create and open a WebBrowser on a button click (button created on xaml page) in C# code. I have seen so many examples where the WebBrowser is created in XAML and Navigate is called in C#. But my requirement is to create a complete screen on button click.

Can anyone help me with this ? Any level of guidance would be helpful.

From the top of my head and assuming the main grid is named "LayoutRoot":

    private void btnOpenAndGo_Click(object sender, RoutedEventArgs e)
    {
        WebBrowser web = new WebBrowser();
        web.Height = LayoutRoot.Height;
        web.Width = LayoutRoot.Width;
        LayoutRoot.Children.Add(web);
        web.Navigate(...);
    }

Edit: To control the hardware back button. Override OnBackKeyPress. And do something like this:

    protected override void OnBackKeyPress(CancelEventArgs e)
    {
        base.OnBackKeyPress(e);

        if (web.Visibility = Visibility.Visibile)
        {
            web.Visibility = Visibility.Collapsed;
            e.Cancel = true;
        }
    }

JonPall's Answer is nice, and if you want to open you uri in IE, try this method:

using namespace : Microsoft.Phone.Tasks

WebBrowserTask task = new WebBrowserTask();
task.URL = "http://yoururl.com";
task.Show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM