INAVigationService实施

时间:2016-08-20 23:55:25

标签: c# xamarin navigation xamarin.forms prism

我正在教自己Prism / Xamarin表格并且已经解决了Prism导航系统的问题。

我在app.cs中注册了两个视图(MainPage和FirstPage)

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

当我导航到MainPage时,它可以正常工作:

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

但是,当我导航到FirstPage时,应用程序错误输出“无资源”错误。

视图和关联的ViewModel都具有类似的编码:

<?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";
        }
    }
}

有人可以看到我哪里出错吗?

1 个答案:

答案 0 :(得分:0)

你错放了'&gt;'在你的第一页的xaml中。查看x:Class行的末尾,您将看到错误的'&gt;'。摆脱它,它可能会有效。