2016-06-10 2 views
2

bool을 매개 변수로 사용하여 PRISM delegatecommand를 사용하려고합니다. 내가 CanExecuteChangeZoomPan에 중단 점을 설정DelegateCommand throw ""지정된 캐스트가 유효하지 않습니다 "

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; 
     } 
    } 
} 

가 공격 결코 극복이는 관련 코드입니다. 문제는 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) 

:

은 스택 트레이스입니다. 그런 다음 xaml 명령 매개 변수이므로 "거짓"문자열을 인수로 사용합니다. T가 아무런 제한도없는 것처럼 보이는 이유는 무엇인가 T와 관련이없는 이유는 무엇입니까? T가 객체이거나 Nullable이어야한다는 사실을 이미 알았지 만 Nullable도 적합하지 않은 것으로 나타났습니다. bool 인수로 작업하는 방법?

감사

답변

2

는 관련 명령 매개 변수 속성은 일반적으로 문자열로 해석하므로, 어떠한 문자 객체로 입력된다. Prism이 자동으로 유형으로 변환 할 수없는 경우 올바른 유형을 전달해야하지만 XAML의 제네릭 (예 : Nullable<T>)을 사용하면 일반적으로 통증이 있으므로 권장하지 않습니다. sys 물론 mscorlib에서 System 네임 스페이스에 XMLNS 매핑입니다 {Binding Source=(sys:Boolean)true} :

는 일반적으로 단지 같은 바인딩을 사용할 수 있어야합니다 XAML에서 간단한 형식으로 변환합니다.

+0

나는 그걸로 살 수 있고, 지금은 그 해결 방법이다. 그런데 왜 위의 문제가 나타 납니까? 명령은 호출되지 않고'RaiseCanExecuteChanged' 만 호출됩니다. 그냥'CanExecuteChangeZoomPan'을 호출하면 안됩니다. 일반적인 제약 조건을 사용하는 것이 좋을까요? –

+0

'CanExecute'도 명령 매개 변수를 전달하므로 거기에서도 변환이 발생합니다. 나는 제약이 여기 어떤 것도 바꿀 것이라고 생각하지 않는다. –

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으로 복잡한 유형을 받아 들일 수 있습니다!

관련 문제