HL Interactive (HLi)
Xamarin.Forms Prism Pages, MVVM View Model Base classes, services, extensions etc.
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");
}
To easily update an ICommand
s CanExecute
there's an extension:
ICommand SaveCommand { get; set; }
SaveCommand.RaiseCanExecute();
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 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">
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".
- Set up Prism ViewModelLocator using the extension
- Create "LoginPage" in CS or XAML (using the base class)
- Create "LoginViewModel". Inherit the
HliViewModel
base class:
public class LoginViewModel : HliViewModel<User>
{
// TODO: Constructor
}
- 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.
Seethis.
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.
Download the nuget package through Package Manager Console:
install-package HLI.Forms.Prism
- Projects
- Packages
- HLI.Core
- HLI.Forms.Core
- HLI.Globalization
- Prism.Forms
- Xamarin.Forms
- Tools
- Windows 8.1 SDK
- Windows 10 SDK
The project is configured to automatically generate a *.nupkg upon build with dotnet cli
.
- 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)
- Events - Prism
- HLI.Forms.Prism - main project
- Multi-target PCL / UAP 10 / NetStandard 1.4
- VS2017 CsProj based package generation to netstandard 1.4