C# wp8:通过单击按钮在webBrowser中打开htm页面?

C# wp8:通过单击按钮在webBrowser中打开htm页面?,c#,xaml,windows-phone-8,C#,Xaml,Windows Phone 8,我的应用程序中有几个html页面。我将这些html文件命名为f1.html、f2.html、f3.html、\。。。f454.html。所以,我想按用户偏好显示这些文件。因此,我使用NuGet在CustomMessageBox中创建了一个文本框和按钮,并创建了一个名为webview.xaml的xaml页面。如果用户在文本框中输入3,则f3.html应在webview.xaml中打开 我不知道如何编码。最好的答案将不胜感激 C#代码我一直在做[更新] TextBox getFileNo = ne

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

我不知道如何编码。最好的答案将不胜感激

C#代码我一直在做[更新]

 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));
        }
    }
您可以导航到webview.xaml页面,并通过查询字符串传递所需的hmtl文件:

NavigationService.Navigate(new Uri(String.Format("/webview.xaml?Page={0}&id={1}", showFile, hashId), UriKind.Relative));
在webview.xaml页面中,您可以覆盖
OnNavigatedTo
方法来检查传递的页面并在web浏览器中打开它:

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));
    }
}

有关更多信息,请参见中的部分。

精彩的代码片段。还有一个疑问。我想导航到我的应用程序中声明的
id
。例如,
f1.html#34
。如何做到这一点?我想您也可以将其作为querystring传递:
NavigationService.Navigate(新Uri(“/webview.xaml?Page=“+showFile+”&id=“+yourId,UriKind.Relative))但什么不起作用?它未在指定id中打开,或者您无法将其作为参数传递到webview.xaml页面?您是否收到任何错误?webview.xaml.cs
中的错误当前上下文中不存在名称“hashId”
Ok,这是因为您也必须在webview.xaml中读取该参数:
var hashId=NavigationContext.QueryString[“id”]