【问题标题】:extendedWebView not displayed from ViewModelExtendedWebView 未从 ViewModel 显示
【发布时间】:2020-05-14 09:15:14
【问题描述】:

我有 webView,我正在通过 ViewModel 绑定它,当我调试它时,我知道该值在那里但是我看不到它。我将它设置在视图的后面,所以我知道一切正常,但我无法从 ViewModel 显示它。有什么建议吗?

XAML

  <controls:ExtendedWebView
    x:Name="nameEng"
    HeightRequest="150"
    IsVisible="True"
    Opacity="1"
    Source="{Binding EN}" />

内容页面

public Dictionary()
        {

            InitializeComponent();
            BindingContext = new DictionaryViewModel();
}

视图模型

 private static HtmlWebViewSource htmlSourceExplanation = new HtmlWebViewSource();
      private ExtendedWebView _en;
     public ExtendedWebView EN
            {
                get { return _en; }
                set
                {
                    _en = value;

                    NotifyPropertyChanged(nameof(_en));
                }
            }

     private void SetNameAndGrammarDetail(string text, string text2)
            {
                int size = CustomFontSize;
                var style = "<style>.text{ font-size:" + size + "px !important; }</style>";

                htmlSourceName.Html = style + "<div class=\"text\"><div>" + text + "</div><div>" + text2 + "</div></div>";
                EN.Source = htmlSourceName;

                EN.BindingContextChanged += (sender, args) =>
                {
                    htmlSourceName.BindingContext = htmlSourceName.BindingContext;
                };
            }

【问题讨论】:

  • 我看到您在视图模型中将 ExtendedWebView 作为属性公开。这不是应该的样子。您基本上是将 WebView.Source(来自您的 XAML)设置为新的 WebView。我认为您应该公开 HtmlText 并将 Source 属性绑定到它。
  • 你能告诉我怎么做吗?

标签: xamarin.forms


【解决方案1】:

您正在将 XAML 中的 WebView.Source 绑定到属性 EN,这只是在 ViewModel 中创建的另一个 WebView。

您要做的是将WebView.Source(在 XAML 中定义)绑定到 HTML 代码或 URL。

Gerald Versluis 的回答完美地描述了如何做到这一点:

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多