Action内部的方法回调触发但不导航到视图

时间:2016-04-14 15:23:51

标签: c# wpf mvvm prism

我正在使用Prism,我有一个带有以下ViewModel的状态模块:

public class StatusViewModel : WorkSpaceViewModel, IStatusQueryViewModel
{
    private bool _isActive;
    private StatusContext _context;        

    public StatusViewModel(IEventAggregator eventAggregator)
    {
        eventAggregator.GetEvent<StatusEvent>()
            .Subscribe(x => UpdateStatus(x), ThreadOption.UIThread, true);
        IsActive = false;
    }

    public bool IsActive
    {
        get { return _isActive; }
        set { SetProperty(ref _isActive, value); }
    }

    public StatusContext Context
    {
        get { return _context; }
        set { SetProperty(ref _context, value); }
    }

    private void UpdateStatus(StatusContext context)
    {
        if (context != null)
        {
            Context = context;
            IsActive = true;

            if (Context.Callback != null)
            {
                 Timer t = new Timer(x =>
                {
                    Context.Callback.Invoke();
                    IsActive = false;
                }, null, 3000, 0);  
            }
        }
    }
}

所以,当我解雇状态时:

_statusQuery.SetStatus(new StatusContext(string.Format("Cargando datos del servidor {0}", server.NombreInstancia),StatusType.Informative,
                    () =>
                    {
                        var targetUri = new Uri("SqlMasterView", UriKind.Relative);
                        var navParameters = new NavigationParameters();
                        navParameters.Add("selectedServer", server);
                        _regionManager.RequestNavigate(RegionNames.ServerInfoBarRegion, targetUri, navParameters);
                    }));

代码正在StatusViewModel中的callback.invoke之后运行,但在它读取的debuger中没有任何反应,没有查看弹出:

A first chance exception of type 'System.InvalidOperationException' occurred in PresentationCore.dll
A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Practices.Unity.dll
A first chance exception of type 'Microsoft.Practices.Unity.ResolutionFailedException' occurred in Microsoft.Practices.Unity.dll
A first chance exception of type 'Microsoft.Practices.ServiceLocation.ActivationException' occurred in Microsoft.Practices.ServiceLocation.dll
A first chance exception of type 'System.InvalidOperationException' occurred in Prism.Wpf.dll

顺便说一句,不知道它是否重要但是statusQuery被包装在另一个回调中:

_dialogQuery.Publish(
            new DialogContext(DialogType.Prompt, 
                String.Format("Desea cargar los datos del servidor {0}?", server.NombreInstancia), null, x =>
        {
            bool b = x == DialogResult.Ok;
            if (b)
            {
                _statusQuery.SetStatus(new StatusContext(string.Format("Cargando datos del servidor {0}", server.NombreInstancia),StatusType.Informative,
                    () =>
                    {
                        var targetUri = new Uri("SqlMasterView", UriKind.Relative);
                        var navParameters = new NavigationParameters();
                        navParameters.Add("selectedServer", server);
                        _regionManager.RequestNavigate(RegionNames.ServerInfoBarRegion, targetUri, navParameters);
                    }));
            }
        }));

也许它与线程有关?谢谢

编辑:在使用statusQuery之前,代码运行正常

编辑2:我尝试使用System.Timers.Timer和First Chance异常抛出

1 个答案:

答案 0 :(得分:0)

好的,如果有人带着同样的问题来到这里,Dispatcher解决了它:

Application.Current.Dispatcher.BeginInvoke((System.Windows.Threading.DispatcherPriority.Normal),
                (Action)(() =>
                {
                    Context.Callback.Invoke();
                    IsActive = false;
                    Dispose();
                }
            ));