Catel更新到5.0之后的Radwindow

时间:2017-08-18 22:01:51

标签: c# wpf mvvm catel radwindow

升级到Catel 5.0后,我开始收到如下所示的异常:

  

'System.EventHandler'类型的对象无法转换为'System.EventHandler`1 [Telerik.Windows.Controls.WindowClosedEventArgs]'的类型。

     

at System.RuntimeType.TryChangeType(Object value,Binder binder,CultureInfo culture,Boolean needsSpecialCast)      在System.Reflection.MethodBase.CheckArguments(Object []参数,Binder binder,BindingFlags invokeAttr,CultureInfo culture,Signature sig)      在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)      在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)      at Catel.Services.UIVisualizerService.HandleCloseSubscription(Object window,Object data,EventHandler`1 completedProc,Boolean isModal)in C:\ CI_WS \ Ws \ 103722 \ Source \ Catel \ src \ Catel.MVVM \ Catel.MVVM.Shared \ Services \ UIVisualizerService.wpf.cs:第141行      在C:\ CI_WS \ Ws \ 103722 \ Source \ Catel \ src \ Catel.MVVM \ Catel.MVVM.Shared \ Services \ UIVisualizerService.wpf中的Catel.Services.UIVisualizerService.ShowWindowAsync(FrameworkElement窗口,对象数据,布尔showModal)。 cs:155行      在C:\ CI_WS \ Ws \ 103722 \ Source \ Catel \ src \ Catel.MVVM \ Catel.MVVM.Shared \ Services \ UIVisualizerService.cs中的Catel.Services.UIVisualizerService.d__15.MoveNext():第222行   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

我基本上设置了所有窗口(消息框,模型窗口和主shell),以便在实现IDatawindow接口时从Radwindow继承,当我尝试使用“IUIVisualizerService”时,它就会发生。以下是一个示例:

public class MessageService : Catel.Services.MessageService, IAppMessageService
{...
public MessageService(IDispatcherService dispatcherService, IUIVisualizerService uiVisualizerService, IViewModelFactory viewModelFactory)
        : base(dispatcherService)
    {
        Argument.IsNotNull(() => dispatcherService);
        Argument.IsNotNull(() => uiVisualizerService);
        Argument.IsNotNull(() => viewModelFactory);

        this._dispatcherService = dispatcherService;
        this._uiVisualizerService = uiVisualizerService;
        this._viewModelFactory = viewModelFactory;
    }
public override Task<MessageResult> ShowAsync(string message, string caption = "", MessageButton button = MessageButton.OK, MessageImage icon = MessageImage.None)
    {...
this._dispatcherService.BeginInvoke(async () =>
        {
            var previousCursor = Mouse.OverrideCursor;
            Mouse.OverrideCursor = null;

            var vm = this._viewModelFactory.CreateViewModel<MessageBoxViewModel>(null, null);

            vm.Message = message;
            vm.Button = button;
            vm.Icon = icon;
            vm.SetTitle(caption);

            await this._uiVisualizerService.ShowDialogAsync(vm);

尝试显示如上所示的对话框时发生错误。

我正在使用Orchestra源代码提供的确切示例:

private readonly WindowLogic _logic;
    private event EventHandler<EventArgs> ViewLoaded;
    private event EventHandler<EventArgs> ViewUnloaded;
    private event EventHandler<DataContextChangedEventArgs> ViewDataContextChanged;
    private event EventHandler<WindowClosedEventArgs> ViewClosed;

    public AppMessageBoxWindow() : this(null) { }

    public AppMessageBoxWindow(IViewModel viewModel)
    {
        GetAndSetIcon();
        this.FixBlurriness();
        this.SizeToContent = true;
        this.ResizeMode = ResizeMode.NoResize;
        this.WindowStartupLocation = WindowStartupLocation.CenterOwner;

        this._logic = new WindowLogic(this, null, viewModel);
        this._logic.ViewModelChanged += (sender, args) => ViewModelChanged.SafeInvoke(this, args);
        this._logic.PropertyChanged += (sender, args) => PropertyChanged.SafeInvoke(this, args);
        this._logic.ViewModelPropertyChanged += (sender, e) => ViewModelPropertyChanged.SafeInvoke(this, e);

        Loaded += (sender, args) => ViewLoaded.SafeInvoke(this, args);
        Unloaded += (sender, args) => ViewUnloaded.SafeInvoke(this);
        DataContextChanged += (sender, e) => ViewDataContextChanged.SafeInvoke(this, new DataContextChangedEventArgs(e.OldValue, e.NewValue));

        OnViewModelChanged();
    }

    public IViewModel ViewModel => this._logic.ViewModel;
    private void GetAndSetIcon()
    {
        this.Icon = new Image { Source = new BitmapImage(ImagesUri.AppIcon) };
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public event EventHandler<EventArgs> ViewModelChanged;
    public event EventHandler<PropertyChangedEventArgs> ViewModelPropertyChanged;
    event EventHandler<EventArgs> IView.Loaded
    {
        add { ViewLoaded += value; }
        remove { ViewLoaded += value; }
    }
    event EventHandler<EventArgs> IView.Unloaded
    {
        add { ViewUnloaded += value; }
        remove { ViewUnloaded += value; }
    }
    event EventHandler<DataContextChangedEventArgs> IView.DataContextChanged
    {
        add { ViewDataContextChanged += value; }
        remove { ViewDataContextChanged += value; }
    }
    private void OnViewModelChanged()
    {
        if (ViewModel != null && !ViewModel.IsClosed)
        {
            ViewModel.ClosedAsync += ViewModelClosed;
        }
    }
    private Task ViewModelClosed(object sender, ViewModelClosedEventArgs e)
    {
        Close();
        return TaskHelper.Completed;
    }
        }

在UIVisualizerService中处理close事件时可能会发生这种情况。

1 个答案:

答案 0 :(得分:0)

这可能是由WeakEventListener的新实现引起的错误。请在https://github.com/catel/catel

上使用repro(没有任何第三方库)报告此问题