WinRT WebView NavigateToString错误

时间:2015-07-06 19:12:49

标签: c# webview navigation windows-runtime winrt-xaml

我对WinRT中的WebView组件有一个奇怪的问题。我用它来显示网站Soure property binding或本地内容NavigateToString

我的问题如下: 如果我更改Source property,它会导航到网站。然后,我调用NavigateToString并正确显示内容。之后,我再次更改Source property,但导航未启动,本地内容仍然存在。

此外,如果我先拨打NavigateToString然后更改Source property,则不会开始导航。

我准备了一个小样本项目来说明我的问题:http://1drv.ms/1HI8p3I

编辑:

我更新了上面的示例项目。我尝试通过Naviagte拨打Attached Property而不是更改Source property,但我遇到了同样的问题。致电NavigateToString后,bindings的所有其他更新均无效。

这是我的类,其中包含WebView的附加属性:

public class WebViewEx
{
    #region ContentString

    public static readonly DependencyProperty ContentStringProperty =
        DependencyProperty.RegisterAttached("ContentString", typeof(string), typeof(WebViewEx), new PropertyMetadata("", new PropertyChangedCallback(OnContentStringPropertyChanged)));

    public static void SetContentString(DependencyObject obj, string contentString)
    {
        obj.SetValue(ContentStringProperty, contentString);
    }

    public static string GetContentString(DependencyObject obj)
    {
        return (string)obj.GetValue(ContentStringProperty);
    }

    private static void OnContentStringPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        WebView webView = d as WebView;

        if (webView != null)
        {
            webView.NavigateToString((string)e.NewValue);
        }
    }

    #endregion

    #region UriString

    public static readonly DependencyProperty UriStringProperty =
        DependencyProperty.RegisterAttached("UriString", typeof(string), typeof(WebViewEx), new PropertyMetadata("", new PropertyChangedCallback(OnUriStringPropertyChanged)));

    public static void SetUriString(DependencyObject obj, string uriString)
    {
        obj.SetValue(UriStringProperty, uriString);
    }

    public static string GetUriString(DependencyObject obj)
    {
        return (string)obj.GetValue(UriStringProperty);
    }

    private static void OnUriStringPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        WebView webView = d as WebView;

        if (webView != null)
        {
            webView.Navigate(new Uri((string)e.NewValue));
        }
    }

    #endregion
}

0 个答案:

没有答案