Write date: October 10, 2023
1. What is UWP?
UWP (Universal Windows Platform) is a platform from Microsoft that allows you to develop applications that run on a variety of devices (PCs, tablets, smartphones, etc.). UWP maximizes developer productivity by enabling the creation of apps that can run on multiple devices with a single codebase.
Key features of UWP include the ability to run apps on various platforms such as mobile, PC, and Xbox,
XAML-based UI composition, and the provision of various APIs for modern app development.
Thanks to these advantages, UWP is becoming increasingly popular among developers.
2. What is the Prism Framework?
The Prism Framework is a library designed to enhance the quality and maintainability of XAML-based applications such as WPF, Xamarin.Forms, and UWP.
It adopts the MVVM (Model-View-ViewModel) pattern to help developers easily organize and manage their apps.
Prism provides the following key features:
- Modularity: Applications can be divided into multiple modules, distributing work and increasing code reuse.
- Dependency Injection: It provides an easy-to-manage structure for necessary components, making it convenient for testing.
- Event Aggregation: It effectively manages and binds a variety of events and commands.
3. Installing the Prism Framework
To use the Prism Framework, you need to install the NuGet package.
After creating a new UWP project in Visual Studio, install the required Prism package through the NuGet Package Manager as follows:
Install-Package Prism.Windows
Once the installation is complete, the basic structure of the project is ready.
4. Creating a New UWP Project
Launch Visual Studio and select the ‘Create a new project’ option. In the ‘Platform’
category, choose ‘Windows’, and then select ‘UWP App’.
After setting the project name and save location, click the ‘Create’ button to generate the new project.
5. Setting Up the Prism-Based Structure
To introduce the Prism Framework into the newly created UWP project, you need to set up the basic structure.
In this step, modify the App.xaml.cs file to set it up as a Prism Application.
using Prism;
using Prism.Ioc;
public sealed partial class App : PrismApplication
{
public App() : base() { }
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// Register services here.
}
protected override void OnInitialized()
{
InitializeComponent();
NavigationService.NavigateAsync("MainPage");
}
}
6. Creating and Configuring Modules
Another advantage of Prism is support for modularity. You can manage each feature as a separate module.
Create a new module and write the Registration file.
public class MyModule : IModule
{
public void OnInitialized()
{
// Called when the module is initialized.
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation();
}
}
7. Implementing the MVVM Pattern
Implement the MVVM pattern using UWP and Prism. Create a ViewModel and bind it to the View to easily manage data and testing.
public class MainViewModel : BindableBase
{
private string _title;
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
public MainViewModel()
{
Title = "Hello, Prism!";
}
}
8. Implementing Pages and Data Binding
Bind the ViewModel in MainPage.xaml to create interaction with UI elements.
Below is a simple example of XAML code.
<Page x:Class="YourAppNamespace.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBlock Text="{Binding Title}"
FontSize="24"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</Page>
9. Testing and Debugging
After building the application, run debugging and testing before deploying to the UWP store.
Utilize Visual Studio’s debugging tools to quickly identify problems in the code.
10. Conclusion and Additional Resources
Through the above steps, I hope you have gained an understanding of the basic flow of UWP app development using the Prism Framework.
I encourage you to refer to websites and books for deeper learning.
Check out the official Prism documentation or
Getting Started with UWP.