我想使用带有bool作为参数的PRISM delegate命令。这是相关的代码:
public class ChartViewModel : BindableBase
{
public DelegateCommand<bool?> ChangeZoomPanCommand { get; private set; }
private bool isInRealtimeMode;
public bool IsInRealtimeMode
{
get { return isInRealtimeMode; }
set
{
SetProperty(ref isInRealtimeMode, value);
ChangeZoomPanCommand.RaiseCanExecuteChanged();
}
}
private bool dragToZoom;
public bool DragToZoom
{
get { return dragToZoom; }
set { SetProperty(ref dragToZoom, value); }
}
public ChartViewModel()
{
ChangeZoomPanCommand = new DelegateCommand<bool?>(ExecuteChangeZoomPan, CanExecuteChangeZoomPan);
IsInRealtimeMode = true;
DragToZoom = true;
}
private bool CanExecuteChangeZoomPan(bool? arg)
{
return !IsInRealtimeMode;
}
private void ExecuteChangeZoomPan(bool? enableZoom)
{
if (enableZoom.HasValue)
{
DragToZoom = enableZoom.Value;
}
}
}
当我在CanExecuteChangeZoomPan
中设置断点时,它永远不会被击中。问题发生在ChangeZoomPanCommand.RaiseCanExecuteChanged()
之后。
这是堆栈跟踪:
&GT;
at Prism.Commands.DelegateCommand`1.<>c__DisplayClass1_0.<.ctor>b__1(Object o)
at Prism.Commands.DelegateCommandBase.CanExecute(Object parameter)
at Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.CanExecute(Object parameter)
at MS.Internal.Commands.CommandHelpers.CanExecuteCommandSource(ICommandSource commandSource)
at System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute()
at System.Windows.Controls.Primitives.ButtonBase.HookCommand(ICommand command)
at System.Windows.Controls.Primitives.ButtonBase.OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
如果我将参数类型更改为字符串,则一切似乎都有效。然后,我当然会得到一个错误的&#34; string作为参数,因为这是我的xaml命令参数。为什么这不适用于任何T,因为似乎对T没有限制。我已经发现T必须是一个对象或可以为空的困难方式,但即使是可空也似乎不适合。如何使用bool参数?
由于
答案 0 :(得分:3)
相关的命令参数属性通常作为对象输入,因此任何文字都被解释为字符串。如果Prism无法自动转换为您的类型,则必须传入正确的类型,但在XAML中使用泛型(如Nullable<T>
)通常很痛苦,所以我不建议这样做。
要在XAML中转换为简单类型,您通常应该只能使用这样的绑定:{Binding Source=(sys:Boolean)true}
,其中sys
是System
mscorlib
命名空间的XMLNS映射{1}}当然。
答案 1 :(得分:0)
你可以创建一个DelegateCommand<object>
而不是DelegateCommand<bool?>
,然后像这样处理它:
void ExecuteChangeZoomPan(object obj)
{
if (obj is bool?)
{
bool? arg = obj as bool?;
// The rest of the code goes here.
}
}
通过这种方式,ExecuteChangeZoomPan
可以非常灵活,并接受与System.DateTime
一样复杂的类型!
答案 2 :(得分:0)
这就是我创建DelegateCommand的新实例的方式
ICommand ConnectCommand = new DelegateCommand<object>(ConnectExecute,ConnectCanExecute);
然后如果您想提出更改
((DelegateCommand<object>)ConnectCommand).RaiseCanExecuteChanged();