I am using a WebBrowser control with an attached property to bind to an Uri
property in ViewModel as described in this question
It works, but on each start of the application the browser navigates exactly once. On second and further tries it just becomes empty (transparent, not white).
ViewModel:
public Uri BrowserUri
{
get
{
return GetBrowserUri();
}
}
Attached property:
public class AttachedUri
{
public static readonly DependencyProperty AttachedUriProperty =
DependencyProperty.RegisterAttached("AttachedUri", typeof(string), typeof(AttachedUri), new UIPropertyMetadata(null, AttachedUriPropertyChanged));
public static string GetAttachedUri(DependencyObject obj)
{
return (string)obj.GetValue(AttachedUriProperty);
}
public static void SetAttachedUri(DependencyObject obj, string value)
{
obj.SetValue(AttachedUriProperty, value);
}
public static void AttachedUriPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if (browser != null)
{
string uri = e.NewValue as string;
if (!String.IsNullOrEmpty(uri))
{
browser.NavigateToString("<html></html>"); // trying to refresh
browser.Navigate(new Uri(uri));
}
}
}
}
XAML:
<controls:WebBrowser at:AttachedUri.AttachedUri="{Binding BrowserUri}" Navigating="PDFBrowser_OnNavigating"/>
As seen above, I also tried to see if Navigating
event fires every time when I try to navigate. It does fire every time for Navigate
but only once for NavigateToString
.
I need the browser to navigate between different named destinations in a PDF file.