【问题标题】:Xamarin Android: Dynamic update UI based on ViewmodelXamarin Android:基于 Viewmodel 的动态更新 UI
【发布时间】:2017-06-12 16:04:06
【问题描述】:

在我的 android 项目中,webview url 是动态的。我在视图端使用 mvvmcros 绑定,但它不是动态的。如果视图模型上的 url 内容正在更改,则它不会在视图中更新。谁能帮帮我?

查看

public string WebContentUrll { get; set; }    
protected override void OnCreate(Android.OS.Bundle bundle)    
{        
  var bindingSet = this.CreateBindingSet<view, ViewModel>(); 
  bindingSet.Bind(this).For(v => v.WebContentUrll).To(vm => vm.WebContentUrl).TwoWay();    
} 

视图模型

   private string webContentUrl;      
    public string WebContentUrl      
    {       
     get       
     {        
       return webContentUrl;       
     }       
     set       
     {        
      webContentUrl = value;        
      RaisePropertyChanged(() => webContentUrl);       
     }      
    }
  public void Init()
  {
     webContentUrl = "https://.."'
  }

页面加载后视图模型中的网页内容url的值发生变化,但android视图无法获取新的更新url。

谁能给点建议。谢谢。

更新

单击按钮会打开 Web 视图,并在页面加载后和单击按钮之前更新 url

【问题讨论】:

  • 我不太确定你想在这里做什么。您是否在视图中使用 WebView 控件? ViewModel 是否负责更改 URL?您现在拥有它的方式,它只是更改视图上的属性......它不会加载新页面。

标签: xamarin mvvm xamarin.android mvvmcross


【解决方案1】:

来自您在开篇文章中的描述。在您的活动中,您定义了一个属性WebContentUrll。你想绑定它并在它改变时更新一些东西。

WebContentUrll的定义是:

public string WebContentUrll { get; set; }

这没有错,当 WebContentUrll 通过绑定从 ViewModel 更改时,您应该会看到反映在该值中的值。但是,没有代码可以根据该属性更新任何视觉状态、视图或任何内容。

如果您有想要更改内容的 WebView,您可以将属性修改为:

private string _webContentUrll;
public string WebContentUrll
{
    get => _webContentUrll;
    set 
    {
        _webContentUrll = value;
        _webView.LoadUrl(_webContentUrll);
    }
 }

鉴于_webView 是您的WebView 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    相关资源
    最近更新 更多