WebView在Windows 10通用应用程序中不显示HTML字符串内容
本文关键字:HTML 显示 字符串 Windows 10通 应用程序 WebView | 更新日期: 2023-09-27 18:21:52
当导航到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);
}
Helper类实现:
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中。任何建议都将不胜感激!
问题是:StackPanel隐藏了您的网络视图。将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));