Command binding 无法将类型为 'System.Reflection.RuntimeEventInfo' 的对象转换为类型 'System.Reflection.MethodInfo'。

19 浏览
0 Comments

Command binding 无法将类型为 'System.Reflection.RuntimeEventInfo' 的对象转换为类型 'System.Reflection.MethodInfo'。

当我通过XAML将按钮连接到一个命令时,我得到一个运行时错误System.Windows.Markup.XamlParseException:在‘System.Windows.Data.Binding’上提供的值引发了异常。---> System.InvalidCastException:无法将类型为‘System.Reflection.RuntimeEventInfo’的对象转换为类型‘System.Reflection.MethodInfo’。

当我在XAML中移除命令绑定时,一切都正常工作,我的项目也正常显示。

这是命令绑定的代码:

Click="{Binding ElementName=MainGrid, Path=DataContext.AlertClickCommand}"

连接视图模型的代码(在窗口的代码后台):

this.AlertsView.DataContext = GlobalStuff.AlertManager1.AlertViewModel1;

这是我的视图模型我的视图模型是视图的数据上下文

using System.Collections.Generic;
using System.ComponentModel;
using Arkle.SharedUI.Model;
using Arkle.SharedUI.ViewModel.Commands;
namespace Arkle.SharedUI.ViewModel
{
    public class AlertViewModel : INotifyPropertyChanged
    {
        private List _alerts = new List();
        public List Alerts
        {
            get { return _alerts; }
            set
            {
                _alerts = value;
                OnPropertyChanged("Alerts");
            }
        }
        public AlertViewModel()
        {
            if (DesignerProperties.IsInDesignMode)
            {
                LoadDesignTimeData();
            }
        }
        private void LoadDesignTimeData()
        {
            Alerts.Add(new Alert { BackgroundMessage = "Sis", IsAlerting = true, OverlayMessage = "3 mins", Tip = "Sis Data not received for 3 mins" });
            Alerts.Add(new Alert { BackgroundMessage = "Bets", IsAlerting = true, OverlayMessage = "4", Tip = "4 unchecked danger bets" });
            Alerts.Add(new Alert { BackgroundMessage = "Texts", IsAlerting = false, OverlayMessage = "3", Tip = "3 Unchecked Text Bets" });
        }
        private AlertClickCommand _alertClickCommand;
        public AlertClickCommand AlertClickCommand
        {
            get { return _alertClickCommand ?? (_alertClickCommand = new AlertClickCommand(this)); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这是我的XAML


    
        
    
    
        
            
                
                    
                        
                        
                    
                
                
                    
                        
                            
                                

这是我的命令

using System;
using System.Windows.Input;
using Arkle.Common;
using Arkle.SharedUI.Events;
using Arkle.SharedUI.Model;
namespace Arkle.SharedUI.ViewModel.Commands
{
    public class AlertClickCommand : ICommand
    {
        private AlertViewModel _alertViewModel;
        public AlertClickCommand(AlertViewModel alertViewModel)
        {
            _alertViewModel = alertViewModel;
        }
        public void Execute(object parameter)
        {
            if (parameter == null)
            {
                return;
            }
            var parameterAsAlert = (Alert)parameter;
            switch (parameterAsAlert.BackgroundMessage)
            {
                case "Bets":
                    EventManager.Instance.GetEvent().Publish(null);
                    break;
                default:
                    return;
            }
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }
        public event EventHandler CanExecuteChanged;
    }
}

我还遇到以下设计时错误(请参见截图)

无法将类型为‘System.Windows.Data.Binding’的对象转换为类型‘Microsoft.Expression.Markup.DocumentModel.DocumentNode’。

enter image description here

第一个运行时错误 - 引发运行时错误

enter image description here

后续运行时错误会一直重复抛出:

System.Windows.Markup.XamlParseException:在‘System.Windows.Data.Binding’上提供的值引发了异常。---> System.InvalidCastException:无法将类型为‘System.Reflection.RuntimeEventInfo’的对象转换为类型‘System.Reflection.MethodInfo’。

在MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension markupExtension,IServiceProvider serviceProvider,DependencyObject&targetDependencyObject,DependencyProperty&targetDependencyProperty)中

在System.Windows.Data.BindingBase.ProvideValue(IServiceProvider serviceProvider)中

在MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me,IServiceProvider serviceProvider)中

---内部异常堆栈跟踪结束---

在System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader,XamlObjectWriter currentWriter)中

.......................

0