简体   繁体   中英

Return to viewmodel disables canexcute of commands (Caliburn Micro - Windows Phone 8.1)

First time I'm using Caliburn Micro for Windows Phone 8.1.

I am using a button with a command doing the binding by convention, which executes a method in my viewmodel in which it has a "CanExecute". So far so good, but when I log in (method of this button) and return (log out) to my Login view, the commands of all the buttons on my login screen stop working (I believe the problem is in CanExecute ), Then it is no longer possible to login without having to exit the application and return.

Below are the methods used to login and logout and their "can execute".

Login (in LoginViewModel.cs):

public async void LoginCommand()
{
     var retorno = await _loginService.LoginAsync(Usuario, Senha);
     if (retorno.User != null)
     {
         Ambiente.Instance.Configuracao.Configurar(retorno.User);
         Ambiente.Instance.Carregar();

         DefinirEstadoInicial();
         navigationService.NavigateToViewModel(typeof(HomeViewModel));
     }
     ...
}

public bool CanLoginCommand
{
     get { return !string.IsNullOrEmpty(Usuario) && !string.IsNullOrEmpty(Senha); }
}


private string _usuario;

public string Usuario
{
     get { return _usuario; }
         set
     {
         _usuario = value;
         NotifyOfPropertyChange();
         NotifyOfPropertyChange(() => CanLoginCommand);
     }
}

private string _senha;

public string Senha
{
     get { return _senha; }

     set
     {
          _senha = value;
          NotifyOfPropertyChange();
          NotifyOfPropertyChange(() => CanLoginCommand);
     }
}

Logout (in HomeViewModel.cs)

public async void SairCommand()
{
     await MessageBox("Deseja fazer logout do aplicativo?", "Logout", textoBotaoEsquerda: "Não", codeBotaoDireita: new System.Action(() =>
     {
          Ambiente.Instance.Recarregar();
          navigationService.NavigateToViewModel(typeof(LoginViewModel));
     }), textoBotaoDireita: "Sim");
}

LoginView.xaml

<StackPanel Margin="0,30,0,0" Name="StackLogin">
            <Image Source="ms-appx:///Assets/Images/logo_login@2x.png" MaxWidth="240" Stretch="UniformToFill" />
            <TextBlock Text="Login" Margin="0,50,0,0" Style="{StaticResource PageTitle}"/>
            <TextBlock Text="Use sua conta Muambator/Pacotes para continuar rastreando suas encomendas"
                       Margin="0,20,0,0"
                       TextWrapping="Wrap" Style="{StaticResource PageDescription}" HorizontalAlignment="Center" />
            <Border Background="White" Margin="0,30,0,0" CornerRadius="3" MaxWidth="320">
                <Grid VerticalAlignment="Center">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Image Source="ms-appx:///Assets/Images/icon_login@2x.png" Margin="10,0,0,0" Width="20" />
                    <TextBox Grid.Column="1" Text="{Binding Path=Usuario, Mode=TwoWay}" PlaceholderText="LOGIN" Style="{StaticResource LoginTextBox}"/>
                    <Border Grid.ColumnSpan="2" BorderBrush="LightGray" BorderThickness="0,0,0,1" />
                    <Image Grid.Row="1" Source="ms-appx:///Assets/Images/icon_password@2x.png" Margin="10,0,0,0" Width="20" />
                    <PasswordBox Grid.Row="1" Grid.Column="1" x:Name="Senha" PlaceholderText="SENHA" Style="{StaticResource LoginPasswordBox}" VerticalAlignment="Center" />
                    <Button Click="MostrarEsqueciMinhaSenhaCommand_Click"
                            x:Name="MostrarEsqueciMinhaSenhaCommand"
                            Grid.Row="1"
                            Grid.Column="1"
                            Width="50" 
                            Style="{StaticResource EsqueciMinhaSenhaButtonStyle}"
                            HorizontalAlignment="Right"/>                    
                </Grid>
            </Border>

            <Button x:Name="LoginCommand" Width="322" Height="40" Margin="0,20,0,0" HorizontalAlignment="Stretch" Background="#6EAF2D" BorderThickness="0" Style="{StaticResource ButtonStyle}" >
                <Grid>
                    <TextBlock Style="{StaticResource ButtonText}">ENTRAR</TextBlock>
                    <Image Margin="20,0,-75,0" Height="15" Source="ms-appx:///Assets/Images/icon_arrow_white_normal@2x.png" />
                </Grid>
            </Button>
        </StackPanel>

Update 1:

The viewmodels are inheriting and implementing from "Screen" and "INotifyPropertyChangedEx"

LoginView.xaml的代码背后有一行代码,它引起了问题,只需删除该行,一切就可以正常工作。

This.NavigationCacheMode = NavigationCacheMode.Required;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM