2016-04-08 7 views
0

J'ai écrit un programme d'installation à l'aide de l'interface utilisateur du programme d'amorçage WIX. Lorsque j'exécute le programme d'installation, seul l'état du bouton Annuler est activé, bien que les autres états aient été correctement configurés. Voici ma classe de modèle.WIX UI affiche uniquement le bouton d'annulation bien que les autres états aient été correctement configurés

public class BootstrapperApplicationModel 
    { 
     private IntPtr hwnd; 
     public BootstrapperApplicationModel(
     BootstrapperApplication bootstrapperApplication) 
     { 
      this.BootstrapperApplication = bootstrapperApplication; 
      this.hwnd = IntPtr.Zero; 
     } 
     public BootstrapperApplication BootstrapperApplication 
     { 
      get; 
      private set; 
     } 

     public int FinalResult { get; set; } 

     public void SetWindowHandle(Window view) 
     { 
      this.hwnd = new WindowInteropHelper(view).Handle; 
     } 

     public void PlanAction(LaunchAction action) 
     { 
      this.BootstrapperApplication.Engine.Plan(action); 
     } 
     public void ApplyAction() 
     { 
      this.BootstrapperApplication.Engine.Apply(this.hwnd); 
     } 
     public void LogMessage(string message) 
     { 
      this.BootstrapperApplication.Engine.Log(LogLevel.Standard,message); 
     } 
    } 

Ci-dessous est mon modèle de vue

public class InstallViewModel : BindableBase 
    { 
     public enum InstallState 
     { 
      Initializing, 
      Present, 
      NotPresent, 
      Applying, 
      Cancelled 
     } 
     private InstallState state; 
     private string message; 
     private BootstrapperApplicationModel model; 
     public ICommand InstallCommand { get; private set; } 
     public ICommand UninstallCommand { get; private set; } 
     public ICommand CancelCommand { get; private set; } 
     public string Message 
     { 
      get 
      { 
       return this.message; 
      } 
      set 
      { 
       if (this.message != value) 
       { 
        this.message = value; 
        this.OnPropertyChanged(() => this.Message); 
       } 
      } 
     } 
     public InstallState State 
     { 
      get 
      { 
       return this.state; 
      } 
      set 
      { 
       if (this.state != value) 
       { 
        this.state = value; 
        this.Message = this.state.ToString(); 
        this.OnPropertyChanged(() => this.State); 
        this.Refresh(); 
       } 
      } 
     } 
     public InstallViewModel(
     BootstrapperApplicationModel model) 
     { 
      this.model = model; 
      this.State = InstallState.Initializing; 
      this.WireUpEventHandlers(); 
      this.InstallCommand = new DelegateCommand(() => 
      this.model.PlanAction(LaunchAction.Install), 
      () => this.State == InstallState.NotPresent); 
      this.UninstallCommand = new DelegateCommand(() => 
      this.model.PlanAction(LaunchAction.Uninstall), 
      () => this.State == InstallState.Present); 
      this.CancelCommand = new DelegateCommand(() => 
     { 
      this.model.LogMessage("Cancelling..."); 
      if (this.State == InstallState.Applying) 
      { 
       this.State = InstallState.Cancelled; 
      } 
      else 
      { 
       CustomBootstrapperApplication.Dispatcher 
       .InvokeShutdown(); 
      } 
     },() => this.State != InstallState.Cancelled); 
     } 
     protected void DetectPackageComplete(
     object sender, 
     DetectPackageCompleteEventArgs e) 
     { 
      if (e.PackageId.Equals(
     "MyInstaller.msi", StringComparison.Ordinal)) 
      { 
       this.State = e.State == PackageState.Present ? 
        InstallState.Present : InstallState.NotPresent; 
      } 
     } 
     protected void PlanComplete(
     object sender, PlanCompleteEventArgs e) 
     { 
      if (this.State == InstallState.Cancelled) 
      { 
       CustomBootstrapperApplication.Dispatcher 
       .InvokeShutdown(); 
       return; 
      } 
      this.model.ApplyAction(); 
     } 
     protected void ApplyBegin(
     object sender, ApplyBeginEventArgs e) 
     { 
      this.State = InstallState.Applying; 
     } 
     protected void ExecutePackageBegin(
     object sender, ExecutePackageBeginEventArgs e) 
     { 
      if (this.State == InstallState.Cancelled) 
      { 
       e.Result = Result.Cancel; 
      } 
     } 
     protected void ExecutePackageComplete(
     object sender, ExecutePackageCompleteEventArgs e) 
     { 
      if (this.State == InstallState.Cancelled) 
      { 
       e.Result = Result.Cancel; 
      } 
     } 
     protected void ApplyComplete(
     object sender, ApplyCompleteEventArgs e) 
     { 
      this.model.FinalResult = e.Status; 
      CustomBootstrapperApplication.Dispatcher 
      .InvokeShutdown(); 
     } 
     private void Refresh() 
     { 
      CustomBootstrapperApplication.Dispatcher.Invoke(
      (Action)(() => 
      { 
       ((DelegateCommand)this.InstallCommand) 
       .RaiseCanExecuteChanged(); 
       ((DelegateCommand)this.UninstallCommand) 
       .RaiseCanExecuteChanged(); 
       ((DelegateCommand)this.CancelCommand) 
       .RaiseCanExecuteChanged(); 
      })); 
     } 
     private void WireUpEventHandlers() 
     { 
      this.model.BootstrapperApplication.DetectPackageComplete 
      += this.DetectPackageComplete; 
      this.model.BootstrapperApplication.PlanComplete += this. 
     PlanComplete; 
      this.model.BootstrapperApplication.ApplyComplete += this. 
      ApplyComplete; 
      this.model.BootstrapperApplication.ApplyBegin += this. 
      ApplyBegin; 
      this.model.BootstrapperApplication.ExecutePackageBegin += 
      this.ExecutePackageBegin; 
      this.model.BootstrapperApplication.ExecutePackageComplete 
      += this.ExecutePackageComplete; 
     } 
    } 

Et ci-dessous est mon avis XAML et son code arrière.

<Window x:Class="CustomBA.Views.InstallView" 
xmlns= 
"http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:mc= 
"http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d= 
"http://schemas.microsoft.com/expression/blend/2008" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="300"> 
    <Window.Resources> 
     <Style TargetType="{x:Type Button}"> 
      <Setter Property="Margin" Value="10" /> 
      <Setter Property="Height" Value="30" /> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <StackPanel> 
      <Label Content="{Binding Message}" /> 
      <Button Command="{Binding InstallCommand}"> 
       Install</Button> 
      <Button Command="{Binding UninstallCommand}"> 
       Uninstall</Button> 
      <Button Command="{Binding CancelCommand}"> 
       Cancel</Button> 
     </StackPanel> 
    </Grid> 
</Window> 

public partial class InstallView : Window 
    { 
     public InstallView(InstallViewModel viewModel) 
     { 
      this.InitializeComponent(); 
      this.DataContext = viewModel; 
      this.Closed += (sender, e) => 
      viewModel.CancelCommand.Execute(this); 
     } 
    } 

Qu'est-ce que je fais mal ici? Veuillez nous conseiller

Voici à quoi cela ressemble, quand je l'exécute.

enter image description here

Répondre

1

Vous semblez être en cours d'exécution dans un problème où WPF ne réévalue l'état de votre commande. This answer couvre bien vos options - essayez CommandManager.InvalidateRequerySuggested(); ou l'événement CanExecuteChanged sur vos commandes.

+0

J'ai essayé d'appeler CommandManager.InvalidateRequerySuggested(); dans la méthode refresh() de la classe InstallViewModel. Mais toujours pas de chance. Est-ce là où je devais l'appeler? J'ai essayé de le mettre en setter de Message et State. Je suis nouveau à WPF, donc désolé pour toutes les questions stupides :( – mayooran

+0

Lorsque je débogue, l'état est toujours à "Initialisation" .Il ne change pas à tout autre état :( – mayooran

+0

@mayooran Êtes-vous sûr que vous avez le bon paquet id dans DetectPackageComplete? Aussi, y a-t-il une raison pour laquelle vous ne pouvez pas utiliser la valeur Installed dans DetectBegin pour indiquer si l'ensemble est installé? –