Xamarin Android:基于Viewmodel的动态更新UI

时间:2017-06-12 16:04:06

标签: xamarin mvvm xamarin.android mvvmcross

在我的android项目中,webview网址是动态的。我在视图端使用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://.."'
  }

视图模型中的Web内容网址的值在页面加载后发生更改,但是android视图无法获取新的更新URL。

任何人都可以提供建议。谢谢。

更新

点击按钮即可打开网页视图,网页加载后点击按钮之前会更新网址

1 个答案:

答案 0 :(得分:1)

从你在开幕式上的描述。在您的活动中,您已定义了属性WebContentUrll。你想绑定它并在更改时更新一些东西。

WebContentUrll的定义是:

public string WebContentUrll { get; set; }

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

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

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

鉴于_webViewWebView的实例。