Another .NET Blog

To content | To menu | To search

Friday 25 March 2011

[CaliburnMicro] Embedded MessageBox

In this article, I'll show you how to use Caliburn Micro to create embedded message boxes. I will use the default implementation you can obtain in CM's big brother source code, and tweak it a little to have a better look.

Continue reading...

Friday 18 December 2009

Methode d'extension pour agir sur l'UI depuis un autre thread en WPF

Je profite d'en avoir parlé lors de mon dernier post pour vous donner une méthode d'extension toute faite qui vous permettra d'appeler automatiquement BeginInvoke sur le Dispatcher d'un contrôle WPF si l'action que vous voulez exécuter sur le contrôle se passe depuis un autre thread que celui sur lequel tourne l'application:

namespace System.Windows.Threading
{
    public static class DispatcherExtensions
    {
        /// 
        /// Executes the action on the thread of the UI if needed, or on the current thread if not
        /// 
        /// The Dispatcher of the control
        /// The action to execute
        /// The dispatcher or the action are null
        public static void CheckAccessAndDo(this Dispatcher dispatcher, Action action)
        {
            CheckAccessAndDo(dispatcher, DispatcherPriority.Normal, action);
        }

        /// 
        /// Executes the action on the thread of the UI if needed, or on the current thread if not
        /// 
        /// The Dispatcher of the control
        /// The priority of the action
        /// The action to execute
        /// The dispatcher or the action are null
        public static void CheckAccessAndDo(this Dispatcher dispatcher, DispatcherPriority priority, Action action)
        {
            if (dispatcher == null) throw new ArgumentNullException("dispatcher");
            if (action == null) throw new ArgumentNullException("action");

            if (dispatcher.CheckAccess())
                action();
            else
            {
                dispatcher.BeginInvoke(priority, action);
            }
        }
    }
}

Pour l'utiliser, c'est très simple et évident:

label.Dispatcher.CheckAccessAndDo(delegate { label.Content = "foo"; } );

De quoi rendre la vie plus facile :p