繁体   English   中英

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

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

第一次将Caliburn Micro用于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:

视图模型是从“ Screen”和“ INotifyPropertyChangedEx”继承并实现的

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

This.NavigationCacheMode = NavigationCacheMode.Required;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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