【问题标题】:UWP app trying to load a local html file to webview controlUWP 应用程序尝试将本地 html 文件加载到 webview 控件
【发布时间】:2015-11-14 18:38:44
【问题描述】:

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

Load local html file in WebView Metro Style app

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

错误 CS4036 'IAsyncOperation' 不包含 'GetAwaiter' 的定义并且没有扩展方法 'GetAwaiter' 接受“IAsyncOperation”类型的第一个参数 可以找到(您是否缺少 using 指令 '系统'?)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);
        }
    }
}

还有其他方法吗?

【问题讨论】:

  • 你有没有“使用系统;”在你声明这个类的cs文件中?很可能它丢失了(正如编译器错误所说),因为这应该编译...
  • @gregkalapos :是的,我有。我已经检查过了。
  • 你能分享一个重现项目吗?

标签: c# webview uwp


【解决方案1】:

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

在名为 MyHTMLPage 的 Assets 文件夹中创建一个 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>

在主页.xaml:

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

在主页.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));
        }
    }

这对你有用吗?

【讨论】:

    【解决方案2】:

    代码示例对我有用。你尝试过新项目吗?我确定您的参考文献有问题。

    尝试一一删除你的引用,找出罪魁祸首。

    【讨论】:

    • 我什至尝试重新安装 Visual Studio(也创建了一个新项目)如果我在这种方法中提供此代码,它会给我一个错误。我把它移到另一个班级,它工作正常。
    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2022-06-25
    • 2017-06-30
    相关资源
    最近更新 更多