Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Xamarin.Forms Prism Pages, MVVM View Model Base classes, services, extensions etc.

License

Notifications You must be signed in to change notification settings

HLinteractive/HLI.Forms.Prism

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HL Interactive

HL Interactive (HLi)

HLI.Forms.Prism

Xamarin.Forms Prism Pages, MVVM View Model Base classes, services, extensions etc.

NuGet Downloads Build Status VSTS

Usage

Extensions

App

You can set the Prism naming policy to determine what directory is used for each MVVM part. Each parameter is optional with the below default values.
Example App.xaml.cs (inherits PrismApplication):

    protected override void ConfigureViewModelLocator()
    {
        base.ConfigureViewModelLocator();
        this.SetViewModelNamingPolicy("MyPageFolder", "MyViewModelFolder", "PageSuffix", "ViewModelSuffix");
    }

ICommand

To easily update an ICommands CanExecute there's an extension:

	ICommand SaveCommand { get; set; }
	SaveCommand.RaiseCanExecute();

Navigation

To navigate Prism deep links you use a path such as "MyTabbedPage/MyNavigationPage/UserPage". Since these are page classes you can use the PageNamesToUri helper to get typed Page names instead of strings:

	await this.NavigationService.NavigateAsync(NavigationExtensions.PageNamesToUri(nameof(MyNavigationPage), nameof(MyTabbedPage), nameof(UserPage);

When recieving Prism NavigationParameters you want to type check these. To help with this NavigationParameters has extensions to get as a specific type:

	Guid query = parameters.GetParameterAsGuid("Id");

Similarily there's GetParameterAsInt and GetParameterAsType where you supply an type argument:

	var feedback = parameters.GetParameterAsType<HliFeedbackMessage>("Feedback");

Prism Page base classes

Prism base classes that call SetAutowireViewModel automatically and load resources from your App class.

Import the namespace and use as base class for your pages:

	<pages:HliContentPage
	xmlns:pages="clr-namespace:HLI.Forms.Prism.Pages;assembly=HLI.Forms.Prism"
	HasNavigationBar="True">

ViewModel list/detail base classes

Working with MVVM there are lot of demands on your View Models; navigation, commands, busy indication etc. This library contains Prism based base classes that help set up a new project quickly; allowing list/detail View Model pattern using factories.

Say we want our app to start at the "login" Page, where the Model is "User".

  1. Set up Prism ViewModelLocator using the extension
  2. Create "LoginPage" in CS or XAML (using the base class)
  3. Create "LoginViewModel". Inherit the HliViewModel base class:
	public class LoginViewModel : HliViewModel<User>
	{
		// TODO: Constructor
	}
  1. Register the page for navigation in App.xaml.cs according to Prism documentation.
    Your ViewModel will now be registered with the Page and have a great deal of code for free.
    See this. in LoginViewModel.cs to auto complete all the methods and properties.
    Or check out the HliViewModel source code to learn more.

For list / detail scenarios HliListViewModel is used as the "master" View Model base class and each "item" child will use HliViewmodel as above. In this case you need to implement IViewModelFactory and register that class with the DI so the list View Model can find it.

Delivery & Deployment

Download the nuget package through Package Manager Console:

install-package HLI.Forms.Prism

Dependencies

  • Projects
  • Packages
    • HLI.Core
    • HLI.Forms.Core
    • HLI.Globalization
    • Prism.Forms
    • Xamarin.Forms
  • Tools
    • Windows 8.1 SDK
    • Windows 10 SDK

NuGet Package Generation

The project is configured to automatically generate a *.nupkg upon build with dotnet cli.

Solution File Structure

  • HLI.Forms.Prism - solution root folder
    • HLI.Forms.Prism - main project
      • Events - Prism PubSubEvent classes
      • Extensions - see above
      • Interfaces
      • Models - ViewModel base classes (above)
      • Pages - Xamarin.Forms Page base classes (above)

Changes and backward compatibility

  • Multi-target PCL / UAP 10 / NetStandard 1.4
  • VS2017 CsProj based package generation to netstandard 1.4