【问题标题】:The best overloaded method match has some invalid arguments C# WPF LEAP最佳重载方法匹配有一些无效参数 C# WPF LEAP
【发布时间】:2015-05-06 03:32:35
【问题描述】:

错误 1 ​​'LeapController1.LeapEventListener.LeapEventListener(LeapController1.ILeapEventDelegate)' 的最佳重载方法匹配有一些无效参数

错误 2 参数 1:无法从 'LeapController1.MainWindow' 转换为 'LeapController1.ILeapEventDelegate'

我正在尝试将数据更新到 GUI 上的标签,但我不知道出了什么问题。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Leap; //using leap motion library

namespace LeapController1
{

public partial class MainWindow : Window
{

    private Controller controller = new Controller();


    private LeapEventListener listener;
    public MainWindow()
    {
        InitializeComponent();
        this.controller = new Controller();
        this.listener = new LeapEventListener(this);
        controller.AddListener(listener);

        Console.ReadKey(); //prevent console output from closing
        controller.RemoveListener(listener); //remove listener from controller
        controller.Dispose(); //dispose controller
    }

    delegate void LeapEventDelegate(string EventName);

    public void LeapEventNotification(string EventName)
    {
        if (this.CheckAccess())
        {
            switch (EventName)
            {
                case "onInit":
                    txtInit.Content = "Initialised";
                    break;

                case "onConnect":
                     txtConnect.Content = "Connected";
                     this.connectHandler();
                     break;

                case "onDisconnect":
                    txtConnect.Content = "Disconnected";
                    break;

                case "onFrame":
                    txtFrame.Content = " on Frame";
                    this.movement(this.controller.Frame());
                    break;
            }
        }
        else
        {
            Dispatcher.Invoke(new LeapEventDelegate(LeapEventNotification), new object[] { EventName });
        }
    }//end method LeapEventNotification

    public void connectHandler()
    {
        this.controller.SetPolicyFlags(Controller.PolicyFlag.POLICY_IMAGES);
        this.controller.SetPolicy(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES);
        this.controller.EnableGesture(Gesture.GestureType.TYPE_KEY_TAP);
        this.controller.EnableGesture(Gesture.GestureType.TYPE_SCREEN_TAP);
        this.controller.EnableGesture(Gesture.GestureType.TYPE_SWIPE);
        this.controller.EnableGesture(Gesture.GestureType.TYPE_CIRCLE);
        this.controller.Config.SetFloat("Gesture.Swipe.MinLength", 100.0f);
    }

    public void movement(Leap.Frame frame)
    {
        HandList allHands = frame.Hands; //get hand data array
        foreach (Hand hand in allHands) //run for each element in array 
        {
            Leap.Vector normal = hand.PalmNormal; //get hand.PalmNormal data
            Leap.Vector direction = hand.Direction; //get hand.Direction data

            double pitch = direction.Pitch; //get pitch data
            double pitch1 = (pitch) * (180 / Math.PI); //convert rad to deg
            int finalpitch = (int)(pitch1); //nearest whole number

            double roll = normal.Roll; //get roll data
            double roll1 = (roll) * (180 / Math.PI); //convert rad to deg
            int finalroll = (int)(roll1); //nearest whole number

            txtPitch.Content = finalpitch; //assign data to label
            txtRoll.Content = finalroll; //assign data to label

        }

        GestureList gestures = frame.Gestures(); //returns a list of gestures

            for (int i = 0; i < gestures.Count(); i++) //run when gesture made
            {
                Gesture gesture = gestures[i]; //gesture at that instant

                switch (gesture.Type) //check gesture type
                {
                    //if gesture.Type == TYPE_SWIPE
                    case Gesture.GestureType.TYPE_SWIPE:
                        txtGesture.Content = "SWIPE";
                        break;

                    //if gesture.Type == TYPE_SCREEN_TAP
                    case Gesture.GestureType.TYPE_SCREEN_TAP:
                        txtGesture.Content = "SCREEN TAP";
                        break;

                    //if gesture.Type == TYPE_KEY_TAP
                    case Gesture.GestureType.TYPE_KEY_TAP:
                        txtGesture.Content = "KEY TAP";
                        break;

                    //if gesture.Type == neither of the above
                    default:
                        txtGesture.Content = "UNKNOWN";
                        break;
                }
            }
    }

}

public interface ILeapEventDelegate
{
    void LeapEventNotification(string EventName);
}

public class LeapEventListener : Listener
{
    ILeapEventDelegate eventDelegate;

    public LeapEventListener(ILeapEventDelegate delegateObject)
    {
        this.eventDelegate = delegateObject;
    }

    public override void OnInit(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onInit");
    }

    public override void OnConnect(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onConnect");
    }

    public override void OnDisconnect(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onDisconnect");      
    }

    public override void OnFrame(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onFrame");
    }       
} 

}

【问题讨论】:

    标签: c# wpf leap-motion


    【解决方案1】:

    通过这一行,您尝试通过传入 MainWindow 类的当前实例来创建新的 LeapEventListener

    this.listener = new LeapEventListener(this);
    

    没有LeapEventListener 的构造函数接受MainWindow,也无法将MainWindow 隐式转换为ILeapEventDelegate


    您需要创建一个实现接口ILeapEventDelegate 的类。该类需要实现LeapEventNotification 方法的行为。


    更新

    Charles Ward 在对此答案的评论中注明...

    Leap Motion docs 中有一个示例——注意 MainWindow 类也实现了 ILeapEventDelegate。

    查看原始问题中的代码,您的 MainWindow 已经实现了 LeapEventNotification 方法。您需要做的就是更改您的类声明以显式实现ILeapEventDelegate 接口。

    public partial class MainWindow : Window, ILeapEventDelegate
    {
        ...
    

    【讨论】:

    猜你喜欢
    • 2014-05-17
    • 2014-04-14
    • 2015-10-14
    • 2018-07-14
    • 2013-01-07
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    相关资源
    最近更新 更多