wpf - 委托(delegate)命令抛出 "Specified cast is not valid"

标签 wpf prism delegatecommand

我想使用带有 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() 之后.

这是堆栈跟踪:

>
    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)

如果我将参数类型更改为字符串,一切似乎都有效。然后我当然会得到一个“False”字符串作为参数,因为那是我的 xaml 命令参数。为什么这不能与任何 T 一起使用,因为 T 似乎没有限制。我已经发现 T 必须是一个对象或可为空的,但即使可以为空似乎也不合适。如何使用 bool 参数?

谢谢

最佳答案

相关的命令参数属性通常被键入为对象,因此任何文字都被解释为字符串。如果 Prism 无法自动转换为您的类型,则您必须传入正确的类型,但在 XAML 中使用泛型(如 Nullable<T> )通常很痛苦,所以我不建议这样做。

要在 XAML 中转换为简单类型,您通常应该能够使用这样的绑定(bind):{Binding Source=(sys:Boolean)true} , 其中 sys是到 System 的 XMLNS 映射。 mscorlib 中的命名空间当然。

关于wpf - 委托(delegate)命令抛出 "Specified cast is not valid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37749302/

相关文章:

c# - 将文本转换为多重绑定(bind)

wpf - 我如何使用 CompositeCommand 保存新员工的详细信息

c# - 如何在 Prism 应用程序中手动解析类型时获得正确的 INavigationService 实例

c# - 带有 MVVM 和 Prism 的 ResourceDictionary

c# - 为什么不调用此命令?

c# - DelegateCommand的重载方法

WPF 和 MVVM : Get values from textboxes and send it to ViewModel

WPF:只读说 TextBox 和绑定(bind)

c# - MVVM 模式,我不明白我应该如何编写 "Detail"ViewModel

c# - wpf自定义控件: audiowaveform rectangle with selection slider