UWP 应用试图将本地 html 文件加载到 webview 控件

UWP app trying to load a local html file to webview control

我的通用 windows 应用需要使用 webview 控件加载 html 文件。我尝试了这个问题中给出的以下解决方案。我无法让它发挥作用。

Load local html file in WebView Metro Style app

现在我的代码如下。它给我一个错误如下

Error CS4036 'IAsyncOperation' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation' could be found (are you missing a using directive for 'System'?) ReaderX c:\users\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);
        }
    }
}

还有其他方法吗?

代码示例适合我。你尝试过新项目吗?我确定你的参考资料有问题。

尝试将您的引用一一删除以找到罪魁祸首。

嘿,如果你有时间告诉我这是否有效:

在您的 Assets 文件夹中创建一个名为 MyHTMLPage 的 Html 文件,它有一个内容类型的构建操作和一个 Copy to Output to Copy Always。

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>

主Page.xaml:

  <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <WebView x:Name="MyWebView"  Width="200" Height="300"></WebView>
    </Grid>

主Page.cs:

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

这对你有用吗?