![](/img/trans.png)
[英]Caliburn.Micro 2.0 and navigating back in Windows Phone 8.1 XAML
[英]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.