Developing Windows Store Apps - InvalidateRequerySposed

in regular WPF projects that I have used

CommandManager.InvalidateRequerySuggested();

      

to force the value converter.

Now, in the Windows Store App, this handy command is no longer available. Is there an equivalent command or something else that does the trick?

Many thanks for your help!

+3


source to share


2 answers


CommandManager

does not exist in WinRT. You need to manually update your listeners. Here's an example implementation DelegateCommand<T>

that illustrates this:

using System;
using System.Windows.Input;

public class DelegateCommand<T> : ICommand
{
    private readonly Action<T> m_executeAction;
    private readonly Predicate<T> m_canExecutePredicate;

    public DelegateCommand(Action<T> executeAction)
        : this(executeAction, null)
    {
    }

    public DelegateCommand(Action<T> executeAction, Predicate<T> canExecutePredicate)
    {
        if (executeAction == null)
        {
            throw new ArgumentNullException("executeAction");
        }

        m_executeAction = executeAction;
        m_canExecutePredicate = canExecutePredicate;
    }

    public event EventHandler Executed;

    public event EventHandler CanExecuteChanged;

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

    void ICommand.Execute(object parameter)
    {
        Execute((T)parameter);
    }

    public bool CanExecute(T parameter)
    {
        var result = true;
        var canExecutePredicate = m_canExecutePredicate;
        if (canExecutePredicate != null)
        {
            result = canExecutePredicate(parameter);
        }
        return result;
    }

    public void Execute(T parameter)
    {
        m_executeAction(parameter);
        RaiseExecuted();
    }

    public void Refresh()
    {
        RaiseCanExecuteChanged();
    }

    protected virtual void RaiseExecuted()
    {
        var handler = Executed;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }

    protected virtual void RaiseCanExecuteChanged()
    {
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }
}

      

The WPF version of this class uses indirectly CommandManager.InvalidateRequerySuggested

by implementing it CanExecuteChanged

as follows:



public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}

      

However, this is not supported in WinRT, and in my version of WinRT, any code that invalidates the command state of a delegate must invoke a method Refresh

to force bound items in the view to prompt for the command.

I think the best solution to your specific problem would be to implement INotifyPropertyChanged

in your view model. Raising an event PropertyChanged

on this interface is equivalent to my method Refresh

and causes the related items in the view to reevaluate themselves and thus rerun all related instances IValueConverter

.

+3


source


According to Microsoft Developer Network, it works with Windows 8 and Framework 4.5. However, it states the following:

The CommandManager only pays attention to certain conditions in determining when the target command changes, such as changing the keyboard focus. In situations where the CommandManager is not sufficient to determine the changed conditions that cause the command to fail, InvalidateRequerySposed can be called to cause the CommandManager to raise the RequerySposed event.

But since it does not mention Windows Mobile for Windows lower than WinRT compatible, the above command may not work for these devices, but if it is for Windows Store for WinRT and Windows 8, it should work fine.



If I misunderstood your question, please let me know and I will try to help further.

An article about the team is here:

0


source







All Articles