命令绑定无法将“System.Reflection.RuntimeEventInfo”类型的对象强制转换为“System.Reflection.MethodInfo”类型

2024-02-08

当我通过 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<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.Expression.Markup.DocumentModel.DocumentNode”类型。

First Runtime Error - Throws Run 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 服务提供商)

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

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

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

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


您没有将命令绑定到Click财产。这Click属性用于将传统的事件处理程序添加到Click事件。您想使用Command属性来绑定您的命令。

<Button Content="{Binding BackgroundMessage}" 
        HorizontalAlignment="Left"  Width="75"  
        VerticalAlignment="Top" Height="Auto"  Margin="2" 
        Command="{Binding ElementName=MainGrid, 
                          Path=DataContext.AlertClickCommand}" 
        CommandParameter="{Binding}" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

命令绑定无法将“System.Reflection.RuntimeEventInfo”类型的对象强制转换为“System.Reflection.MethodInfo”类型 的相关文章

  • Subversion 和 Visual Studio 项目的最佳实践

    我最近开始在 Visual Studio 中处理各种 C 项目 作为大型系统计划的一部分 该系统将用于替换我们当前的系统 该系统是由用 C 和 Perl 编写的各种程序和脚本拼凑而成的 我现在正在进行的项目已经达到了颠覆的临界点 我想知道什
  • C# 正则表达式用于查找 中具有特定结尾的链接

    我需要一个正则表达式模式来查找字符串 带有 HTML 代码 中的链接 以获取文件结尾如 gif 或 png 的链接 示例字符串 a href site com folder picture png target blank picture
  • 在 C# Winforms 应用程序中嵌入 Windows XP 主题

    我有一个旧版 C Windows 窗体应用程序 其布局是根据 Windows XP 默认主题设计的 由于需要将其作为 Citrix 应用程序进行分发 该应用程序现在看起来像经典主题应用程序 因为 Citrix 不鼓励使用主题系统服务 所以
  • 从 C 结构生成 C# 结构

    我有几十个 C 结构 我需要在 C 中使用它们 典型的 C 结构如下所示 typedef struct UM EVENT ULONG32 Id ULONG32 Orgin ULONG32 OperationType ULONG32 Size
  • mprotect 之后 malloc 导致分段错误

    在使用 mprotect 保护内存区域后第一次调用 malloc 时 我遇到分段错误 这是执行内存分配和保护的代码片段 define PAGESIZE 4096 void paalloc int size Allocates and ali
  • 获取尚未实例化的类的函数句柄

    我对 C 相当陌生 我想做的事情可能看起来很复杂 首先 我想获取一些函数的句柄以便稍后执行它们 我知道我可以通过以下方式实现这一目标 List
  • 如何生成 appsettings..json 文件?

    我有一个 ASP NET Core 2 WebAPI 它将部署在以下环境中 INT QA STAGE 生产环境 基于上述 我需要有appsettings
  • 无法解析远程名称 - webclient

    我面临这个错误 The remote name could not be resolved russgates85 001 site1 smarterasp net 当我请求使用 Web 客户端读取 html 内容时 出现错误 下面是我的代
  • TcpClient 在异步读取期间断开连接

    我有几个关于完成 tcp 连接的问题 客户端使用 Tcp 连接到我的服务器 在接受客户端后listener BeginAcceptTcpClient ConnectionEstabilishedCallback null 我开始阅读netw
  • libxml2 xmlChar * 到 std::wstring

    libxml2似乎将所有字符串存储在 UTF 8 中 如xmlChar xmlChar This is a basic byte in an UTF 8 encoded string It s unsigned allowing to pi
  • 默认析构函数做了多少事情

    C 类中的默认析构函数是否会自动删除代码中未显式分配的成员 例如 class C public C int arr 100 int main void C myC new C delete myC return 0 删除 myC 会自动释放
  • 分配器感知容器和propagate_on_container_swap

    The std allocator traits模板定义了一些常量 例如propagate on container copy move assign让其他容器知道它们是否应该在复制或移动操作期间复制第二个容器的分配器 我们还有propag
  • C++11 动态线程池

    最近 我一直在尝试寻找一个用于线程并发任务的库 理想情况下 是一个在线程上调用函数的简单接口 任何时候都有 n 个线程 有些线程比其他线程完成得更快 并且到达的时间不同 首先我尝试了 Rx 它在 C 中非常棒 我还研究了 Blocks 和
  • 从 R 到 C 处理列表并访问它

    我想使用从 R 获得的 C 列表 我意识到这个问题与此非常相似 使用 call 在 R 和 C 之间传递数据帧 https stackoverflow com questions 6658168 passing a data frame f
  • 初始化 LPCTSTR /LPCWSTR [重复]

    这个问题在这里已经有答案了 我很难理解并使其正常工作 基本上归结为我无法成功初始化这种类型的变量 它需要有说的内容7 2E25DC9D 0 USB003 有人可以解释 展示这种类型的正确初始化和类似的值吗 我已查看此站点上的所有帮助 将项目
  • 从 Delphi 调用 C# dll

    我用单一方法编写了 Net 3 5 dll 由Delphi exe调用 不幸的是它不起作用 步骤 1 使用以下代码创建 C 3 5 dll public class MyDllClass public static int MyDllMet
  • 在 C++17 中使用 成员的链接错误

    我在 Ubuntu 16 04 上使用 gcc 7 2 并且需要使用 C 17 中的新文件系统库 尽管确实有一个名为experimental filesystem的库 但我无法使用它的任何成员 例如 当我尝试编译此文件时 include
  • C++、三元运算符、std::cout

    如何使用 C 用三元运算符编写以下条件 int condition1 condition2 condition3 int double result int or double std cout lt lt condition1 resul
  • 带有私有设置器的 EFCore Base 实体模型属性 - 迁移奇怪的行为

    实体模型继承的类内的私有设置器似乎会导致 EFCore 迁移出现奇怪的问题 考虑以下示例 其中有多个类 Bar and Baz 继承自Foo 跑步时Add Migration多次命令 添加 删除private修饰符 生成的模式在多个方面都是
  • 如何使用 C# 以低分辨率形式提供高分辨率图像

    尝试使用 300dpi tif 图像在网络上显示 目前 当用户上传图像时 我正在动态创建缩略图 如果创建的页面引用宽度为 500x500px 的高分辨率图像 我可以使用相同的功能即时转换为 gif jpg 吗 将创建的 jpg 的即将分辨率

随机推荐