如何在UWP 中将WebView 转换为IRandomAccessStream?

how to convert WebView into IRandomAccessStream in UWP?

我试过的是? 我的 Xaml 代码:

<Grid x:Name="MyGrid">
    <Button x:Name="but" Content="Click" Click="but_Click"></Button>
</Grid>

我的 C# 代码:

    private async void but_Click(object sender, RoutedEventArgs e)
    {
        WebView x = new WebView();
        x.Width = 100; x.Height = 100;
        InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
        await x.CapturePreviewToStreamAsync(ms);
    }

当我尝试将 Webview 转换为 Stream.It 时抛出这样的错误。

我不明白为什么会这样。任何人都可以建议我,如何将 webview 转换为 IRandomAccessstream?

问题是 WebView 没有呈现到 xaml 可视化树中。我们需要在调用 CapturePreviewToStreamAsync 方法之前将 WebView 插入页面内容。

private WebView x;
public MainPage()
{
    this.InitializeComponent();
    x = new WebView();
    x.Height = 100; x.Width = 100;
    RootGrid.Children.Add(x);
}

private async void bookmarkBtn_Click(object sender, RoutedEventArgs e)
{

    InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
    await x.CapturePreviewToStreamAsync(ms);
}