public BottomViewModel( // This will be a unique IUpbeatService created and injected by the IUpbeatStack specifically for this ViewModel. IUpbeatService upbeatService, // This is a shared singleton service. SharedTimer sharedTimer) { _upbeatService = upbeatService ?? throw new NullReferenceException(nameof(upbeatService)); _sharedTimer = sharedTimer ?? throw new NullReferenceException(nameof(sharedTimer)); // Registering a CloseCallback allows the ViewModel to prevent itself from closing. For example: if there is unsaved work. This can also completely prevent the application from shutting down. CloseCallbacks can be either async or non-async methods/lambdas. _upbeatService.RegisterCloseCallback(AskBeforeClosingAsync); _sharedTimer.Ticked += SharedTimerTicked; // DelegateCommand is a common convenience ICommand implementation to call methods or lambda expressions when the command is executed. It supports both async and non-async methods/lambdas. OpenMenuCommand = new DelegateCommand( // Create a Parameters object for a ViewModel and pass it to the IUpbeatStack using OpenViewModel. The IUpbeatStack will use the configured mappings to create the appropriate ViewModel from the Parameters type. () => _upbeatService.OpenViewModel( new MenuViewModel.Parameters())); OpenSharedListCommand = new DelegateCommand( () => _upbeatService.OpenViewModel( new SharedListViewModel.Parameters())); OpenRandomDataCommand = new DelegateCommand( () => _upbeatService.OpenViewModel( new RandomDataViewModel.Parameters())); }
public MenuViewModel( // This will be a unique IUpbeatService created and injected by the IUpbeatStack specifically for this ViewModel. IUpbeatService upbeatService, // This ViewModel requires an async delegate to be provided, so it can start closing the application. Func <Task> closeApplicationCallbackAsync, // This is a shared singleton service. SharedTimer sharedTimer) { _upbeatService = upbeatService ?? throw new NullReferenceException(nameof(upbeatService)); _ = closeApplicationCallbackAsync ?? throw new NullReferenceException(nameof(closeApplicationCallbackAsync)); _sharedTimer = sharedTimer ?? throw new NullReferenceException(nameof(sharedTimer)); _sharedTimer.Ticked += SharedTimerTicked; // DelegateCommand is a common convenience ICommand implementation to call methods or lambda expressions when the command is executed. It supports both async and non-async methods/lambdas. ExitCommand = new DelegateCommand(closeApplicationCallbackAsync); OpenRandomDataCommand = new DelegateCommand( () => { // Create a Parameters object for a ViewModel and pass it to the IUpbeatStack using OpenViewModel. The IUpbeatStack will use the configured mappings to create the appropriate ViewModel from the Parameters type. _upbeatService.OpenViewModel(new RandomDataViewModel.Parameters()); // Since this is Side Menu, it can close after the requested ViewModel is opened. _upbeatService.Close(); }); OpenSharedListCommand = new DelegateCommand( () => { _upbeatService.OpenViewModel(new SharedListViewModel.Parameters()); _upbeatService.Close(); }); }
public RandomDataViewModel( // This will be a unique IUpbeatService created and injected by the IUpbeatStack specifically for this ViewModel. IUpbeatService upbeatService, // This will be an injected transient instance of a Random service. Random random, // This is a shared singleton service. SharedTimer sharedTimer) { _upbeatService = upbeatService ?? throw new ArgumentNullException(nameof(upbeatService)); _random = random ?? throw new ArgumentNullException(nameof(random)); _sharedTimer = sharedTimer ?? throw new ArgumentNullException(nameof(sharedTimer)); _sharedTimer.Ticked += SharedTimerTicked; // DelegateCommand is a common convenience ICommand implementation to call methods or lambda expressions when the command is executed. It supports both async and non-async methods/lambdas. OpenPositionedPopupCommand = new DelegateCommand <Func <Point> >( // Create a Parameters object for a ViewModel and pass it to the IUpbeatStack using OpenViewModel. The IUpbeatStack will use the configured mappings to create the appropriate ViewModel from the Parameters type. pointGetter => _upbeatService.OpenViewModel( new PopupViewModel.Parameters { Message = "This popup appears on top of\nthe button that opened it.", // The pointGetter parameter is a Func<Point> created by the View that will return the position within the window of the control that executed this command. See the bindings in View\RandomDataControl.xaml for details on how to bind a pointGetter() as a CommandParameter. Position = pointGetter(), })); RefreshDataCommand = new DelegateCommand(RefreshData); Data = new ReadOnlyObservableCollection <KeyValuePair <string, string> >(_data); for (var i = 0; i < 100; i++) { _data.Add(CreateRandomKeyValuePair()); } }
public SharedListDataViewModel( // This will be a unique IUpbeatService created and injected by the IUpbeatStack specifically for this ViewModel. IUpbeatService upbeatService, // This is a scoped service shared between this ViewModel and other ViewModel or scoped/transient service dependencies. SharedList sharedList) { _upbeatService = upbeatService ?? throw new ArgumentNullException(nameof(upbeatService)); _sharedList = sharedList ?? throw new ArgumentNullException(nameof(sharedList)); Strings = new ReadOnlyObservableCollection <string>(_strings); _strings.Synchronize(_sharedList.Strings); _sharedList.StringAdded += SharedListStringAdded; // DelegateCommand is a common convenience ICommand implementation to call methods or lambda expressions when the command is executed. It supports both async and non-async methods/lambdas. AddStringCommand = new DelegateCommand <Func <Point> >(ExecuteAddStringAsync, pg => _strings.Count < 10); }
public ConfirmPopupViewModel // This will be a unique IUpbeatService created and injected by the IUpbeatStack specifically for this ViewModel. (IUpbeatService upbeatService, // These are the parameters the parent used when opening this ViewModel. The IUpbeatService can inject the Parameters object into this constructor to pass initialization data or callbacks. Parameters parameters, // This is a shared singleton service. SharedTimer sharedTimer) : base(parameters, sharedTimer) { // DelegateCommand is a common convenience ICommand implementation to call methods or lambda expressions when the command is executed. It supports both async and non-async methods/lambdas. ConfirmCommand = new DelegateCommand( () => { parameters?.ConfirmCallback?.Invoke(); // Will close this ViewModel. upbeatService.Close(); }); }
public SharedListViewModel( // This will be a unique IUpbeatService created and injected by the IUpbeatStack for this ViewModel and child ViewModels. IUpbeatService upbeatService, // This is a scoped service shared between this ViewModel and other ViewModel or scoped/transient service dependencies. SharedList sharedList, // This is a shared singleton service. SharedTimer sharedTimer, // This is a child ViewModel, which can help with separating concerns and keep ViewModels from being too complicated. Child ViewModels share an IUpbeatService and any scoped services with their parents. SharedListDataViewModel sharedListDataViewModel) { _upbeatService = upbeatService ?? throw new NullReferenceException(nameof(upbeatService)); _sharedTimer = sharedTimer ?? throw new NullReferenceException(nameof(sharedTimer)); _sharedList = sharedList ?? throw new ArgumentNullException(nameof(sharedList)); SharedListDataViewModel = sharedListDataViewModel ?? throw new ArgumentNullException(nameof(sharedListDataViewModel)); _sharedTimer.Ticked += SharedTimerTicked; _sharedList.StringAdded += SharedListStringAdded; CloseCommand = new DelegateCommand(_upbeatService.Close); }