7
votes

Commande de relais dans l'application Windows 8 Windows Store

Y a-t-il une version de RelayCommand, puisque CommandManager n'est pas disponible dans les applications Win8 Metro?


0 commentaires

3 Réponses :


4
votes

Il y a une version ici .

using System;
using System.Diagnostics;

#if METRO
using Windows.UI.Xaml.Input;
using System.Windows.Input;
#else
using System.Windows.Input;
#endif

namespace MyToolkit.MVVM
{
#if METRO
    public class RelayCommand : NotifyPropertyChanged, ICommand
#else
    public class RelayCommand : NotifyPropertyChanged<RelayCommand>, ICommand
#endif
    {
        private readonly Action execute;
        private readonly Func<bool> canExecute;

        public RelayCommand(Action execute)
            : this(execute, null) { }

        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            this.execute = execute;
            this.canExecute = canExecute;
        }

        bool ICommand.CanExecute(object parameter)
        {
            return CanExecute;
        }

        public void Execute(object parameter)
        {
            execute();
        }

        public bool CanExecute 
        {
            get { return canExecute == null || canExecute(); }
        }

        public void RaiseCanExecuteChanged()
        {
            RaisePropertyChanged("CanExecute");
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, new EventArgs());
        }

        public event EventHandler CanExecuteChanged;
    }

    public class RelayCommand<T> : ICommand
    {
        private readonly Action<T> execute;
        private readonly Predicate<T> canExecute;

        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            this.execute = execute;
            this.canExecute = canExecute;
        }

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return canExecute == null || canExecute((T)parameter);
        }

        public void Execute(object parameter)
        {
            execute((T)parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, new EventArgs());
        }

        public event EventHandler CanExecuteChanged;
    } 
}


2 commentaires

Il suffit de souligner que ce fichier n'a pas le notifypropertychanged Les références informatiques, et la plupart des gens sauront de la recréer, mais cela peut être bon d'inclure pour ceux qui ne le font pas.


@Owenjohnson notifyPropertychangned trouvé ici: myToolkit.svn .Codeplex.com / SVN / Shared / MVVM / ...



1
votes

Il n'y a pas de mise en œuvre si ICommand fournit dans Metro, bien qu'il existe plusieurs versions disponibles, telles que celle-ci sur codeProject .


0 commentaires

0
votes

prisme pour Windows Store Apps est désormais disponible, qui contient des déléguecommand (qui implémente l'iCommand), ainsi qu'une implémentation d'ONPROPERTYCHANGED.


0 commentaires