方法匹配委托没有重载

本文关键字:重载 方法 | 更新日期: 2023-09-27 18:36:44

我是c#的新手,我正在尝试学习Windows Phone开发。我正在尝试做的只是能够用手指移动一个矩形,但我收到此错误:

Error   1   No overload for 'Drag_ManipulationDelta' matches delegate 'System.EventHandler<Windows.UI.Xaml.Input.ManipulationDeltaEventHandler>'    C:'Users'Zach'documents'visual studio 2013'Projects'App2'App2'MainPage.xaml.cs  35  46  App2

我之前看过这个"方法匹配委托没有重载"的问题,但由于我是新手,我对正在发生的事情有点困惑。

以下是完整的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641
namespace App2 
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    private int buttonCount = 0;
    private TranslateTransform dragTranslation; // For changing position of myRectangle 
    private SolidColorBrush redRect = new SolidColorBrush(Windows.UI.Colors.Red);
    public MainPage()
    {
        this.InitializeComponent();
        myRectangle.ManipulationDelta += new EventHandler<ManipulationStartedEventHandler>(Drag_ManipulationDelta);

        //myRectangle.ManipulationDelta += new System.EventHandler<ManipulationDeltaEventArgs>(Drag_ManipulationDelta);
        dragTranslation = new TranslateTransform();
        myRectangle.RenderTransform = this.dragTranslation;
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }
    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // TODO: Prepare page for display here.
        // TODO: If your application contains multiple pages, ensure that you are
        // handling the hardware Back button by registering for the
        // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
        // If you are using the NavigationHelper provided by some templates,
        // this event is handled for you.
    }
    // < Called when myButton is pressed >
    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        buttonCount += 1;
        myRectangle.Fill = redRect;
        resultText.Text = "";
        // Determines visibility of myRectangle
        if(buttonCount % 2 != 0)
        {
            myRectangle.Visibility = Visibility.Visible; 
        }
        else
        {
            myRectangle.Visibility = Visibility.Collapsed;
        }         
    }
    // < Called when myRectangle is pressed >
    private void myRectangle_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        resultText.Text = "You touched the rectangle.";
    }
    void Drag_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        // Move the rectangle.
        dragTranslation.X += e.Delta.Translation.X;
        dragTranslation.Y += e.Delta.Translation.Y;
        //dragTranslation.Y += e.DeltaManipulation.Translation.Y;
    }    
}
}

谢谢

方法匹配委托没有重载

您在帖子中显示的事件订阅版本(注释掉的版本,尤其是之前的订阅版本(使用事件处理程序委托类型本身作为EventHandler<T>类型的类型参数在任何上下文中都没有意义) – 使用与您的实际方法一致的类型, 因此错误。

编译器抱怨没有重载匹配,因为理论上可以有多个具有相同名称的方法。因此,简单地说"方法"不匹配的错误是没有意义的。在这种情况下,具有该名称的可用方法都不匹配;在这种情况下,碰巧只有一种可用的方法。

如果没有myRectangle对象及其类型的声明,特别是ManipulationDelta事件,就不可能确定需要做什么。

但是,您很可能完全摆脱显式委托类型:

myRectangle.ManipulationDelta += Drag_ManipulationDelta;

然后,无需猜测委托实例初始化使用哪种类型。编译器将代表您推断正确的类型甚至委托实例化。

如果这不起作用,则需要修复方法声明,使其事件的委托类型匹配。如果没有看到事件声明及其类型,我无法对此提供任何具体建议。


编辑:

根据你尝试遵循快速入门:Windows Phone 8 触摸输入中的示例的说明,我可以看到你的方法声明不正确。

该事件声明为 EventHandler<ManipulationDeltaEventArgs> ,但您的方法使用 ManipulationDeltaRoutedEventArgs 作为其第二个参数的类型。为什么Microsoft选择在旧的电话 API 和新的 XAML/WinRT API 之间更改名称,我不知道。但保持正确的类型很重要:

void Drag_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)

请注意,我之前的评论仍然适用;订阅时无需指定委托类型。您可以按照我上面最初显示的方式编写事件订阅。