已帮助:409人

可以通过将 WebBrowser (WebView)控件托管在应用程序中并使用其 Source 属性或 Navigate(Uri)

方法更改该控件的位置来实现该操作。

警告: 默认情况下,脚本在 WebBrowser 控件中处于禁用状态。如果您想在控件中启用脚本,请将 IsScriptEnabled 属性设置为

true。

以下代码示例显示如何从 .xaml 文件内部更新 WebBrowser (WebView)控件的 Source 属性:

或者,如果在 .xaml 文件中为 WebBrowser (WebView)控件指定了一个名称,则可以从该代码隐藏文件内部更新 Source

属性。以下代码示例显示如何更新 WebBrowser 控件(在 .xaml 文件中已命名为 webBrowser1(webView1))的 Source

属性:

Wp8:
webBrowser1.Source = new Uri("http://.com", UriKind.Absolute);
Win8:
webView1.Source = new Uri("http://.com", UriKind.Absolute);

或者,也可以使用 WebBrowser(WebView)类的 Navigate(Uri) 方法来实现该目标:

Wp8:
webBrowser1.Navigate(new Uri("http://www.bing.com", UriKind.Absolute));
Win8:
webView.Navigate(new Uri("http://www.bing.com", UriKind.Absolute));

如果您选择调用方法而不是设置属性,那么请记住,如果WebBrowser控件尚不在可视化树中,则会引发

InvalidOperationException。为了避免这个问题,您可以向 Loaded

事件附加一个处理程序,以确保在调用该方法之前此控件位于可视化树中。

.Loaded += (object sender, RoutedEventArgs e) =>
{
webBrowser1.Navigate(new Uri("http://www.bing.com", UriKind.Absolute));
}; webView1.Loaded += (object sender, RoutedEventArgs e) =>
{
webView1.Navigate(new Uri("http://www.bing.com", UriKind.Absolute));
};

2. 显示静态 Web 内容

您可以使用 WebBrowser

(WebView)控件在应用程序中显示已设置格式的静态内容。例如,开发人员可能希望在应用程序包中包含帮助文本,以便用户可以随时访问。或者,您也可以使用

WebBrowser (WebView)控件显示应用程序已使用 SaveToString() 方法保存到独立存储的静态 Web 内容。

向项目中添加静态内容

向项目中添加静态内容的步骤

1. 使用以下 HTML 代码创建一个名为 readme.htm 的 HTML 文件:

Sample Readme Content

2. 在 Visual Studio 中打开一个新的或现有的解决方案。

3. 在“解决方案资源管理器”中,右键单击您项目的名称,单击“添加”,然后单击“现有项”。

4. 导航到 readme.htm 文件的位置,选择该文件,然后单击“添加”。

5. 在“解决方案资源浏览器”中,单击该文件的名称。确认“属性”窗口中的“生成操作”部分。

添加命名空间

在页面后台代码中添加以下资源以包含以下命名空间。例如,如果您对主页使用默认命名约定,则应更新 MainPage.xaml.cs。

using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Resources;

添加 WebBrowser(WebView)控件

可以使用工具添加 WebBrowser (WebView)控件,也可以手动添加 WebBrowser (WebView)控件。

使用工具添加 WebBrowser(WebView)控件

使用工具添加 WebBrowser (WebView)控件的步骤

1. 在 Visual Studio 中打开一个新的或现有的解决方案。

2. 查看项目的 XAML 文件时,单击“工具箱”,将 WebBrowser (WebView)控件拖动到设备的图像中。

手动添加 WebBrowser(WebView)控件

在 XAML 中创建 WebBrowser (WebView)控件的步骤

1. 打开将在其中添加 WebBrowser (WebView)控件的页面的 XAML 文件。在“解决方案资源浏览器”中,右键单击该页面的 .xaml

文件(默认情况下,新应用程序的主页名为“MainPage.xaml”),然后选择“打开”。

2. 在 ContentGrid 中添加一个 WebBrowser (WebView)控件。例如:

Wp8:

Win8:

添加向独立存储中添加文件的代码

修改页面后台代码以包含以下两个函数,这两个函数将帮助向独立存储中添加静态文件。例如,如果您对主页使用默认命名约定,则应更新

MainPage.xaml.cs。

private void SaveFilesToIsoStore()
{
//These files must match what is included in the application package,
//or BinaryStream.Dispose below will throw an exception.
string[] files = {
"readme.htm"
};
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication();
if (false == isoStore.FileExists(files[0]))
{
foreach (string f in files)
{
StreamResourceInfo sr = Application.GetResourceStream(new Uri(f,
UriKind.Relative));
using (BinaryReader br = new BinaryReader(sr.Stream))
{
byte[] data = br.ReadBytes((int)sr.Stream.Length);
SaveToIsoStore(f, data);
}
}
}
}
private void SaveToIsoStore(string fileName, byte[] data)
{
string strBaseDir = string.Empty;
string delimStr = "/";
char[] delimiter = delimStr.ToCharArray();
string[] dirsPath = fileName.Split(delimiter);
//Get the IsoStore.
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication();
//Re-create the directory structure.
for (int i = 0; i < dirsPath.Length - 1; i++)
{
strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
isoStore.CreateDirectory(strBaseDir);
}
//Remove the existing file.
if (isoStore.FileExists(fileName))
{
isoStore.DeleteFile(fileName);
}
//Write the file.
using (BinaryWriter bw = new
BinaryWriter(isoStore.CreateFile(fileName)))
{
bw.Write(data);
bw.Close();
}
}