Example #1
0
        public MainPage()
        {
            InitializeComponent();

            // workflows yield IWorkflow objects. We can optionally handled errors encountered
            // during the workflow, at which point the workflow is terminated.
            WorkflowController.Begin(Workflow(),
                                     ex => JounceHelper.ExecuteOnUI(() => MessageBox.Show(ex.Message)));
        }
Example #2
0
        /// <summary>
        ///     Publish an event
        /// </summary>
        /// <typeparam name="TEvent">The type of the event to publish</typeparam>
        /// <param name="sampleEvent">The event data</param>
        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)
                    {
                        JounceHelper.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);
        }
Example #3
0
        /// <summary>
        ///     Returns a non-shared version of the view
        /// </summary>
        /// <param name="viewTag">The view tag</param>
        /// <param name="dataContext">Data context to wire</param>
        /// <param name="parameters">Parameters to pass to the view</param>
        /// <returns>The view</returns>
        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 = dataContext as IViewModel;

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

                viewModel.RegisterVisualState(viewTag,
                                              (state, transitions) =>
                                              JounceHelper.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);
        }
Example #4
0
        /// <summary>
        ///     Activates a view and returns the view
        /// </summary>
        /// <param name="viewName">The view name</param>
        /// <param name="parameters">The parameters for the view</param>
        /// <returns>The user control</returns>
        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) =>
                                                      JounceHelper.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 #5
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)
        {
            // Ignore when in designer
            if (DesignerProperties.IsInDesignTool)
            {
                return(null);
            }

            if (Router == null)
            {
                CompositionInitializer.SatisfyImports(this);
            }

            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) =>
                                                      JounceHelper.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);
        }
Example #6
0
 static void GroupCurrentStateChanged(object sender, VisualStateChangedEventArgs e)
 {
     JounceHelper.ExecuteOnUI(() => MessageBox.Show(string.Format("Transition {0}=>{1}", e.OldState.Name, e.NewState.Name)));
 }