WPF交互触发器CallMethodAction [英] WPF Interaction Trigger CallMethodAction
问题描述
我在View部分的给定代码中使用了事件触发器,如下所示。几乎所有绑定都可以通过ViewModel类-MainWindowViewModel正确地进行,但是对于方法 CustomRibbonWindow_Loaded,它的抛出运行时异常如下:
I have Used Event Trigger in my View part given code as below. Almost all bindings are properly with ViewModel class - MainWindowViewModel, but for the method "CustomRibbonWindow_Loaded", its throwing runtime exception like :
类型为System的异常。 ArgumentException发生在Microsoft.Expression.Interactions.dll中,但未在用户代码
中处理。其他信息:在与预期签名匹配的类型 MainWindow的对象上找不到名为 CustomRibbonWindow_Loaded的方法。
如果有用于此异常的处理程序,则该程序可以安全地继续。
我尝试将 TargetObject = {Binding ElementName = RR} 以及 TargetObject = {Binding} 。但是似乎没有任何作用。
I have tried putting putting TargetObject="{Binding ElementName=RR}" as well as TargetObject="{Binding}" also. But none of seems working.
我在VM中的方法如下:
My method way in VM is as below,
private void CustomRibbonWindow_Loaded()
{
...
}
请指导如何解决。
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<si:CallMethodAction MethodName="CustomRibbonWindow_Loaded"/>
</i:EventTrigger>
</i:Interaction.Triggers>
MainWindow.xaml
<custom:CustomRibbonWindow x:Class="gDispatchApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent"
xmlns:custom="clr-namespace:gDispatchAppLib.Helpers.CustomUIControls;assembly=gDispatchAppLib"
xmlns:DockUI="clr-namespace:DockingLibrary;assembly=DockingLibrary"
xmlns:UserControls="clr-namespace:gDispatchAppLib.View.UserControls;assembly=gDispatchAppLib"
xmlns:AppWindows="clr-namespace:gDispatchAppLib.View.AppWindows;assembly=gDispatchAppLib"
xmlns:VM="clr-namespace:gDispatchAppLib.ViewModel.AppWindows;assembly=gDispatchAppLib"
xmlns:VM2="clr-namespace:gDispatchAppLib.ViewModel;assembly=gDispatchAppLib"
xmlns:PE="clr-namespace:gDispatchAppLib.ViewModel.CADQueues;assembly=gDispatchAppLib"
xmlns:wpfHelper="clr-namespace:gDispatchAppLib.WPFHelpers;assembly=gDispatchAppLib"
xmlns:conv="clr-namespace:gDispatch.MvvmValidation.WPF;assembly=gDispatch.MvvmValidation"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:si="http://schemas.microsoft.com/expression/2010/interactions"
Title="SERIS CAD"
MinHeight="300"
WindowState="Maximized" HorizontalContentAlignment="Stretch"
FlowDirection="LeftToRight" CaptionHeight="50"
IsIconVisible="False" WindowStyle="SingleBorderWindow"
xmlns:my="clr-namespace:System;assembly=mscorlib"
x:Name="RR">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<si:CallMethodAction MethodName="CustomRibbonWindow_Loaded" TargetObject="{Binding ElementName=RR}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
</custom:CustomRibbonWindow>
推荐答案
您的方法订阅是这样的。
Do your method subscription like this.
private void Window_Loaded(object sender, RoutedEventArgs e)
// add the proper parameters
{
// code here
}
这里是文档:链接
这篇关于WPF交互触发器CallMethodAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!