在Caliburn IWindowManager界面中集成闪烁任务栏

时间:2015-01-10 01:01:49

标签: c# wpf caliburn.micro

为了使任务栏闪烁,我找到了以下代码:https://stackoverflow.com/a/5118285/1830639,它给了我两个我关心的扩展方法:

window.FlashWindow(5);
window.StopFlashingWindow();

我正在创建某种聊天信使,有两个有趣的场景:

  1. 当用户点击聊天已打开的聊天时,焦点打开窗口。
  2. 当消息到达已打开的聊天时,闪烁打开窗口。
  3. 1。我已完成以下任务(https://stackoverflow.com/a/14765848/1830639):

     public class MetroWindowManager : WindowManager
    {
        private IDictionary<WeakReference, WeakReference> windows = new Dictionary<WeakReference, WeakReference>();
    
        public override void ShowWindow(object rootModel, object context = null, IDictionary<string, object> settings = null)
        {
            NavigationWindow navWindow = null;
    
            if (Application.Current != null && Application.Current.MainWindow != null)
            {
                navWindow = Application.Current.MainWindow as NavigationWindow;
            }
    
            if (navWindow != null)
            {
                var window = CreatePage(rootModel, context, settings);
                navWindow.Navigate(window);
            }
            else
            {
                var window = GetExistingWindow(rootModel);
                if (window == null)
                {
                    window = CreateWindow(rootModel, false, context, settings);
                    windows.Add(new WeakReference(rootModel), new WeakReference(window));
                    window.Show();
                }
                else
                {
                    window.Focus();
                }
            }
    
        }
    

    ChatManager

     public class ChatManager : IChatManager
    {
        private readonly IChatWindowSettings chatWindowSettings;
        private readonly IWindowManager windowManager;
        private IDictionary<WeakReference, WeakReference> chats;
    
        public ChatManager(IChatWindowSettings chatWindowSettings, IWindowManager windowManager)
        {
            this.chatWindowSettings = chatWindowSettings;
            this.windowManager = windowManager;
    
            chats = new Dictionary<WeakReference, WeakReference>();
        }
    
        public void OpenFor(Contact contact)
        {
            var settings = chatWindowSettings.Create();
            var viewModel = CreateOrGetViewModel(contact);
            windowManager.ShowWindow(viewModel, null, settings);
        }
    
        private ChatViewModel CreateOrGetViewModel(Contact contact)
        {
            ChatViewModel viewModel;
            if (!chats.Any(c => c.Key.IsAlive && c.Key.Target == contact))
            {
                viewModel = new ChatViewModel(contact);
                chats.Add(new WeakReference(contact), new WeakReference(viewModel));
            }
            else
            {
                var chat = chats.Single(d => d.Key.Target == contact).Value;
                if (!chat.IsAlive)
                {
                    viewModel = new ChatViewModel(contact);
                    chats.Add(new WeakReference(contact), new WeakReference(viewModel));
                }
                else
                {
                    viewModel = chat.Target as ChatViewModel;
                }
            }
    
            return viewModel;
        }
    

    如您所见,我依赖 IWindowManager 界面。现在对于 2。我想到在 IWindowManager Blink()上实现扩展方法,但是在扩展方法中我不想可以访问我在 MetroWindowManager 中创建的 WeakReferences windows 变量):

     public void MessageFor(Contact contact, IChatMessage message)
        {
            var viewModel = CreateOrGetViewModel(contact);
            windowManager.Blink(viewModel);
        }
    

    有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我已经弄明白了。如果有人有兴趣,可以分享代码。

我已经创建了自己IWindowManager,扩展了Caliburn.Micro.IWindowManager。问题是如何获得两个接口的相同实例

/// <summary>
/// Extends Caliburn.Micro.IWindowManager functionalities, such as blinking windows.
/// </summary>
public interface IWindowManager : Caliburn.Micro.IWindowManager
{
    void Blink(object rootModel);
}

<强> MetroWindowManager

public class MetroWindowManager : WindowManager, Communicator.Core.IWindowManager
{
    private IDictionary<WeakReference, WeakReference> windows = new Dictionary<WeakReference, WeakReference>();

    public void Blink(object rootModel)
    {
        //have access to 'windows' dictionary
    }

SimpleInjector Bootstrapper

var windowManager = new MetroWindowManager();
        container = new Container();
        container.Register<Communicator.Core.IWindowManager>(()=> windowManager);
        container.Register<Caliburn.Micro.IWindowManager>(() => windowManager);

每当我依赖IWindowManagerCaliburn.Micro.IWindowManager时,IoC都会传递相同的实例。