public class MyViewModel
{
private IRepository _Repository;
public string CountText { get; set; }
public MyViewModel (IRepository repository)
{
_Repository = repository;
CountText = "test ctor";
}
public void MyButtonCommand()
{
_Repository.GetResult((Result r) => MyActionAsync(r), (Exception e) => ManageException(e));
}
public void MyActionAsync(SchedeConsunitiviResult result)
{
CountText = string.Format("{0} items", result.Count);
}
public void ManageException(Exception e)
{
//to log the exception here and display some alert message
}
}
在这里我的xaml:
<sdk:Label Content="{Binding Path=CountText, Mode=TwoWay}" Grid.Row="3" Height="28" HorizontalAlignment="Left" Margin="12,142,0,0" Name="label1" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" />
CountText的第一个实例在Label中可见。但是异步方法之后的第二个不会改变LAbel的内容。我应该添加一些像PropertyChanged这样的机制来告诉视图这个属性已经改变了吗?如果是这样,我怎么能只使用xaml呢?
thx求助
答案 0 :(得分:2)
实施INotifyPropertyChanged并通过EventHandler通知您的财产已更改。
public class MyViewModel : INotifyPropertyChanged
{
private string countText;
public string CountText
{
get { return this.countText; }
set { this.countText = value; NotifyPropertyChanged("CountText"); }
}
.....snip.....
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(params string[] properties)
{
if (PropertyChanged != null)
{
foreach (string property in properties)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(property));
}
}
}
答案 1 :(得分:0)
据我所知,你的viewmodel中需要一个像PropertyChanged这样的机制