我是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;
}
}
}
由于
答案 0 :(得分:1)
您在帖子中显示的事件订阅版本都没有 - 注释掉的那个版本,而尤其是之前的版本(使用事件处理程序委托类型本身作为{{的类型参数1}}类型在任何上下文中都没有任何意义) - 使用与您的实际方法一致的类型,因此错误。
编译器抱怨没有重载匹配,因为理论上可以有多个具有相同名称的方法。所以这个错误就是简单地说&#34; 方法&#34;不匹配是没有意义的。具有该名称匹配的可用方法的 none 就是这种情况;在这种情况下,它只发生了一种可用的方法。
如果没有EventHandler<T>
对象及其类型的声明,特别是myRectangle
事件,则无法确定您需要做什么。
但是,您很可能只是完全摆脱显式委托类型:
ManipulationDelta
然后,您不需要猜测用于委托实例初始化的类型。编译器将代表您推断出正确的类型甚至委托实例化。
如果这不起作用,那么您需要修复方法声明,以使 匹配事件的委托类型。如果没有看到事件声明及其类型,我就无法提供任何具体建议。
修改强>
根据您的解释,您试图按照Quickstart: Touch input for Windows Phone 8上的示例,我可以看到您的方法声明不正确。
事件声明为myRectangle.ManipulationDelta += Drag_ManipulationDelta;
,但您的方法使用EventHandler<ManipulationDeltaEventArgs>
作为其第二个参数的类型。为什么Microsoft选择在旧的Phone API和新的XAML / WinRT API之间更改名称,我不知道。但保持正确的类型非常重要:
ManipulationDeltaRoutedEventArgs
请注意,我之前的评论仍适用;订阅时,您不需要指定委托类型。你可以按照我上面的原来编写事件订阅。