[英]Why binding is not enough to refresh UI in WP7 (MVVM)
I'm trying to create WP7 application based on MVVM pattern but I have problem with refreshing bind content of TextBlock. 我正在尝试基于MVVM模式创建WP7应用程序,但我在刷新TextBlock的绑定内容时遇到问题。 In the current state I need to reopen page to refresh content. 在当前状态下,我需要重新打开页面来刷新内容。 I think that it's related to setting data context but I couldn't fix it. 我认为这与设置数据上下文有关但我无法修复它。
PropertyChangedEventHandler in ViewModel.cs 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 App.xaml.cs中的ViewModel属性
public partial class App : Application
{
private static ViewModel _viewModel { get; set; }
public static ViewModel ViewModel
{
get { return _viewModel ?? (_viewModel = new ViewModel()); }
}
Setting DataContext in StatusPage.xaml.cs 在StatusPage.xaml.cs中设置DataContext
public partial class Status : PhoneApplicationPage
{
public Status()
{
InitializeComponent();
DataContext = App.ViewModel;
}
Binding in StatusPage.xaml 在StatusPage.xaml中绑定
<TextBlock x:Name="TxtStatus" Text="{Binding Path=TxtStatus, Mode=OneWay}" Width="450" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" />
UPDATE 1 更新1
Setting value of TxtStatus in MqttService.cs 在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;
}
MqttService Property in ViewModel.cs ViewModel.cs中的MqttService属性
private MqttService _mqttService;
public MqttService MqttService
{
get { return _mqttService ?? (_mqttService = new MqttService(this)); }
}
Now I wonder if maybe I have some kind of the circular reference problem (MqttService-ViewModel). 现在我想知道是否有某种循环引用问题(MqttService-ViewModel)。 I'm not sure, it looks good to me. 我不确定,这对我来说很好看。
Your code works fine for me in WPF
.NET4.0
在WPF
.NET4.0
您的代码适合我
so maybe your Property TxtStatus
never get a string 所以也许你的Property TxtStatus
永远不会得到一个字符串
_txtStatus ="new status"; // wrong
TxtStatus = "new status"; // right
or you get some interfering with your x:Name="TxtStatus"
but that would be an Windows-Phone-7 based problem 或者你得到一些干扰你的x:Name="TxtStatus"
但这将是一个基于Windows Phone 7的问题
Thank You all. 谢谢你们。 After Erno de Weerd and ken2k wrote comments about threads I did some research and found this: Notify the UI Thread from Background Thread . 在Erno de Weerd和ken2k撰写关于线程的评论之后,我做了一些研究并发现: 从背景线程通知UI线程 。 I changed the way of setting value of the TxtStatus and it's working perfectly now. 我改变了设置TxtStatus值的方法,现在它运行得很好。 :) :)
(bad) Setting value of TxtStatus in MqttService.cs (坏)在MqttService.cs中设置TxtStatus的值
private void Log(string log)
{
_viewModel.TxtStatus = _viewModel.TxtStatus + log;
}
(good) Setting value of TxtStatus in MqttService.cs (好)在MqttService.cs中设置TxtStatus的值
private void Log(string log)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
App.ViewModel.TxtStatus = log + App.ViewModel.TxtStatus;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.