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

时间:2015-11-14 16:37:57

标签: c# xaml webview win-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内容在(e.Parameter as WebLink).Content中可用,但不在WebView中显示。 任何指针都表示赞赏!

1 个答案:

答案 0 :(得分:2)

问题是:StackPanel隐藏了您的webview。将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));