我的通用Windows应用程序需要使用webview控件加载html文件.我在这个问题中尝试了以下解决方案.我无法让它发挥作用.
在WebView Metro Style应用程序中加载本地html文件
现在我的代码如下.它给我一个错误如下
错误CS4036'IAsyncOperation'不包含'GetAwaiter'的定义,并且没有可以找到接受类型'IAsyncOperation'的第一个参数的扩展方法'GetAwaiter'(你是否缺少'System'的using指令?)ReaderX c:\用户\ sumod\documents\visual studio 2015\Projects\ReaderX\ReaderX\Views\TextView.xaml.cs 28
这是我的代码
namespace ReaderX.Views
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class TextView : Page
{
private NavPoints point;
public TextView()
{
this.InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
point = (NavPoints)e.Parameter;
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
var html = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
HTMLViewer.NavigateToString(html);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以做到这一点?
嘿,如果你有时间告诉我这是否有效:
在Assets文件夹中创建名为MyHTMLPage的Html文件,它具有类型内容的构建操作和复制到输出以始终复制.
Html文件:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div style="background-color: chartreuse">HELLO WORLD, from a webview</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在Main Page.xaml上:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView x:Name="MyWebView" Width="200" Height="300"></WebView>
</Grid>
Run Code Online (Sandbox Code Playgroud)
在主页面上:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
string src = "ms-appx-web:///Assets/MyHTMLPage.html";
this.MyWebView.Navigate(new Uri(src));
}
}
Run Code Online (Sandbox Code Playgroud)
这对你有用吗?