在App.cs中使用Prism NavigationService

时间:2017-08-04 07:37:00

标签: c# mvvm xamarin.forms prism

我有一个使用Prism的xamarin表单应用程序,我在App.xaml中使用一个带有图标的控件模板,该图标可以转到上一页。 在tapGesture事件方法imgBack_Tapped中,我正在调用NavigationService.GoBackAsync(); 但它没有进入上一页。

Brian xamarin forums link这里说的是为了避免在视图模型中使用NavigationService,但是我不得不使用控件模板。

任何解决方案或替代方案都会很棒,提前谢谢。

App.xaml

<ControlTemplate x:Key="TitleBarTemplate">
    <Grid Grid.RowSpacing="0">
      <Grid.RowDefinitions>
        <RowDefinition Height="0.1*" />
        <RowDefinition Height="0.9*" />
      </Grid.RowDefinitions>

      <StackLayout Grid.Row="0" BackgroundColor="#2ea5d5" Orientation="Horizontal" Padding="10,0">
        <Image Source="Back.png" WidthRequest="30" HorizontalOptions="Start" IsVisible="{TemplateBinding Parent.IsBackBtnVisible}">
          <Image.GestureRecognizers>
            <TapGestureRecognizer Tapped="imgBack_Tapped"/>
          </Image.GestureRecognizers>
        </Image>
        <Label Text="{TemplateBinding Parent.TitleBarText}" TextColor="White" FontSize="18" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" Style="{StaticResource TitleBarFont}"></Label>
        <Image Source="logo_round.png" WidthRequest="50" HorizontalOptions="End" IsVisible="{TemplateBinding Parent.IsRoundLogoVisible}"></Image>
      </StackLayout>

      <ContentPresenter Grid.Row="1">

      </ContentPresenter>
    </Grid>
  </ControlTemplate>

App.xaml.cs

 protected override void OnInitialized()
    {
        InitializeComponent();
        NavigationService.NavigateAsync("NavigationPage/Login");
        NavigationPage.SetHasNavigationBar(this, false);
    }

    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<NavigationPage>();
        Container.RegisterTypeForNavigation<Login>();
        Container.RegisterTypeForNavigation<Dashboard>();
        Container.RegisterTypeForNavigation<MyActivity>();
        Container.RegisterTypeForNavigation<About>();
    }

    private async void imgBack_Tapped(object sender, EventArgs e)
    {
        try
        {
            await NavigationService.GoBackAsync();
        }
        catch (Exception ex)
        {

        }
    }

更新: 我能够解决它,我在视图模型中创建了一个命令,并使用图像tapped命令的绑定上下文来引用视图模型。

App.xaml

<Image Source="Back.png" WidthRequest="30" HorizontalOptions="Start" IsVisible="{TemplateBinding Parent.IsBackBtnVisible}">
          <Image.GestureRecognizers>
            <TapGestureRecognizer Command="{TemplateBinding Parent.BindingContext.BackImgTappedCommand}"/>
          </Image.GestureRecognizers>
        </Image>

在我的视图模型中 SamplePageViewModel

private async void BackImgTapped()
    {
        await _navigationService.GoBackAsync(); 
    }

1 个答案:

答案 0 :(得分:1)

尝试在viewModel构造函数中接收navigationService:

private ICommand navigatePackagesCommand;

        public ICommand NavigatePackagesCommand
        {
            get { return navigatePackagesCommand; }
        }

public YourPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
 //...
}

并使用它来导航:

_navigationService.NavigateAsync("DestinationPage");

或在你的情况下:

_navigationService.GoBackAsync();