user2160518
user2160518

Reputation: 3

INavigationService Implementation

I am teaching myself Prism/Xamarin Forms and have struck an issue with the Navigation System in Prism.

I have two Views (MainPage and FirstPage) Registered in app.cs

protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<MainPage>("MainPage");
        Container.RegisterTypeForNavigation<FirstPage>("FirstPage");
    }

When I navigate to MainPage it works fine:

NavigationService.NavigateAsync("MainPage?title=MainPage");

However, when I navigate to FirstPage the app errors out with a "No Resource" Error.

Both Views and associated ViewModels are similarly coded:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="PrismDemo.Views.MainPage"
             Title="MainPage">
  <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
    <Label Text="{Binding Title}" />
    <Button Text="Navigate" Command="{Binding NavigateCommand}" />
  </StackLayout>
</ContentPage>

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="PrismDemo.Views.FirstPage">
             Title="FirstPage">
  <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
    <Label Text="{Binding Title}" />
    <Button Text="Back" Command="{Binding NavigateCommand}" />
  </StackLayout>
</ContentPage>

using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PrismDemo.ViewModels
{
    public class MainPageViewModel : BindableBase, INavigationAware
    {
        INavigationService _navigationService;
        private string _title;

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public DelegateCommand NavigateCommand { get; set; }

        public MainPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
            NavigateCommand = new DelegateCommand(Navigate);
        }

        private void Navigate()
        {
            _navigationService.NavigateAsync("FirstPage");
        }

        public void OnNavigatedFrom(NavigationParameters parameters)
        {

        }

        public void OnNavigatedTo(NavigationParameters parameters)
        {
            if (parameters.ContainsKey("title"))
                Title = (string)parameters["title"] + " and Prism";
        }
    }
}

using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PrismDemo.ViewModels
{
    public class FirstPageViewModel : BindableBase, INavigationAware
    {
        INavigationService _navigationService;
        private string _title;

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public DelegateCommand NavigateCommand { get; set; }

        public FirstPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
            NavigateCommand = new DelegateCommand(Navigate);
        }

        private void Navigate()
        {
            _navigationService.GoBackAsync();
        }

        public void OnNavigatedFrom(NavigationParameters parameters)
        {

        }

        public void OnNavigatedTo(NavigationParameters parameters)
        {
            if (parameters.ContainsKey("title"))
                Title = (string)parameters["title"] + " and Prism";
        }
    }
}

Can anybody see where I am going wrong?

Upvotes: 0

Views: 512

Answers (1)

madgrizzle
madgrizzle

Reputation: 21

You have a misplaced '>' in your firstpage's xaml. Look at the end of the x:Class line and you will see the misplaced '>'. Get rid of it and it might work.

Upvotes: 0

Related Questions