命令绑定无法转换类型'System.Reflection.RuntimeEventInfo“对象键入'System.Reflection.MethodInfo” [英] Command binding Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'

查看:4010
本文介绍了命令绑定无法转换类型'System.Reflection.RuntimeEventInfo“对象键入'System.Reflection.MethodInfo”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的按钮线了通过XAML的命令,我得到一个运行时错误
System.Windows.Markup.XamlParseException:在System.Windows.Data.Binding提供价值引发了异常。 ---> System.InvalidCastException:无法投型System.Reflection.RuntimeEventInfo对象键入'System.Reflection.MethodInfo

When I wire up my button to a command via XAML, I'm getting a run time error System.Windows.Markup.XamlParseException: Provide value on 'System.Windows.Data.Binding' threw an exception. ---> System.InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.

当我删除命令在XAML绑定一切工作好,和我的项目展示等。
这里是命令绑定:

When I remove the command binding in XAML everything works okay, and my items display etc.. Here is the command binding:

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

code要连接视图模型(code我的窗口后面):

Code to wire up view model (in code behind of my window):

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

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

Here is my view Model My view model is the data context of my view

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<Alert> _alerts = new List<Alert>();
        public List<Alert> 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

<UserControl x:Class="Arkle.SharedUI.View.AlertsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:viewModel="clr-namespace:Arkle.SharedUI.ViewModel"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type=viewModel:AlertViewModel, IsDesignTimeCreatable=True}"
        x:Name="EarlyPriceEditorViewModelWindow"
    Height="Auto" Width="Auto">
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </UserControl.Resources>

    <Grid Name="MainGrid">
        <StackPanel Name="MainStackPanel">
            <ListBox   ItemsSource="{Binding Path=Alerts}"   >
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal" >
                        </WrapPanel>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Visibility="{Binding IsAlerting,Converter={StaticResource BooleanToVisibilityConverter}}">
                            <StackPanel Orientation="Horizontal">
                                <Button Content="{Binding BackgroundMessage}" HorizontalAlignment="Left"  Width="75"  VerticalAlignment="Top" Height="Auto"  Margin="2" 
                                    Click="{Binding ElementName=MainGrid, Path=DataContext.AlertClickCommand}" CommandParameter="{Binding}"

                                 />
                                <Label Content="{Binding OverlayMessage}" HorizontalAlignment="Left" Width="Auto" Margin="1,0,0,0" VerticalAlignment="Top" Background="Red" Foreground="White"
                                   FontWeight="Bold">
                                    <Label.Style>
                                        <Style>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsAlerting}" Value="True">
                                                    <Setter  Property="Image.Visibility" Value="Visible" />
                                                    <DataTrigger.EnterActions>
                                                        <BeginStoryboard x:Name="ImageFlash">
                                                            <Storyboard>
                                                                <DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)"
                                                BeginTime="0:0:0" Duration="0:0:0.5"
                                                From="1.0" To="0.0" RepeatBehavior="Forever" AutoReverse="True"/>
                                                            </Storyboard>
                                                        </BeginStoryboard>
                                                    </DataTrigger.EnterActions>
                                                    <DataTrigger.ExitActions>
                                                        <StopStoryboard BeginStoryboardName="ImageFlash" />
                                                    </DataTrigger.ExitActions>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding IsAlerting}" Value="False">
                                                    <DataTrigger.Setters>
                                                        <Setter  Property="Image.Visibility" Value="Collapsed" />
                                                    </DataTrigger.Setters>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>

                                    </Label.Style>
                                </Label>
                                <Label Content="|" Margin="5"/>
                            </StackPanel>


                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

            </ListBox>


        </StackPanel>
    </Grid>

</UserControl>

这是我的命令

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<ShowDangerBetsRequestedEvent>().Publish(null);
                    break;
                default:
                    return;
            }
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;
    }
}

我也得到了以下的设计时错误(见截图)
无法转换类型'System.Windows.Data.Binding对象键入'Microsoft.Ex pression.Markup.DocumentModel.DocumentNode。

I also get the following design time error (see screenshot) Unable to cast object of type 'System.Windows.Data.Binding' to type 'Microsoft.Expression.Markup.DocumentModel.DocumentNode'.

第一个运行时错误 - 抛出运行

First Runtime Error - Throws Run

后续运行时错误一再抛出:
System.Windows.Markup.XamlParseException:在System.Windows.Data.Binding提供价值引发了异常。 ---> System.InvalidCastException:无法投型System.Reflection.RuntimeEventInfo对象键入'System.Reflection.MethodInfo

Subsequent runtime errors throws repeatedly: System.Windows.Markup.XamlParseException: Provide value on 'System.Windows.Data.Binding' threw an exception. ---> System.InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.

在MS.Internal.Helper.CheckCanReceiveMarkupExtension(的MarkupExtension的MarkupExtension,的IServiceProvider的ServiceProvider,DependencyObject的&安培; targetDependencyObject,DependencyProperty的&安培; targetDependencyProperty)

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

在System.Windows.Data.BindingBase.ProvideValue(的IServiceProvider的ServiceProvider)

at System.Windows.Data.BindingBase.ProvideValue(IServiceProvider serviceProvider)

在MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(我的MarkupExtension,的IServiceProvider的ServiceProvider)

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

---内部异常堆栈跟踪的结尾---

--- End of inner exception stack trace ---

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

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

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

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

推荐答案

您不绑定到点击属性命令。在点击属性是增加一个传统的事件处理程序点击事件。你想使用命令属性绑定你的命令。

You don't bind a command to the Click property. The Click property is for adding a traditional event handler to the Click event. You want to use the Command property to bind your command.

<Button Content="{Binding BackgroundMessage}" 
        HorizontalAlignment="Left"  Width="75"  
        VerticalAlignment="Top" Height="Auto"  Margin="2" 
        Command="{Binding ElementName=MainGrid, 
                          Path=DataContext.AlertClickCommand}" 
        CommandParameter="{Binding}" />

这篇关于命令绑定无法转换类型'System.Reflection.RuntimeEventInfo“对象键入'System.Reflection.MethodInfo”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆