Reputation: 16
I am trying to display static HTML content sent from previous Page as string to current Page when navigated to, in a Windows 10 Universal app.
I have following WebView in XAML which will display HTML content:
<Grid>
<StackPanel Orientation="Vertical">
<WebView x:Name="NewsContent" ext:MyExtensions.HTML="{Binding Source={Binding Content}}" Margin="10,0,0,0"/>
</StackPanel>
</Grid>
C# portion:
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 class implementation:
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);
}
}
}
While debugging, the HTML content is available in (e.Parameter as WebLink).Content, but is not displayed in WebView. Any pointers are appreciated!
Upvotes: 0
Views: 1481
Reputation: 1942
The problem is: StackPanel hides your webview. Put webview outside of the StackPanel should make it work.
Also you have problem with the extension:
Change
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));
to
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));
Upvotes: 2