Ejemplo n.º 1
0
        /// <summary>
        /// Constructor - registers with the message mediator and hooks up any imports/exports
        /// with the default MEF catalog
        /// </summary>
        public ViewModel()
        {
            // Register with the message mediator - this will locate and bind all message
            // targets on this instance. Shouldn't really need to check for existence - it should
            // always be present, but just in case someone Unregisters it..
            var mediator = Resolve <IMessageMediator>();

            if (mediator != null)
            {
                mediator.Register(this);
            }

            // Hook up any MEF imports/exports
            IDynamicResolver loader = Resolve <IDynamicResolver>();

            if (loader != null)
            {
                try
                {
                    loader.Compose(this);
                }
                catch (CompositionException)
                {
                    // Can throw if in invalid state - i.e. creating VM on behalf of a view.
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This creates the WPF window from a key.
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="dataContext">DataContext (state) object</param>
        /// <param name="setOwner">True/False to set ownership to MainWindow</param>
        /// <param name="completedProc">Callback</param>
        /// <param name="isModal">True if this is a ShowDialog request</param>
        /// <returns>Success code</returns>
        private Window CreateWindow(string key, object dataContext, bool setOwner, EventHandler <UICompletedEventArgs> completedProc, bool isModal)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            // If we've not scanned for available exported views, do so now.
            if (!_haveLoadedVisuals)
            {
                CheckForDynamicRegisters();
            }

            Type winType;

            lock (_registeredWindows)
            {
                if (!_registeredWindows.TryGetValue(key, out winType))
                {
                    return(null);
                }
            }

            // Create the top level window
            var win = (Window)Activator.CreateInstance(winType);

            if (setOwner)
            {
                win.Owner = Application.Current.MainWindow;
            }

            // Register the view with MEF to resolve any imports.
            try
            {
                _dynamicLoader.Compose(win);
            }
            catch (CompositionException)
            {
            }

            if (dataContext != null)
            {
                win.DataContext = dataContext;
                var bvm = dataContext as ViewModel;

                // Wire up the event handlers.  Go through the dispatcher in case the window
                // is being created on a secondary thread so the primary thread can invoke the
                // event handlers.
                if (bvm != null)
                {
                    if (isModal)
                    {
                        bvm.CloseRequest += ((s, e) => win.Dispatcher.Invoke((Action)(() =>
                        {
                            try
                            {
                                win.DialogResult = e.Result;
                            }
                            catch (InvalidOperationException)
                            {
                                win.Close();
                            }
                        }), null));
                    }
                    else
                    {
                        bvm.CloseRequest += ((s, e) => win.Dispatcher.Invoke((Action)(win.Close), null));
                    }

                    bvm.ActivateRequest += ((s, e) => win.Dispatcher.Invoke((Action)(() => win.Activate()), null));
                }
            }

            if (completedProc != null)
            {
                win.Closed +=
                    (s, e) =>
                    completedProc(this,
                                  new UICompletedEventArgs {
                    State = dataContext, Result = (isModal) ? win.DialogResult : null
                });
            }

            return(win);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Construct the service provider.  Resolve all the imported services.
 /// </summary>
 public ServiceProvider()
 {
     _mefLoader.Compose(this);
 }