public void Publish <TEvent>(TEvent sampleEvent)
        {
            Logger.LogFormat(LogSeverity.Verbose, GetType().FullName, "{0} : {1} : {2}", MethodBase.GetCurrentMethod().Name,
                             sampleEvent.GetType().FullName, sampleEvent);

            if (!_subscribers.ContainsKey(typeof(TEvent)))
            {
                // no one is listening
                return;
            }

            // snapshot the list
            Monitor.Enter(_mutex);
            var subscribers = (from sub in _subscribers[typeof(TEvent)] select sub).ToArray();

            Monitor.Exit(_mutex);

            // now filter through and mark any dead subscriptions
            var dead = new List <Tuple <bool, WeakReference> >();

            foreach (var sub in subscribers)
            {
                var sink = sub.Item2.Target as IEventSink <TEvent>;
                if (sink == null || !sub.Item2.IsAlive)
                {
                    dead.Add(sub);
                }
                else
                {
                    if (sub.Item1)
                    {
                        OpiumHelper.ExecuteOnUI(() => sink.HandleEvent(sampleEvent));
                    }
                    else
                    {
                        sink.HandleEvent(sampleEvent);
                    }
                }
            }

            // scrub the dead subscriptions
            Monitor.Enter(_mutex);
            foreach (var deadSub in dead.Where(deadSub => _subscribers[typeof(TEvent)].Contains(deadSub)))
            {
                _subscribers[typeof(TEvent)].Remove(deadSub);
            }
            Monitor.Exit(_mutex);
        }
        public UserControl GetNonSharedView(string viewTag, object dataContext, Dictionary <string, object> parameters)
        {
            var viewEntry = (from factory in ViewFactory
                             where factory.Metadata.ExportedViewType.Equals(viewTag)
                             select factory).FirstOrDefault();

            if (viewEntry == null)
            {
                return(null);
            }

            var view         = viewEntry.CreateExport().Value;
            var viewMetadata = viewEntry.Metadata;

            BindViewModel(view, dataContext);

            var viewModel = (IViewModel)dataContext;

            if (viewModel != null)
            {
                if (viewMetadata.DeactivateOnUnload)
                {
                    view.Unloaded += (o, e) =>
                                     viewModel.Deactivate(viewMetadata.ExportedViewType);
                }

                viewModel.RegisterVisualState(viewTag,
                                              (state, transitions) =>
                                              OpiumHelper.ExecuteOnUI(
                                                  () => VisualStateManager.GoToState(view, state,
                                                                                     transitions)));
                viewModel.RegisteredViews.Add(viewTag);
                viewModel.Initialize();
                RoutedEventHandler loaded = null;
                loaded = (o, e) =>
                {
                    // ReSharper disable AccessToModifiedClosure
                    ((UserControl)o).Loaded -= loaded;
                    // ReSharper restore AccessToModifiedClosure
                    viewModel.Activate(viewTag, parameters);
                };
                view.Loaded += loaded;
            }
            return(view);
        }
        public bool ActivateView(string viewName, IDictionary <string, object> parameters)
        {
            Logger.LogFormat(LogSeverity.Verbose, GetType().FullName, Resources.ViewModelRouter_ActivateView,
                             MethodBase.GetCurrentMethod().Name,
                             viewName);

            if (HasView(viewName))
            {
                var viewInfo = GetViewInfo(viewName);
                var view     = viewInfo.Value;

                var viewModelInfo = GetViewModelInfoForView(viewName);

                if (viewModelInfo != null)
                {
                    var firstTime = !viewModelInfo.IsValueCreated;

                    var viewModel = viewModelInfo.Value;

                    if (!viewModel.RegisteredViews.Contains(viewName))
                    {
                        viewModel.RegisterVisualState(viewName,
                                                      (state, transitions) =>
                                                      OpiumHelper.ExecuteOnUI(
                                                          () => VisualStateManager.GoToState(view, state,
                                                                                             transitions)));
                        BindViewModel(view, viewModel);
                        viewModel.RegisteredViews.Add(viewName);
                    }

                    if (firstTime)
                    {
                        if (viewInfo.Metadata.DeactivateOnUnload)
                        {
                            view.Unloaded += (o, e) => viewModel.Deactivate(viewName);
                        }
                        viewModel.Initialize();
                        RoutedEventHandler loaded = null;
                        loaded = (o, e) =>
                        {
                            // ReSharper disable AccessToModifiedClosure
                            ((UserControl)o).Loaded -= loaded;
                            // ReSharper restore AccessToModifiedClosure
                            viewModel.Activate(viewName, parameters);
                        };
                        view.Loaded += loaded;
                    }
                    else
                    {
                        viewModel.Activate(viewName, parameters);
                    }

                    if (parameters.ContainsKey(Constants.WINDOW_TITLE))
                    {
                        viewModel.SetTitle(parameters.ParameterValue <string>(Constants.WINDOW_TITLE));
                    }
                }

                if (parameters.ContainsKey(Constants.AS_WINDOW) &&
                    parameters.ParameterValue <bool>(Constants.AS_WINDOW))
                {
                    var title = string.Empty;

                    if (parameters.ContainsKey(Constants.WINDOW_TITLE))
                    {
                        title = parameters.ParameterValue <string>(Constants.WINDOW_TITLE);
                    }

                    var height = 480.0;
                    if (parameters.ContainsKey(Constants.WINDOW_HEIGHT))
                    {
                        height = parameters.ParameterValue <double>(Constants.WINDOW_HEIGHT);
                    }

                    var width = 640.0;
                    if (parameters.ContainsKey(Constants.WINDOW_WIDTH))
                    {
                        width = parameters.ParameterValue <double>(Constants.WINDOW_WIDTH);
                    }

                    var window = new Window
                    {
                        Height      = height,
                        Width       = width,
                        WindowState = WindowState.Normal,
                        Topmost     = true,
                        Title       = title,
                        Content     = view,
                        Visibility  = Visibility.Visible
                    };
                    parameters.Add(Constants.WINDOW_REFERENCE, window);
                }

                return(true);
            }
            return(false);
        }
Example #4
0
        /// <summary>
        /// Override to provide the value requested
        /// </summary>
        /// <param name="serviceProvider">The service provider for pulling information from the Xaml parser</param>
        /// <returns>The instance of the view model if it can be resolved</returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Router == null)
            {
                ServiceLocator.Current.GetAllInstances(typeof(IViewModelRouter));
            }

            if (Router == null)
            {
                return(null);                // not in runtime, return nothing
            }
            var ipvt = serviceProvider.GetService(typeof(IProvideValueTarget))
                       as IProvideValueTarget;

            var vm =
                CreateNew ?
                Router.GetNonSharedViewModel(ViewModelName) :
                Router.ResolveViewModel <IViewModel>(true, ViewModelName);

            if (ipvt != null)
            {
                var view = ipvt.TargetObject as UserControl;

                if (view == null)
                {
                    return(vm);
                }

                var viewName = view.GetType().FullName;

                var baseViewModel = vm;

                if (!baseViewModel.RegisteredViews.Contains(viewName))
                {
                    baseViewModel.RegisterVisualState(viewName,
                                                      (state, transitions) =>
                                                      OpiumHelper.ExecuteOnUI(
                                                          () => VisualStateManager.GoToState(view, state,
                                                                                             transitions)));
                    BindViewModel(view, baseViewModel);
                    baseViewModel.RegisteredViews.Add(viewName);
                }


                baseViewModel.Initialize();
                RoutedEventHandler loaded = null;
                loaded = (o, e) =>
                {
                    ((UserControl)o).Loaded -= loaded;
                    baseViewModel.Activate(viewName);
                };
                view.Loaded += loaded;

                if (CallDeactivateOnUnload)
                {
                    view.Unloaded += (o, e) => vm.Deactivate(viewName);
                }
            }

            return(vm);
        }