返回viewmodel禁用canexcute命令(Caliburn Micro - Windows Phone 8.1)

时间:2017-03-11 20:46:16

标签: c# wpf xaml windows-phone-8.1 caliburn.micro

我第一次使用Caliburn Micro for Windows Phone 8.1。

我正在使用一个带有命令的按钮按惯例执行绑定,该命令在我的viewmodel中执行一个方法,其中有一个" CanExecute"。到目前为止一切顺利,但是当我登录(此按钮的方法)并返回(注销)到我的登录视图时,登录屏幕上所有按钮的命令都停止工作(我相信问题出在CanExecute中),然后无需退出应用程序并返回就无法再登录。

以下是用于登录和注销的方法,以及他们可以执行"。

登录(在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);
     }
}

退出(在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>

更新1:

视图模型是从&#34; Screen&#34;继承和实现的。和&#34; INotifyPropertyChangedEx&#34;

1 个答案:

答案 0 :(得分:0)

LoginView.xaml的代码隐藏有一条导致问题的行,只需删除该行,一切都运行良好。

This.NavigationCacheMode = NavigationCacheMode.Required;