【问题标题】:No overload for method matches delegate?方法匹配委托没有重载?
【发布时间】:2015-01-08 01:26:13
【问题描述】:

我是 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;
    }    
}
}

谢谢

【问题讨论】:

    标签: c# visual-studio windows-phone


    【解决方案1】:

    您在帖子中显示的事件订阅的两个版本 - 已注释掉的版本,尤其是 之前的版本(使用事件处理程序委托类型本身作为 @987654322 的类型参数@type 在任何情况下都毫无意义)——使用与您的实际方法一致的类型,因此会出现错误。

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

    如果没有myRectangle 对象及其类型的声明,尤其是ManipulationDelta 事件,就无法确定您需要做什么。

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

    myRectangle.ManipulationDelta += Drag_ManipulationDelta;
    

    那么您无需猜测委托实例初始化使用什么类型。编译器将代表您推断出正确的类型,甚至是委托实例化。

    如果这不起作用,那么您需要修复方法声明,使其确实匹配事件的委托类型。在没有看到事件声明及其类型的情况下,我无法就此提供任何具体建议。


    编辑:

    根据您的解释,您尝试遵循 Quickstart: Touch input for Windows Phone 8 的示例,我可以看到您的方法声明不正确。

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

    void Drag_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多