繁体   English   中英

WebView在Windows 10 Universal应用程序中不显示HTML字符串内容

[英]WebView does not display HTML string content in Windows 10 Universal app

在Windows 10 Universal应用程序中,我试图在导航到时显示从上一页发送的静态HTML内容作为字符串发送到当前页面。

我在XAML中有以下WebView,它将显示HTML内容:

<Grid>    
<StackPanel Orientation="Vertical">
            <WebView x:Name="NewsContent" ext:MyExtensions.HTML="{Binding Source={Binding Content}}" Margin="10,0,0,0"/>
        </StackPanel>
</Grid>

C#部分:

public sealed partial class NewsItemView : Page
{
    WebLink link;

    public NewsItemView()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        NewsContent.NavigateToString((e.Parameter as WebLink).Content); 
}

助手类的实现:

public class MyExtensions
{
    public static string GetHTML(DependencyObject obj)
    {
        return (string)obj.GetValue(HTMLProperty);
    }

    public static void SetHTML(DependencyObject obj, string value)
    {
        obj.SetValue(HTMLProperty, value);
    }

    // Using a DependencyProperty as the backing store for HTML.  This enables animation, styling, binding, etc...  
    public static readonly DependencyProperty HTMLProperty =
        DependencyProperty.RegisterAttached("HTML", typeof(string), typeof(MyExtensions), new PropertyMetadata(0));

    private static void OnHTMLChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        WebView wv = d as WebView;
        if (wv != null)
        {
            wv.NavigateToString((string)e.NewValue);
        }
    }
}

调试时,HTML内容位于(例如,参数为WebLink).Content中,但未显示在WebView中。 任何指针表示赞赏!

问题是:StackPanel隐藏您的Web视图。 将webview放在StackPanel之外应该可以正常工作。

另外,您对扩展名也有疑问:

更改

public static string GetHTML(DependencyObject obj)
public static void SetHTML(DependencyObject obj, string value)
public static readonly DependencyProperty HTMLProperty =
    DependencyProperty.RegisterAttached("HTML", typeof(string), typeof(MyExtensions), new PropertyMetadata(0));

public static string GetHTML(WebView obj)
public static void SetHTML(WebView obj, string value)
public static readonly DependencyProperty HTMLProperty =
    DependencyProperty.RegisterAttached("HTML", typeof(string), typeof(MyExtensions), new PropertyMetadata("", OnHTMLChanged));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM