コード例 #1
0
        public static InfoWindow Create(string title, string text, string image_url, string action_name, Action action, string sound_file = null, Brush box_brush = null, Brush button_brush = null)
        {
            InfoWindow w = null;

            if (text.Length > Settings.View.InfoToastMaxTextLength)
            {
                text = text.Remove(Settings.View.InfoToastMaxTextLength, text.Length - Settings.View.InfoToastMaxTextLength) + "<...>";
            }

            Action a = () =>
            {
                w = new InfoWindow(title, text, image_url, action_name, action);
                w.SetAppearance(box_brush, button_brush);
                WindowInteropHelper h = new WindowInteropHelper(w);
                h.EnsureHandle();
                w.Show();
                ThreadRoutines.StartTry(() =>
                {
                    Thread.Sleep(Settings.View.InfoToastLifeTimeInSecs * 1000);
                    w.Dispatcher.BeginInvoke((Action)(() => { w.Close(); }));
                });
                if (string.IsNullOrWhiteSpace(sound_file))
                {
                    sound_file = Settings.View.InfoSoundFile;
                }
                sound_file = PathRoutines.GetAbsolutePath(sound_file);
                SoundPlayer sp = new SoundPlayer(sound_file);
                sp.Play();
            };

            lock (ws)
            {
                if (dispatcher == null)
                {//!!!the following code does not work in static constructor because creates a deadlock!!!
                    dispatcher_t = ThreadRoutines.StartTry(() =>
                    {
                        if (invisible_owner_w == null)
                        {//this window is used to hide notification windows from Alt+Tab panel
                            invisible_owner_w               = new Window();
                            invisible_owner_w.Width         = 0;
                            invisible_owner_w.Height        = 0;
                            invisible_owner_w.WindowStyle   = WindowStyle.ToolWindow;
                            invisible_owner_w.ShowInTaskbar = false;
                            invisible_owner_w.Show();
                            invisible_owner_w.Hide();
                        }

                        if (dispatcher == null)
                        {
                            //dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
                            dispatcher = System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread);
                            System.Windows.Threading.Dispatcher.Run();
                        }
                    }, null, null, true, ApartmentState.STA);
                    if (!SleepRoutines.WaitForCondition(() => { return(dispatcher != null); }, 3000))
                    {
                        throw new Exception("Could not get dispatcher.");
                    }
                }
            }
            dispatcher.Invoke(a);
            return(w);
        }