本文整理汇总了C#中ICommand.CanExecute方法的典型用法代码示例。如果您正苦于以下问题:C# ICommand.CanExecute方法的具体用法?C# ICommand.CanExecute怎么用?C# ICommand.CanExecute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICommand
的用法示例。
在下文中一共展示了ICommand.CanExecute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateAndAddButton
/// <summary>
/// Creates a button and adds it to an application bar.
/// </summary>
/// <param name="appBar"></param>
/// <param name="iconFilenameRelative"></param>
/// <param name="command"></param>
/// <param name="text"></param>
/// <returns></returns>
public static ApplicationBarIconButton CreateAndAddButton(this IApplicationBar appBar, string iconFilenameRelative, ICommand command, string text)
{
ApplicationBarIconButton btn = new ApplicationBarIconButton(new Uri("/icons/" + iconFilenameRelative, UriKind.Relative));
// First-time values.
btn.IsEnabled = command.CanExecute(btn);
btn.Text = text;
// Adds click handler to execute the command upon click.
btn.Click += (o, e) =>
{
if (command.CanExecute(btn))
{
command.Execute(btn);
}
};
// Adds CanExecute changed handler.
command.CanExecuteChanged += (o, e) =>
{
btn.IsEnabled = command.CanExecute(btn);
};
// Adds the button.
appBar.Buttons.Add(btn);
return btn;
}
示例2: CreateAndAddMenuItem
/// <summary>
/// Creates a menu item and adds it to an application bar.
/// </summary>
/// <param name="appBar"></param>
/// <param name="command"></param>
/// <param name="text"></param>
/// <returns></returns>
public static ApplicationBarMenuItem CreateAndAddMenuItem(this IApplicationBar appBar, ICommand command, string text)
{
ApplicationBarMenuItem mi = new ApplicationBarMenuItem(text);
// First-time values.
mi.IsEnabled = command.CanExecute(mi);
// Adds click handler to execute the command upon click.
mi.Click += (o, e) =>
{
if (command.CanExecute(mi))
{
command.Execute(mi);
}
};
// Adds CanExecute changed handler.
command.CanExecuteChanged += (o, e) =>
{
mi.IsEnabled = command.CanExecute(mi);
};
// Adds the button.
appBar.MenuItems.Add(mi);
return mi;
}
示例3: Include
public void Include (ICommand command)
{
command.CanExecuteChanged += (s, e) => {
if (command.CanExecute (null))
command.Execute (null);
};
}
示例4: Execute
public static void Execute(ICommand command, object parameter, IInputElement target)
{
var routedCmd = command as RoutedCommand;
if (routedCmd != null && routedCmd.CanExecute(parameter, target))
routedCmd.Execute(parameter, target);
else if (command != null && command.CanExecute(parameter))
command.Execute(parameter);
}
示例5: CanExecute
public static bool CanExecute(ICommand command, object parameter, IInputElement target)
{
if (command == null)
return false;
var routedCommand = command as RoutedCommand;
return routedCommand?.CanExecute(parameter, target) ?? command.CanExecute(parameter);
}
示例6: ExecuteCommand
public static void ExecuteCommand(
ICommand command,
GestureArgs parameter = null)
{
if (command != null &&
command.CanExecute (parameter)) {
command.Execute (parameter);
}
}
示例7: CanExecuteCommand
public static bool CanExecuteCommand(ICommand command, object parameter, IInputElement target)
{
if (command == null)
return false;
RoutedCommand routed = command as RoutedCommand;
if (routed != null)
return routed.CanExecute(parameter, target);
return command.CanExecute(parameter);
}
示例8: Run
public void Run()
{
var objBindable = new DataObject();
var myBinding = new Binding("Amount", objBindable, "Amount", true, DataSourceUpdateMode.OnPropertyChanged);
var objWithBinding = new DataObjectWithBindable();
objWithBinding.PropertyChanged += (sender, args) =>
{
Console.WriteLine(args.PropertyName);
};
objWithBinding.DataBindings.Add(myBinding);
objBindable.Amount = 30 + 10;
objBindable.Message = "test";
objWithBinding.Amount = 55;
objBindable.Amount = 54;
HiButtonCommand = new RelayCommand(
ShowMessage,
param =>
{
var t = (DataObject)param;
return t.Amount < 10;
});
HiButtonCommand.CanExecuteChanged += HiButtonCommandOnCanExecuteChanged;
objBindable.Amount = 9;
if (HiButtonCommand.CanExecute(objBindable))
{
HiButtonCommand.Execute(objBindable);
}
objBindable.Amount = 11;
if (HiButtonCommand.CanExecute(objBindable))
{
HiButtonCommand.Execute(objBindable);
}
}
示例9: ExecuteRefreshCommand
protected virtual void ExecuteRefreshCommand(ICommand command)
{
if (command == null)
return;
if (!command.CanExecute(null))
return;
command.Execute(null);
}
示例10: ExecuteCommand
/// <summary>
/// Attempts to executes a command command.
/// </summary>
/// <param name="cmd">The command to be executed.</param>
/// <param name="parameter">The command parameter.</param>
/// <returns>True if the command was executed; otherwise false.</returns>
private static bool ExecuteCommand(ICommand cmd, object parameter)
{
if (cmd != null && cmd.CanExecute(parameter))
{
cmd.Execute(parameter);
return true;
}
return false;
}
示例11: CommandPropertyItem
public CommandPropertyItem(PropertyDescriptor property, object instance)
: base(property, instance)
{
_command = property.GetValue(instance) as ICommand;
if (_command != null)
{
_command.CanExecuteChanged += CanExecuteChanged;
}
_executeCommand = new Command<object>(o => _command.Execute(GetCommandParameter()), o => _command != null && _command.CanExecute(GetCommandParameter()));
}
示例12: ChainCommand
public ChainCommand(ICommand postCommand, Action<object> execute, Func<object, bool> canExecute = null)
{
if (canExecute == null) canExecute = x => true;
_execute = x => {
execute(x);
postCommand.Execute(x);
};
_canExecute = x => canExecute(x) && postCommand.CanExecute(x);
}
示例13: JSCommand
public JSCommand(HTMLViewContext context, IJavascriptToCSharpConverter converter, ICommand command)
{
_JavascriptToCSharpConverter = converter;
_HTMLViewContext = context;
_Command = command;
try
{
_InitialCanExecute = _Command.CanExecute(null);
}
catch { }
}
示例14: ExecuteCommandOnItem
protected virtual void ExecuteCommandOnItem(ICommand command)
{
if (command == null)
return;
var item = DataContext;
if (item == null)
return;
if (!command.CanExecute(item))
return;
command.Execute(item);
}
示例15: ExecuteCommand
private void ExecuteCommand(ICommand command, object parameter, IInputElement target)
{
RoutedCommand command2 = command as RoutedCommand;
if (command2 != null)
{
if (command2.CanExecute(parameter, target))
{
command2.Execute(parameter, target);
}
}
else if (command.CanExecute(parameter))
{
command.Execute(parameter);
}
}