Last week was a huge week for the Uno platform with their inaugural Uno conference, #UnoConf. As the technology continues to mature, I’ve no doubt that Uno will become a viable solution for building applications to target all sorts of markets. This includes support being progressively added by the various Mvvm frameworks.
Following my previous posts (MVVM Navigation with Xamarin.Forms Shell and MVVM Navigation with Xamarin.Forms Shell – Part II) where I discussed a simple approach to Mvvm with Xamarin.Forms, I figured I’d so something similar with Uno.
Mvvm with Uno
Let’s get on with it and start with a new Uno project – Download and install the Uno Platform Solution Templates extension from the Visual Studio marketplace, if you haven’t already. In Visual Studio, create a new project based on the Cross-Platform App (Uno Platform) project template. I’m going to call the app DoUnoMvvm.
Creating a Class Library
We’re going to separate out our viewmodels and services into a separate library, so add a new project, DoUnoMvvm.Core, based on the Class Library (.NET Standard) project template. Delete the class1.cs and then add a reference to the class library to each of the head projects (i.e. Droid, iOS, UWAP and Wams).
Adjusting NuGet Package References
Right-click on the solution node in the Solution Explorer window and select Manage NuGet Packages for Solution. Go to the Updates tab, check the Include prerelease option and then check the box alongside the packages Uno.Wasm.Bootstrap, Uno.UI, Microsoft.NETCore.UniversalWindowsPlatform and Uno.Core (don’t check either the Logging packages). Click Update to get the latest version of the packages that are checked.
From the Browse tab on the NuGet-Solution window used in the previous step, enter BuildIt.General.Uno into the search box. Select BuildIt.General.Uno and install the packages into all five of the projects.
Mvvm Basics with ViewModelLocator
Now we should be ready to start writing some code. We’re going to keep it simple with the following steps:
- Create ViewModelLocator class – used for serving up viewmodels and creating services as required
- Create an instance of ViewModelLocator in App Resources, making it accessible as a static resource in XAML
- Create MainViewModel class – the viewmodel for the existing MainPage
- Update ViewModelLocator with a property Main that returns an instance of the MainViewModel class
- Set the DataContext of the MainPage to use the Main property on the ViewModelLocator
- Run the application and show data is being served by the MainViewModel.
Here we go…. firstly a new ViewModelLocator class, which is added to the DoUnoMvvm.Core project
public class ViewModelLocator
{
}
Update App.xaml to create an instance of the ViewModelLocator class
<Application
x:Class="DoUnoMvvm.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DoUnoMvvm"
xmlns:core="using:DoUnoMvvm.Core"
RequestedTheme="Light">
<Application.Resources>
<core:ViewModelLocator x:Key="ViewModelLocator" />
</Application.Resources>
</Application>
Now to create the MainViewModel, also in the DoUnoMvvm.Core project. We’ll create a property, WelcomeText, that will return some data to be displayed on MainPage.
public class MainViewModel
{
public string WelcomeText => "How well do Uno Mvvm?";
}
We need to update the ViewModelLocator class to include the Main property
public class ViewModelLocator
{
public MainViewModel Main => new MainViewModel();
}
And use this property when setting the DataContext for MainPage. I’ve also updated the TextBlock to be data bound to the WelcomeText property.
<Page
x:Class="DoUnoMvvm.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DoUnoMvvm"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding WelcomeText}" Margin="20" FontSize="30" />
</Grid>
</Page>
Run the application and there we have it, our first data bound page
Quick Navigation using Event Mapping
That’s pretty much the basics of Mvvm. However, following my previous posts discussing navigation, I just want to demonstrate how to abstract navigation away from both the page and the viewmodel – this allows for more independent testing of viewmodels as there’s no interdependency between viewmodels. Here’s the basic process:
- Add a new page, SecondPage, that we’re going to navigate to
- Add a corresponding viewmodel, SecondViewModel, and property, Second, on the ViewModelLocator
- Update SecondPage to set the DataContext to be bound to the Second property on the ViewModelLocator
- Add a Button to MainPage that invokes a method, Next, on the MainViewModel
- Add an event, Complete, to MainViewModel, and raise it from the Next method.
- Add a mapping to the App.xaml.cs that navigates to SecondPage when the Complete method is raised.
And here’s the code. I’m not going to show you the initial SecondPage as it’s just generated from the template and you’ll see it later anyhow. Instead, we’ll jump to the SecondViewModel (if you’re following along you still need to add the SecondPage to the DoUnoMvvm.Shared project in the Pages folder).
public class SecondViewModel
{
public string ProgressText => "Now you know how to navigate....";
}
Add the Second property to the ViewModelLocator
public class ViewModelLocator
{
public MainViewModel Main => new MainViewModel();
public SecondViewModel Second => new SecondViewModel();
}
Now back to the SecondPage and I have set the DataContext and bound the TextBlock.
<Page
x:Class="DoUnoMvvm.Shared.Pages.SecondPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DoUnoMvvm.Shared.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
DataContext="{Binding Second, Source={StaticResource ViewModelLocator}}"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<TextBlock Text="{Binding ProgressText}" />
</Grid>
</Page>
Now a Button to invoke the transition from MainPage to SecondPage
<Page
x:Class="DoUnoMvvm.MainPage" ...
DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}" >
<StackPanel>
<TextBlock Text="{Binding WelcomeText}" Margin="20" FontSize="30" />
<Button Content="Go to Second Page" Click="GoNextClick" />
</StackPanel>
</Page>
Here we’re simply using a code behind but you could easily use a command. Unfortunately x:Bind doesn’t appear to be working with Uno yet, so you can’t simply bind the Click method to a method on the viewmodel.
public void GoNextClick(object sender, RoutedEventArgs e)
{
(DataContext as MainViewModel)?.Next();
}
The Next method simply raises the Complete event
public class MainViewModel
{
public event EventHandler Complete;
public string WelcomeText => "How well do Uno Mvvm?";
public void Next()
{
Complete?.Invoke(this, EventArgs.Empty);
}
}
The final step is to add the mapping to App.xaml.cs to define what happens when the Complete event is triggered on the MainViewModel. Add the following property and method to App.xaml.cs, and update the App class to implement the IApplicationWithMapping interface (which comes from the BuildIt.General.Uno library that you should have added earlier)
public IDictionary<Type, IEventMap[]> Maps { get; } = new Dictionary<Type, IEventMap[]>();
private void MapPageTransitions()
{
Maps.For<MainViewModel>()
.Do(new EventHandler((s, args) => (Windows.UI.Xaml.Window.Current.Content as Frame)?.Navigate(typeof(SecondPage))))
.When((vm, a) => vm.Complete += a, (vm, a) => vm.Complete -= a);
}
Invoke the MapPageTransitions method immediately after the Window.Current.Content property has been set equal to a new Frame. In order for the events to get correctly wired up you also need to update both MainPage and SecondPage to inherit from the MappingBasePage class.
Now when you run the application, MainPage will appear with a Button that you can click to navigate to the SecondPage.
Uno How to Mvvm!
You might be thinking…. you’ve just shown me how to do a bunch of UWP code… and that is EXACTLY the point. If you switch to the Droid or iOS or Wasm target, you can run the same application on each of those platforms with NO further code changes required. The Uno platform is about leveraging the skills you already have as a UWP (or as a Xamarin.Forms) developer, allowing you to build rich, high-quality applications for iOS, Android and Web.
Link to the source code
1 thought on “Do Uno Mvvm?”