0

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.

Community
  • 1
  • 1
  • There is no notification mechanism that informs the Binding when the BrowserUri property changes. Your view model class should implement the INotifyPropertyChanged interface. – Clemens Sep 07 '16 at 10:51
  • My view model does implement INotifyPropertyChanged. I see each new Uri get processed in AttachedUriPropertyChanged, it just doesn't seem to have any effect on the browser. The binding is meant as one-way only, from view model to browser, not back – gingergenius Sep 07 '16 at 10:57
  • One more thing I noticed: Calling Refresh() on browser always results in an exception.HRESULT E_FAIL – gingergenius Sep 07 '16 at 15:51

1 Answers1

0

I just decided to create a new browser in a static border each time I need to navigate.

<Border Grid.Row="1" at:AttachedUri.AttachedUri="{Binding BrowserUri}"/>

And code:

public static void AttachedUriPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        Border border = o as Border;            
        if (border != null)
        {                
            string uri = e.NewValue as string;
            if (!String.IsNullOrEmpty(uri))
            {
                Uri link = new Uri(uri);
                WebBrowser newBrowser = new WebBrowser();
                newBrowser.Navigate(link);
                border.Child = newBrowser;
            }                
        }
    }