WebView数据绑定仅更新一次

时间:2014-11-02 05:50:53

标签: windows-phone-8 xamarin.forms

我目前在XAML中有一个WebView,如下所示

<StackLayout>
  <WebView x:Name="PrimaryWebView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    <WebView.Source>
      <HtmlWebViewSource Html="{Binding Path=ViewModel.HtmlSource}" />
    </WebView.Source>
  </WebView>     
</StackLayout>

在我的ViewModel中我有

private String htmlSource = null;
    public String HtmlSource
    {
        get
        {

            if (String.IsNullOrEmpty(htmlSource))
            {

                Device.BeginInvokeOnMainThread(async () =>
                {
                    ScreenLoading();

                    var result = await contentController.Get(currentPage);
                    if (result != null)
                    {

                        HtmlSource = result.Content;
                        Heading = result.Name;

                    }

                    ScreenFinished();

                });

                // Returns empty while loading
                return contentController.GetBlank();
            }

            // returns here once htmlSource is no longer empty
            return htmlSource;

        }
        set
        {
            htmlSource = value;
            RaisePropertyChanged(() => HtmlSource);

        }
    }

在我的视图中,我有此修复已知错误

    /// <summary>
    /// FIX: Due to bug https://bugzilla.xamarin.com/show_bug.cgi?id=21699
    /// </summary>
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        PrimaryWebView.Source.BindingContext = BindingContext;
    }

在第一页加载

  1. 我的ScreenLoading通过显示加载屏幕来工作
  2. GetBlank()正常工作并正确填充WebView(我已用虚拟数据填充此内容进行测试)
  3. ScreenFinished正确运行并删除加载屏幕。
  4. 问题:一旦正确返回,WebView就不会使用新内容进行更新。
  5. 也许我的方法都错了,我不确定ScreenLoading和ScreenFinished的工作正常,因为他们更改了屏幕上正确更新的可绑定属性,我想知道为什么HtmlSource不是做同样的事。

1 个答案:

答案 0 :(得分:1)

我找到了一种让它更新的方法,但我完全不喜欢它。我已更新OnBindingContextChanged以检测属性何时更改,然后将其重新绑定到新源。它有效,但如果有人有更好的方式或为什么这首先发生,请告诉我。

 /// <summary>
 /// FIX: Due to bug https://bugzilla.xamarin.com/show_bug.cgi?id=21699
 /// </summary>
 protected override void OnBindingContextChanged()
 {
     base.OnBindingContextChanged();

     PrimaryWebView.Source.BindingContext = BindingContext;
     PrimaryWebView.Source.PropertyChanged += (sender, e) =>
     {

          PrimaryWebView.Source = new HtmlWebViewSource() { BindingContext = BindingContext, Html = ((PolicyViewModel)BindingContext).HtmlSource};

      };
  }
相关问题