DigitalMath
DigitalMath

Reputation: 85

No overload for method matches delegate?

I am new to c# and I am trying to learn Windows Phone development. What I am trying to do is simply be able to move a rectangle with your finger but I get this error:

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

I have seen this 'No overload for method matches delegate' question asked before but since I am new I am a little confused on what is going on.

Here is the full code:

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

Thanks

Upvotes: 2

Views: 1819

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70691

Neither version of the event subscription you show in your post – the commented-out one, and especially the one before that (using an event handler delegate type itself as the type parameter for the EventHandler<T> type just makes no sense at all in any context) – use a type that is consistent with your actual method, hence the error.

The compiler complains that no overload matches, because it is theoretically possible to have multiple methods having the same name. So an error that simply says "the method" doesn't match wouldn't make sense. It is the case that none of the available methods with that name match; in this case, it just happens there's only one available method.

Without the declaration for the myRectangle object and its type, and in particular the ManipulationDelta event, it's not possible to know for sure what you need to do.

However, most likely you can just get rid of the explicit delegate type altogether:

myRectangle.ManipulationDelta += Drag_ManipulationDelta;

Then you don't need to guess as to what type to use for the delegate instance initialization. The compiler will infer the correct type and even the delegate instantiation on your behalf.

If that doesn't work, then you need to fix the method declaration so that it does match the event's delegate type. Without seeing the event declaration and its type, I can't offer any specific advice about that.


EDIT:

Per your explanation that you are trying to follow the example at Quickstart: Touch input for Windows Phone 8, I can see that your method declaration is incorrect.

The event is declared as EventHandler<ManipulationDeltaEventArgs>, but your method uses ManipulationDeltaRoutedEventArgs as the type for its second parameter. Why Microsoft chose to change the name between the older Phone API and the new XAML/WinRT API I don't know. But it's important to keep the right type:

void Drag_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)

Note that my previous comment still applies as well; you don't need to specify the delegate type when subscribing. You can write the event subscription as I showed originally above.

Upvotes: 2

Related Questions