为什么我的调度程序不显示我的状态消息?

时间:2016-08-22 16:55:57

标签: wpf dispatcher

我正在尝试在删除操作开始时显示状态消息,例如Deleting Items...,以便用户知道发生了什么。我有一个DeleteManager类来处理删除任何POCO时的消息显示:

public class DeleteManager : IDisposable
{
    private readonly IStatusMessageService _status;
    private readonly string _objectInstanceName;
    private readonly string _objectTypeName;
    private readonly string _singularFormat = "Are you sure you want to delete this {0}?";
    private readonly string _pluralFormat = "Are you sure you want to delete these {0}?";
    public readonly bool Confirmed;
    private bool _multipleItems;
    private readonly Dispatcher _dispatcher;

    /// <summary>
    /// Displays status messages and performs confirmation for deleting an object
    /// </summary>
    /// <param name="status">the instance of IStatusMessageService that will display messages</param>
    /// <param name="objectInstanceName">The name of the instance of the object to delete</param>
    /// <param name="objectTypeName">The type of object to delete - specify the plural if needed. </param>
    /// <param name="multipleItems">Whether or not there are multiple items, to use plural strings.</param>
    public DeleteManager(IStatusMessageService status, string objectTypeName, string objectInstanceName = null, bool multipleItems = false)
    {
        _dispatcher = Wpf.Application.Current.Dispatcher;
        _status = status;
        _objectInstanceName = objectInstanceName;
        _objectTypeName = objectTypeName;
        _multipleItems = multipleItems;

        Confirmed = ConfirmDelete();

        if (Confirmed)
        {
            var message = _multipleItems
                ? string.Format("Deleting multiple {0}...", _objectTypeName)
                : string.Format("Deleting {0}: {1}...", _objectTypeName, _objectInstanceName);

            // this is the message that doesn't display... dunno why
            _dispatcher.BeginInvoke(new Action(() => _status.Status.StaticMessage(message)));
        }
    }

    private bool ConfirmDelete()
    {
        var format = _multipleItems
            ? _pluralFormat
            : _singularFormat;

        var message = string.Format(format, (_objectInstanceName == null)
            ? _objectTypeName
            : string.Format("{0} : \"{1}\"", _objectTypeName, _objectInstanceName));

        return (bool)_dispatcher.Invoke(
            new Func<bool>(() => 
            // StatusControl is a modal window
            StatusControl.ConfirmMessage("Delete Warning", message)));
    }

    public void Dispose()
    {
        if (Confirmed)
        {
            var message = _multipleItems
                ? string.Format("Multipe {0} Deleted.", _objectTypeName)
                : string.Format("{0} Deleted: {1}.", _objectTypeName, _objectInstanceName);

            // THIS message displays just fine.  
            _dispatcher.BeginInvoke(new Action(() => _status.Status.InfoMessage(message)));
        }
    }
}

......它正在被这样使用:

var deleteId = Poco.Id;

using (var delete = new DeleteManager(StatusMessageService, "Poco", Poco.Name))
{
    if (delete.Confirmed)
    {
        var success = _dataProvider.DeleteCurriculum(deleteId);
        if (success)
        { EventAggregator.GetEvent<DeletedNotification>().Publish(deleteId); }
    }
}

InfoMessage()中的Dispose()电话显示正常,但构造函数中的StaticMessage()调用永远显示...即使我将其更改为使用InfoMessage()代替......我无法确定原因,也无法解决问题。

0 个答案:

没有答案