As mentioned earlier in part 1, if we use PRISM library for MVVM pattern, all the data transition & state can be implemented in the ViewModels. We already know how to simply make connection between the View and the ViewModel and binding data & command between them. This time I want to write more about other features of PRISM library.

 

  1. Navigation

First, open App.xaml.cs (or App.cs) and modify the App class to inherited from PrismApplication.

Change from:

public partial class App : Application

To:

public partial class App : PrismApplication

After this modification, App class properties MainPage & method OnStart/OnSleep/OnResume will not available. Instead we have 2 new methods: OnInitialized() and RegisterTypes().

If using App.xaml, change the base class too.

Declare assembly:

xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"

Change from:

<Application>

…

</Application>

To:

<prism:PrismApplication>

…

</prism:PrismApplication>

For the start page, we have to register page & navigate to it when initialize App().

protected override void OnInitialized()
{
    NavigationService.NavigateAsync("NavigationPage/ToDoList");
}

protected override void RegisterTypes()
{
    Container.RegisterTypeForNavigation<NavigationPage>();
    Container.RegisterTypeForNavigation<ToDoList>();
}

From now on, each time we create a new page, we should register it for navigation in App.RegisterTypes().

 

Next, create ToDoDetails page and its ViewModel. If using Prism Template Pack extension for Visual Studio, it will create the ViewModel and register the new page for navigation automatically.

On the ViewModel, declare navigation service

protected INavigationService navigationService;
…
public ToDoListViewModel(INavigationService navigationService)
{
    this.navigationService = navigationService;
}

And when the navigation need to happen, e.g.: when a command is executed:

await navigationService.NavigateAsync("ToDoDetails");

This way, we can navigate between page without using code behind.

 

  1. Binding command in a ListView

In the PRISM (part 1), for the control that already has Command property, we can binding directly the Command property with command defined in the ViewModel. But for control that don’t have Command, we need some more work. ListView doesn’t have Command property, but we can use behavior for binding the command.

Adding the namespace in the View:

xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"

For ListView, we can bind the command for ItemSelected event:

<ListView.Behaviors>
    <b:EventToCommandBehavior EventName="ItemSelected"
         Command="{Binding ItemSelectedCommand}"
         EventArgsParameterPath="SelectedItem" />
</ListView.Behaviors>

Similarly, we can binding command for ItemTapped event.

 

  1. Navigation with parameters

When navigating from one page to another page, usually we need to passing some values. With PRISM, it is done with NavigateAsync() method.

First, inherited interface INavigatedAware in the ViewModel class of each page. The interface will require implementation of two methods, OnNavigatedFrom() and OnNavigatedTo(). We can passing parameters using these methods.

In the source page:

NavigationParameters param = new NavigationParameters();
param.Add("Id", selectedItem.Id);
await navigationService.NavigateAsync("ToDoDetails", param);

In the destination page:

int itemId = (int)parameters["Id"];

We can also pass parameter as string:

await navigationService.NavigateAsync("ToDoDetails?Id="+selectedItem.Id);

In the destination page:

int itemId = int.Parse((string)parameters["Id"]);

 

  1. Using DialogService

Using dialog service is similar with NavigationService.

protected IPageDialogService dialogService;
…
public ToDoListViewModel(IPageDialogService dialogService)
{
    this.dialogService = dialogService;
}

Then, calling DisplayAlert from any async method:

await dialogService.DisplayAlertAsync("Sample", "What color do you see?", "OK");

 

  1. Propagating Event

We can define our own event within our ViewModel or even propagate the event to all class that subscribe to that event.

First, define the event and data to carry:

using Prism.Events;

public class CountDownEvent : PubSubEvent<string>

 

Using PubSubEvent in a ViewModel is the same as NavigationService or DialogService.

protected IEventAggregator eventAggregator;
…
public ToDoListViewModel(IEventAggregator eventAggregator)
{
    this.eventAggregator = eventAggregator;
}

Subscribe to receive event:

eventAggregator.GetEvent<CountDownEvent>().Subscribe(ReachCountDown);

Publish the event:

eventAggregator.GetEvent<CountDownEvent>().Publish("Count down is reached!");

If we don’t want to receive event anymore:

eventAggregator.GetEvent<CountDownEvent>().Unsubscribe(ReachCountDown);

 

The sample code can be found here:

https://github.com/longnt-ttm/Xamarin-Samples/tree/master/MvvmPrismSample

Leave a Reply

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>