wp8:点击按钮打开webBrowser中的htm页面?

时间:2013-08-28 06:00:20

标签: c# xaml windows-phone-8

我的应用中有几个html页面。我将这些html文件命名为f1.html,f2.html,f3.html,\ ... f454.html。所以,我想按用户偏好显示这些文件。因此,我使用CustomMessageBoxNuGet创建了一个文本框和按钮,以及一个名为webview.xaml的xaml页面。如果用户在文本框中输入3,则应在webview.xaml中打开f3.html。

我不知道如何编码。最好的答案将非常感谢。

C#Code我现在做了[UPDATE];

 TextBox getFileNo = new TextBox();
 getFileNo.Height = 72;
 getFileNo.Width = 150;
 getFileNo.MaxLength = 3;
 TextBox getHashNo = new TextBox();
 getHashNo.Height = 72;
 getHashNo.Width = 150;
 getHashNo.MaxLength = 3;

 string showFile;
 showFile = getFileNo.Text;
 string hashId;
 hashId = getHashNo.text;
 NavigationService.Navigate(new Uri("/webview.xaml?Page=" + site, UriKind.Relative));

在webview.xaml中:

        protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        if (NavigationContext.QueryString.ContainsKey("Page"))
        {
            var page = NavigationContext.QueryString["Page"];
            browser.Navigate(new Uri("/f" + page + ".html#" + hashId, UriKind.Relative));
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以导航到 webview.xaml 页面,并使用所需的hmtl文件传递querystring:

NavigationService.Navigate(new Uri(String.Format("/webview.xaml?Page={0}&id={1}", showFile, hashId), UriKind.Relative));

webview.xaml 页面中,您可以覆盖OnNavigatedTo方法以检查传递的网页并在网络浏览器中将其打开:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    if (NavigationContext.QueryString.ContainsKey("Page") && NavigationContext.QueryString.ContainsKey("id"))
    {   
        var page = NavigationContext.QueryString["Page"];
        var hashId = NavigationContext.QueryString["id"];

        yourWebBrowser.Navigate(new Uri(String.Format("/f{0}.html#{1}", page, hashId), UriKind.Relative));
    }
}

有关详情,请参阅Passing paramentersHow to perform page navigation on Windows Phone部分。

相关问题