0

I am developing a Xamarin Forms project and use Prism Library as tool to implement the MvvM pattern.

I have two views:

  • PersonViewModel: Summary and has Edit button
  • PersonEditViewModel: Details, and Cancel Save and Delete buttons

The proposed concept: Handle the result of the PersonEditViewModel when navigating back to the PersonViewModel and make a decision based upon navigation parameters.

My questions about the concept:

  • Is the way of handling dialog results 'ok' given I try to follow MvvM principles?
  • Is this making good use of the Prism Library Navigation funtions?

The example code. I left out private fields, error handling etc. for readabilty.

public enum ItemDialogResult
{
    Saved = 1,
    Cancelled = 0,
    Deleted = -1
}

public class PersonViewModel : INavigationAware
{
    private readonly INavigationService _navigationService;

    public PersonViewModel()
    {
        EditCommand = new Command(async () => await _navigationService.NavigateAsync($"PersonEditPage?id={Person.Id}");
    }

    public async void OnNavigatedTo(INavigationParameters parameters)
    {
        var navigationMode = parameters.GetNavigationMode();

        if (navigationMode == NavigationMode.Back)
        {
            // GetValueOrDefault is a custom extension using GetValue but returning a default value if it does not exist in the parameters.
            // For example when the hardware-button is used to navigate back.
            var result = parameters.GetValueOrDefault("result", ItemDialogResult.Cancelled);

            switch (result)
            {
                case ItemDialogResult.Cancelled:
                    // Shown person is still valid
                    return;

                case ItemDialogResult.Saved:
                    // Continue and reload the person below
                    break;

                case ItemDialogResult.Deleted:
                    // person has been deleted. Navigate back from the p
                    await _navigationService.GoBackAsync();
                    return;
            }
        }

        Person = _personService.LoadPerson();
    }
}

public class PersonEditViewModel : INavigationAware
{
    private readonly INavigationService _navigationService;

    public PersonEditViewModel()
    {           
        CancelCommand = new Command(async () => await GoBack(ItemDialogResult.Cancelled));
        DeleteCommand = new Command(async () =>
        {
            _personService.Delete(_person);
            await GoBack(ItemDialogResult.Deleted);
        });
        SaveCommand = new Command(async () =>
        {
            _personService.SaveOrUpdate(_person);
            await GoBack(ItemDialogResult.Saved);
        });
    }

    private async Task GoBack(ItemDialogResult result)
    {
        var parameters = new NavigationParameters
        {
            {"result", result}
        };

        await _navigationService.GoBackAsync(parameters);
    }
}
  • Please explain the downvote? I would love to improve this question. – Aphelion Jan 16 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.