Команда Delegate не стреляет в Xamarin с использованием Prism - PullRequest
0 голосов
/ 15 ноября 2018

Я создаю страницу входа и регистрации и делаю навигацию между двумя страницами.LoginPage может перейти к RegisterPage, но в RegisterPage обе команды делегата даже не запускаются.

Ниже приведен код для RegisterPageViewModel:

using Prism.Commands;
using Prism.Navigation;
using Dialog.Interfaces;
using Prism.Services;

namespace Dialog.ViewModels
{
    public class RegisterPageViewModel : ViewModelBase
    {
        private INavigationService _navigationservice;
        private IRegistrationService _registartionService;
        private IPageDialogService _pageDialogService;
        private string _email;
        private string _password;
        private string _confirmpassword;

        public DelegateCommand NavigateToLoginCommand { get; private set; }
        public DelegateCommand RegisterCommand { get; private set; }


        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                SetProperty(ref _email, value);
            }
        }
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                SetProperty(ref _password, value);
            }
        }
        public string ConfirmPassword
        {
            get
            {
                return _confirmpassword;
            }
            set
            {
                SetProperty(ref _confirmpassword, value);
            }
        }


        public RegisterPageViewModel(INavigationService navigationService, IRegistrationService registrationService, IPageDialogService pageDialogService) : base(navigationService)
        {
            _navigationservice = navigationService;
            _registartionService = registrationService;
            _pageDialogService = pageDialogService;

            NavigateToLoginCommand = new DelegateCommand(NavigateToLoginPage);
            RegisterCommand = new DelegateCommand(OnRegisterCommandExecuted, RegisterCommandCanExecute)
                .ObservesProperty(() => Email)
                .ObservesProperty(() => Password)
                .ObservesProperty(() => ConfirmPassword);
        }

        private bool RegisterCommandCanExecute()
        {
            return !string.IsNullOrWhiteSpace(Name) && !string.IsNullOrWhiteSpace(Email) && !string.IsNullOrWhiteSpace(Password) && !string.IsNullOrWhiteSpace(ConfirmPassword) && IsNotBusy;
        }

        private void OnRegisterCommandExecuted()
        {
            IsBusy = true;
            _registartionService.CreateUser("xxx", "xxxx", "xxx");
            IsBusy = false;
        }

        private void NavigateToLoginPage()
        {
            _navigationservice.GoBackAsync();
        }
    }
}

RegisterCommandCanExecute () работает нормально и включаетКнопка регистрации.Но команды не запускаются.

RegisterPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Dialog.Views.RegisterPage" NavigationPage.HasNavigationBar="False">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.Resources>
            <ResourceDictionary>
                <Style TargetType="Entry">
                    <Setter Property="Margin" Value="40,10" />
                </Style>
            </ResourceDictionary>
        </Grid.Resources>
        <StackLayout Grid.Row="1" VerticalOptions="FillAndExpand">
            <Entry Text="{Binding Email}" Placeholder="Email Address" VerticalOptions="EndAndExpand" />
            <Entry Text="{Binding Password}" IsPassword="true" Placeholder="Password" VerticalOptions="StartAndExpand" />
            <Entry Text="{Binding ConfirmPassword}" IsPassword="true" Placeholder="Confirm Password" VerticalOptions="StartAndExpand" />
            <Button Text="Register" Command="{Binding RegisterCommand}" />
            <Button Text="Login" Command="{Binding NavigateToLoginCommand}" />
        </StackLayout>
    </Grid>
</ContentPage>

Код такой же для LoginPageViewModel и работает нормально.Я зарегистрировал страницу для навигации в App.xaml.cs.Ниже приведен код для App.xaml.cs

using Prism;
using Prism.Ioc;
using Dialog.ViewModels;
using Dialog.Views;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Dialog.Interfaces;
using Dialog.Services;
using Prism.Unity;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Dialog
{
    public partial class App: PrismApplication
    {
        /* 
         * The Xamarin Forms XAML Previewer in Visual Studio uses System.Activator.CreateInstance.
         * This imposes a limitation in which the App class must have a default constructor. 
         * App(IPlatformInitializer initializer = null) cannot be handled by the Activator.
         */
        public App() : this(null) { }

        public static new App Current
        {
            get { return Application.Current as App; }
        }

        public App(IPlatformInitializer initializer) : base(initializer) { }

        protected override async void OnInitialized()
        {
            InitializeComponent();
            if (App.Current.Properties.ContainsKey("isLoggedIn"))
                await NavigationService.NavigateAsync("NavigationPage/RegisterPage");
            else
                await NavigationService.NavigateAsync("NavigationPage/LoginPage");
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<NavigationPage>();
            containerRegistry.RegisterForNavigation<LoginPage, LoginPageViewModel>();
            containerRegistry.RegisterForNavigation<RegisterPage, RegisterPageViewModel>();
            containerRegistry.Register<IAuthenticationService, AuthenticationService>();
            containerRegistry.Register<IRegistrationService, RegistrationService>();
        }
    }
}
...