Introduction
In Tip #1, I created a new UWP project with a template from Windows Template Studio v1.1. The app is a Navigation Pane style project that uses MVVMLight and contains Web Views to display three different websites.Let's take a look at a few aspects of the generated project.
NuGet Packages
Besides the basic UWP NuGet package, three other packages were added to my project when it was created.- Microsoft.Toolkit.Uwp.UI.Controls - Part of the UWP Community Toolkit
- Microsoft.Xaml.Behaviors.Uwp.Managed - XAML Behaviors
- MvvmLight - Our MVVM toolkit of choice
Views
Views were created for the application's shell, the main page which is generated without any content, and my three Web Views.- ShellPage.xaml
- MainPage.xaml
- MorningDewPage.xaml
- UWPTipsPage.xaml
- WPFTipsPage.xaml
Each web view contains some style code for in the Page.Resources for styling the browser buttons. If you have multiple web views, this XAML could be centralized and reused across your pages. There is also some code for handling the loading behaviors, visibility, and the WebView (Windows.UI.Xaml.Controls.WebView) itself.
The MainPage is essentially empty with the exception of some Xaml to handle the VisualStates. I added a couple of TextBlocks with a welcome message and some information about navigating to the other pages.
ViewModels
There are ViewModels corresponding to each view listed above. There is another for ShellNavigationItem. This facilitates handling multiple pages in the navigation app without hard-coding them in the Shell's view somewhere. There is also a ViewModelLocator class, which will be familiar to anyone who has used MVVMLight. This answer on StackOverflow does a great job of explaining its purpose.The ShellViewModel uses the ShellNavigationItem and a NavigationServiceEx to switch between the views of its main content panel.
private void Navigate(object item) { var navigationItem = item as ShellNavigationItem; if (navigationItem != null) { NavigationService.Navigate(navigationItem.ViewModelName); } }Most of the code in the ViewModels for each web view is dedicated to handling. I would recommend refactoring most of this code out to a common base class if you are going to be working with more than one or two web views in a project. Much of the logic is centered around handling navigation between websites and web pages within the WebView control: browsing forward/back, retry, refresh, etc.
Services and Helpers
There are two service classes in the project, ActivationService and NavigationServiceEx. Let's start with the NavigationServiceEx used by the Shell. Here's the class diagram of this service.It contains all you need to browse between your child views, including browser-style back/forward functionality.
The ActivationService encapsulates our app's activation behavior and its back stack in Windows, working with the NavigationServiceEx for this.
Wrap-Up
The project I generated with Windows Template Studio, created a great starting point for application that can be used to navigate a few different websites. If you create something similar, I would recommend some refactoring to reduce code duplication between Views and ViewModels of the same type. This will lead to much greater maintainability moving forward. Otherwise, it's a really solid way to get started on your next project.Happy coding!
Alvin: Thank you! WTS looks great but there is not much out there (yet) on guidance on how to modify the generated code in order to produce a 'real' LOB app. Your new Blog fills the void nicely. I am looking forward to tips on building real apps with WTS and the UWP Community Toolkit. Keep up the good work.
ReplyDelete