Ejemplo n.º 1
0
        private static DataConstantValue[] MergeParameters(IViewModel parentViewModel, ObservationMode?observationMode, DataConstantValue[] parameters)
        {
            if (observationMode == null && parentViewModel == null)
            {
                return(parameters);
            }

            var values = new List <DataConstantValue>();

            if (observationMode.HasValue)
            {
                values.Add(InitializationConstants.ObservationMode.ToValue(observationMode.Value));
            }

            if (parentViewModel != null)
            {
                values.Add(InitializationConstants.ParentViewModel.ToValue(parentViewModel));
            }
            if (parameters != null && parameters.Length != 0)
            {
                for (int index = 0; index < parameters.Length; index++)
                {
                    DataConstantValue parameter = parameters[index];
                    if (values.Any(value => value.DataConstant.Equals(parameter.DataConstant)))
                    {
                        continue;
                    }
                    values.Add(parameter);
                }
            }
            return(values.ToArray());
        }
        public void GetViewModelMethodShouldUseCorrectParameters()
        {
            const IocContainerCreationMode iocContainerCreationMode = IocContainerCreationMode.ParentViewModel;
            const ObservationMode          listenType = ObservationMode.Both;
            var  constantValue = DataConstantValue.Create(new DataConstant <string>("test"), "test");
            bool isInvoked     = false;

            var providerMock = new ViewModelProviderMock();
            var func         = IocContainer.GetFunc;

            IocContainer.GetFunc = (type, s, arg3) =>
            {
                if (type == typeof(IViewModelProvider))
                {
                    return(providerMock);
                }
                return(func(type, s, arg3));
            };
            ViewModelBase         viewModel = GetViewModelBase();
            Action <IDataContext> check     = context =>
            {
                context.GetData((DataConstant <string>)constantValue.DataConstant).ShouldEqual(constantValue.Value);
                context.GetDataTest(InitializationConstants.ObservationMode).ShouldEqual(listenType);
                context.GetDataTest(InitializationConstants.IocContainerCreationMode).ShouldEqual(iocContainerCreationMode);
                isInvoked = true;
            };

            providerMock.GetViewModel = (@delegate, context) =>
            {
                check(context);
                return(@delegate(IocContainer));
            };
            providerMock.GetViewModelType = (type, context) =>
            {
                check(context);
                return(new ViewModelBaseWithContext());
            };


            viewModel.GetViewModel(getViewModel: adapter => new ViewModelBaseWithContext(),
                                   containerCreationMode: iocContainerCreationMode, observationMode: listenType, parameters: constantValue);
            isInvoked.ShouldBeTrue();
            isInvoked = false;

            viewModel.GetViewModel(adapter => new ViewModelBaseWithContext(), listenType, iocContainerCreationMode, constantValue);
            isInvoked.ShouldBeTrue();
            isInvoked = false;

            viewModel.GetViewModel(typeof(ViewModelBaseWithContext), listenType, iocContainerCreationMode, constantValue);
            isInvoked.ShouldBeTrue();
            isInvoked = false;

            viewModel.GetViewModel <ViewModelBaseWithContext>(listenType, iocContainerCreationMode, constantValue);
            isInvoked.ShouldBeTrue();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used when the application is launched to open a specific file, to display
        /// search results, and so forth.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override async void OnLaunched(LaunchActivatedEventArgs e)
        {
//#if DEBUG
//            if (System.Diagnostics.Debugger.IsAttached)
//            {
//                this.DebugSettings.EnableFrameRateCounter = true;
//            }
//#endif
            int?idSensor = GetIdSensorFromArguments(e.Arguments);

            Frame rootFrame = Window.Current.Content as Frame;
            WinRTBootstrapperBase bootstrapper = null;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                var autofacContainer = new AutofacContainer();

                if (idSensor == null)
                {
                    bootstrapper = new Bootstrapper <ThermometerApp>(rootFrame, autofacContainer);
                }
                else
                {
                    bootstrapper = new Bootstrapper <SensorHistoryVm>(rootFrame, autofacContainer);
                    bootstrapper.InitializationContext = new DataContext(DataConstantValue.Create(Constants.IdSensor, idSensor.Value));
                }
                await bootstrapper.InitializeAsync();

                //Associate the frame with a SuspensionManager key
                SuspensionManager.RegisterFrame(rootFrame, "AppFrame");

                // TODO: change this value to a cache size that is appropriate for your application
                rootFrame.CacheSize = 1;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    // Restore the saved session state only when appropriate
                    try
                    {
                        await SuspensionManager.RestoreAsync();
                    }
                    catch (SuspensionManagerException)
                    {
                        // Something went wrong restoring state.
                        // Assume there is no state and continue
                    }
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (rootFrame.Content == null)
            {
#if WINDOWS_PHONE_APP
                // Removes the turnstile navigation for startup.
                if (rootFrame.ContentTransitions != null)
                {
                    this.transitions = new TransitionCollection();
                    foreach (var c in rootFrame.ContentTransitions)
                    {
                        this.transitions.Add(c);
                    }
                }

                rootFrame.ContentTransitions = null;
                rootFrame.Navigated         += this.RootFrame_FirstNavigated;
#endif

                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                bootstrapper?.Start();
            }

            // Ensure the current window is active
            Window.Current.Activate();

            if (bootstrapper == null && idSensor != null)
            {
                await ServiceProvider.Get <ICurrentWeatherManager>().ShowSensorHistoryAsync(idSensor.Value, null);
            }
        }