为什么在WP7(MVVM)中仅使用数据绑定不足以刷新UI

4
我正在尝试基于MVVM模式创建WP7应用程序,但我在刷新TextBlock的绑定内容方面遇到了问题。 在当前状态下,我需要重新打开页面才能刷新内容。 我认为这与设置数据上下文有关,但我无法解决它。
在ViewModel.cs中使用PropertyChangedEventHandler
public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (null != PropertyChanged)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string _txtStatus = "";
    public string TxtStatus
    {
        get { return _txtStatus; }
        set
        {
            _txtStatus = value;
            NotifyPropertyChanged("TxtStatus");
        }
    }

ViewModel Property in App.xaml.cs

public partial class App : Application
{
    private static ViewModel _viewModel { get; set; }
    public static ViewModel ViewModel
    {
        get { return _viewModel ?? (_viewModel = new ViewModel()); }
    }

在 StatusPage.xaml.cs 中设置 DataContext
public partial class Status : PhoneApplicationPage
{
    public Status()
    {
        InitializeComponent();
        DataContext = App.ViewModel;
    }

StatusPage.xaml中的绑定

<TextBlock x:Name="TxtStatus" Text="{Binding Path=TxtStatus, Mode=OneWay}" Width="450" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" />

更新 1

在 MqttService.cs 中设置 TxtStatus 的值

public class MqttService
{
    private readonly ViewModel _viewModel;

    public MqttService(ViewModel viewModel)
    {
        _viewModel = viewModel;
    }

    private void Log(string log)
    {
        _viewModel.TxtStatus = _viewModel.TxtStatus + log;
    }

    private void Connect()
    {
        _client.Connect(true);
        Log(MsgConnected + _viewModel.TxtBrokerUrl + ", " + _viewModel.TxtClientId + "\n");
        _viewModel.IsConnected = true;
    }

ViewModel.cs 中的 MqttService 属性

    private MqttService _mqttService;
    public MqttService MqttService
    {
        get { return _mqttService ?? (_mqttService = new MqttService(this)); }
    }

现在我想知道是否可能存在某种循环引用问题(MqttService-ViewModel)。我不确定,但我认为看起来还不错。


2
输出视图中是否有绑定错误? - Emond
2
我没有看到你的代码有任何问题。你是如何更新要在文本块中显示的值的? - Kevin Gosse
也许你可以添加一个 , UpdateSourceTrigger=PropertyChanged - WiiMaxx
2
你的应用程序中是否有多线程?更具体地说,TxtStatus 是否在 UI 线程中更新? - ken2k
1
你可以在ViewModel的TxtStatus setter中设置断点,看看当你更新属性时是否会被触发。如果它被触发了,那么你可能有两个并发的ViewModels;一个附加到View上,另一个在服务中。 - Emond
显示剩余3条评论
2个回答

0

谢谢大家。在Erno de Weerdken2k发表有关线程的评论后,我进行了一些研究,并找到了这篇文章:从后台线程通知UI线程。我改变了设置TxtStatus值的方式,现在它完美地工作了。:)

(不好的) 在MqttService.cs中设置TxtStatus的值

private void Log(string log)
{
    _viewModel.TxtStatus = _viewModel.TxtStatus + log;
}

(好的)在MqttService.cs中设置TxtStatus的值

private void Log(string log)
{
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        App.ViewModel.TxtStatus = log + App.ViewModel.TxtStatus;
    });
}

0

你的代码在我的 WPF .NET4.0 上运行良好。

所以可能是你的属性 TxtStatus 没有得到一个字符串。

_txtStatus ="new status"; // wrong
TxtStatus = "new status"; // right

或者你会遇到一些干扰你的x:Name="TxtStatus"但那应该是一个基于Windows Phone 7的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接