Return to viewmodel 禁用可执行命令 (Caliburn Micro - Windows Phone 8.1)
Posted
技术标签:
【中文标题】Return to viewmodel 禁用可执行命令 (Caliburn Micro - Windows Phone 8.1)【英文标题】:Return to viewmodel disables canexcute of commands (Caliburn Micro - Windows Phone 8.1) 【发布时间】:2017-08-02 02:24:02 【问题描述】:我第一次使用适用于 Windows Phone 8.1 的 Caliburn Micro。
我正在使用带有命令的按钮按约定进行绑定,该命令在我的视图模型中执行一个方法,其中它有一个“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”继承和实现
【问题讨论】:
【参考方案1】:LoginView.xaml 的代码隐藏中有一行导致问题,只需删除该行,一切正常。
This.NavigationCacheMode = NavigationCacheMode.Required;
【讨论】:
以上是关于Return to viewmodel 禁用可执行命令 (Caliburn Micro - Windows Phone 8.1)的主要内容,如果未能解决你的问题,请参考以下文章
AutoMapper Model To ViewModel Vice Versa 更新记录