2 回答
TA贡献1725条经验 获得超7个赞
这就是您的管理方式。。。您必须选择在何处处理数据并在ViewModel中进行转换。唯一的事情是在调度程序线程上填充可观察对象。
主视图模型
_UpdateTimerClock = new DispatcherTimer();
_UpdateTimerClock.Interval = new TimeSpan(0, 0, 6);
_UpdateTimerClock.IsEnabled = true;
_UpdateTimerClock.Tick += UpdateTimerClockElapsed;
public void UpdateTimerClockElapsed(object tag, EventArgs args)
{
try
{
Messenger.Default.Send(new NotificationViewModelRefresh());
}
catch (Exception err)
{
BusinessLogger.Manage(err);
}
}
子视图模型
public ViewModel()
{
try
{
Messenger.Default.Register<NotificationViewModelRefresh>(this, HandleRefreshAction);
}
catch (Exception error)
{
BusinessLogger.Manage(error);
}
}
private void HandleRefreshAction(NotificationViewModelRefresh msg)
{
if (!this.IsVisible)
return;
儿童观
public View()
{
InitializeComponent();
if (!DesignerProperties.GetIsInDesignMode(this))
{
this.IsVisibleChanged += (o, e) =>
{
if (this.IsVisible)
(this.DataContext as ViewModel).Refresh();
};
}
}
在计时器中的刷新或更常规的处理任务中...
Task tk = Task.Factory.StartNew(() =>
{
if (System.Threading.Monitor.TryEnter(_RefreshLocker))
{
try
{
VisualHelper.InvokeBackground(() =>
{
视觉辅助提取物
private static void BeginInvoke(DispatcherPriority prio, Action action)
{
try
{
if (Application.Current != null)
{
if (Application.Current.Dispatcher.CheckAccess())
action();
else
Application.Current.Dispatcher.BeginInvoke(prio, (ThreadStart)(() => action()));
}
}
catch (Exception err)
{
BusinessLogger.Manage(err);
}
}
- 2 回答
- 0 关注
- 280 浏览
添加回答
举报