WPF 交互触发器 CallMethodAction

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26835703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:41:21  来源:igfitidea点击:

WPF Interaction Trigger CallMethodAction

c#.netwpftriggers

提问by Pratik

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 :

我在给定代码的视图部分中使用了事件触发器,如下所示。几乎所有绑定都适用于 ViewModel 类 - MainWindowViewModel,但对于方法“CustomRibbonWindow_Loaded”,其抛出的运行时异常如下:

An exception of type 'System.ArgumentException' occurred in Microsoft.Expression.Interactions.dll but was not handled in user code Additional information: Could not find method named 'CustomRibbonWindow_Loaded' on object of type 'MainWindow' that matches the expected signature. If there is a handler for this exception, the program may be safely continued.

Microsoft.Expression.Interactions.dll 中出现类型为“System.ArgumentException”的异常,但未在用户代码中处理附加信息:在与预期签名匹配的“MainWindow”类型的对象上找不到名为“CustomRibbonWindow_Loaded”的方法。如果有这个异常的处理程序,程序可以安全地继续。

I have tried putting putting TargetObject="{Binding ElementName=RR}"as well as TargetObject="{Binding}"also. But none of seems working.

我也尝试将TargetObject="{Binding ElementName=RR}"以及TargetObject="{Binding}" 放入。但似乎没有一个工作。

My method way in VM is as below,

我在VM中的方法如下,

private void CustomRibbonWindow_Loaded()
    {
       ...
    }

Please guide how to resolve.

请指导如何解决。

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <si:CallMethodAction MethodName="CustomRibbonWindow_Loaded"/>
    </i:EventTrigger>

</i:Interaction.Triggers>

MainWindow.xaml

主窗口.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>

回答by DevEstacion

Do your method subscription like this.

像这样订阅你的方法。

private void Window_Loaded(object sender, RoutedEventArgs e) 
// add the proper parameters
{
    // code here
}  

Here is the documentation: Link

这是文档:链接

回答by John Henckel

I had the same problem. Just make it public. Thanks for your comment on the other answer :)

我有同样的问题。只是公开而已。感谢您对另一个答案的评论:)

public void CustomRibbonWindow_Loaded()