简体   繁体   English

WP8-如何仅通过C#在Windows Phone 8中创建和打开WebBrowser

[英]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. 我是Windows Phone开发的新手,请原谅我的无知。 I need to create and open a WebBrowser on a button click (button created on xaml page) in C# code. 我需要在C#代码中的按钮单击(在xaml页上创建的按钮)上创建并打开WebBrowser。 I have seen so many examples where the WebBrowser is created in XAML and Navigate is called in C#. 我已经看到了很多示例,其中在XAML中创建WebBrowser,在C#中调用Navigate。 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": 从我的头顶开始,假设主网格名为“ 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. 覆盖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: JonPall的答案很好,如果您想在IE中打开uri,请尝试以下方法:

using namespace : Microsoft.Phone.Tasks 使用名称空间: Microsoft.Phone.Tasks

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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