首页 文章

在MvvmCross Xamarin.Forms应用程序中打开没有导航的视图

提问于
浏览
0

我试图在MvvmCross中创建一个简单的登录视图(使用Xamarin.Forms选项),在按下登录按钮后切换到另一个视图,但是当我执行类似 this.ShowViewModel<MainViewModel>();navigationService.Navigate<MainViewModel>(); 的操作时

它留下导航后退按钮,将视图切换回登录视图 .

如何显示不同的视图但阻止其添加后退按钮?

下面是我的LoginViewModel.cs文件:

using System;
using System.Threading.Tasks;
using System.Windows.Input;
using MvvmCross.Core.Navigation;
using MvvmCross.Core.ViewModels;

namespace MvvmForms.MySampleApp.Core.ViewModels
{
    public class LoginViewModel : MvxViewModel
    {
        readonly IMvxNavigationService navigationService;

        public LoginViewModel(IMvxNavigationService navigationService)
        {
            this.navigationService = navigationService;
        }

        ...

        public ICommand ShowMainPageCommand => new MvxCommand(ShowInfoPage);

        private void ShowMainPage()
        {
            navigationService.Navigate<MainViewModel>();
        }
    }
}

这是我的LoginPage.xaml文件:

<?xml version="1.0" encoding="utf-8"?>
<mvx:MvxContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:forms="using:Xamarin.Forms"
    xmlns:mvx="clr-namespace:MvvmCross.Forms.Core;assembly=MvvmCross.Forms"
    x:Class="MvvmForms.MySampleApp.Core.Pages.LoginPage" 
    Title="Login Page"
>
<mvx:MvxContentPage.Padding Thickness="5, 0, 5, 95">
    <OnPlatform x:TypeArguments="Thickness">
        <On Platform="Android" Value="5, 0, 5, 0" />
        <On Platform="iOS" Value="5, 20, 5, 0" />
    </OnPlatform>
</mvx:MvxContentPage.Padding>
<StackLayout Spacing="10" Orientation="Vertical">
    <Label FontSize="24" Text="Enter your nickname in the box below" />
    <Entry Placeholder="Who are you?" TextColor="Red" Text="{Binding YourNickname}" />
    <Label FontSize="24" Text="{Binding Hello}" />
    <Button x:Name="LoginButton" Text="Login" Command="{Binding ShowMainPageCommand}" />
</StackLayout>

2 回答

  • 1

    这种方法可能适合您 .

    _navigation的类型为NaviagtionPage

    page是您希望成为新根的页面 .

    var root  = _navigation.Navigation.NavigationStack.First();
    _navigation.Navigation.InsertPageBefore(page);
    _navigation.PopToRootAsync();
    

    这会将您的页面作为根页面插入并删除登录页面

  • 0

    转到其他页面后,从导航堆栈中删除登录页面

    推送你的页面

    await Navigation.PushAsync(new MainPage());
    

    现在,如果你有登录页面的引用,你可以做

    Navigation.RemovePage(loginPage);
    

    否则在堆栈上找到您的页面并将其删除

    var pages = Navigation.NavigationStack.ToList();
      foreach (var page in pages)
         {
             if (page.GetType() == typeof(LoginPage))
                  Navigation.RemovePage(page);
         }
    

相关问题