C# 是否没有方法匹配委托的重载?
我是c#的新手,我正在努力学习Windows Phone开发。我试图做的只是能够用手指移动一个矩形,但我得到以下错误:C# 是否没有方法匹配委托的重载?,c#,visual-studio,windows-phone,C#,Visual Studio,Windows Phone,我是c#的新手,我正在努力学习Windows Phone开发。我试图做的只是能够用手指移动一个矩形,但我得到以下错误: Error 1 No overload for 'Drag_ManipulationDelta' matches delegate 'System.EventHandler<Windows.UI.Xaml.Input.ManipulationDeltaEventHandler>' C:\Users\Zach\documents\visual studi
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
错误1“Drag\u OperationDelta”没有重载匹配委托”系统。EventHandler“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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用System.Windows;
使用System.Windows.Input;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用Windows.UI.Xaml.Shapes;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=391641
名称空间App2
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
私有int按钮计数=0;
私有TranslateTransform dragTranslation;//用于更改myRectangle的位置
private SolidColorBrush redRect=新的SolidColorBrush(Windows.UI.Colors.Red);
公共主页()
{
this.InitializeComponent();
myRectangle.ManipulationDelta+=新事件处理程序(拖动操作Delta);
//myRectangle.ManipulationDelta+=新系统.EventHandler(拖动操作Delta);
dragTranslation=新的TranslateTransform();
myRectangle.RenderTransform=this.dragtransation;
this.NavigationCacheMode=NavigationCacheMode.Required;
}
///
///当此页面即将显示在框架中时调用。
///
///描述如何到达此页面的事件数据。
///此参数通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
//TODO:准备在此处显示的页面。
//TODO:如果应用程序包含多个页面,请确保
//通过注册来处理硬件后退按钮
//Windows.Phone.UI.Input.HardwareButtons.BackPressed事件。
//如果您正在使用某些模板提供的NavigationHelper,
//此事件已为您处理。
}
//<按下myButton时调用>
私有无效myButton_单击(对象发送方,路由目标)
{
按钮计数+=1;
myRectangle.Fill=redRect;
resultText.Text=“”;
//确定myRectangle的可见性
如果(按钮计数%2!=0)
{
myRectangle.Visibility=可见性.Visibility;
}
其他的
{
myRectangle.Visibility=Visibility.collazed;
}
}
//<按下myRectangle时调用>
私有void myRectangle_PointerPressed(对象发送方,PointerRoutedEventArgs e)
{
resultText.Text=“您触摸了矩形。”;
}
void Drag_操纵delta(对象发送器,操纵deltaroutedventargs e)
{
//移动矩形。
dragtransation.X+=e.Delta.Translation.X;
dragtransation.Y+=e.Delta.Translation.Y;
//dragtransation.Y+=e.DeltaManipulation.Translation.Y;
}
}
}
感谢您在帖子中显示的事件订阅的两个版本–注释掉的版本,尤其是之前的版本(使用事件处理程序委托类型本身作为
EventHandler
类型的类型参数在任何上下文中都毫无意义)-使用与实际方法一致的类型,因此出现了错误
编译器抱怨没有重载匹配,因为理论上可能有多个方法具有相同的名称。因此,简单地说“方法”不匹配的错误是没有意义的。在这种情况下,该名称的可用方法都不匹配;在这种情况下,恰好只有一种方法可用
如果没有myRectangle
对象及其类型的声明,特别是operationdelta
事件,就无法确定需要做什么
但是,最有可能的情况是,您可以完全摆脱显式委托类型:
myRectangle.ManipulationDelta += Drag_ManipulationDelta;
然后,您不需要猜测委托实例初始化使用的类型。编译器将代表您推断正确的类型,甚至是委托实例化
如果这不起作用,则需要修复方法声明,使其与事件的委托类型匹配。在没有看到事件声明及其类型的情况下,我无法对此提供任何具体建议
编辑: 根据您的解释,您试图遵循的示例是,我可以看出您的方法声明是不正确的 该事件声明为
EventHandler
,但您的方法使用操纵DeltaroutedEventArgs
作为其第二个参数的类型。我不知道为什么微软选择在旧的手机API和新的XAML/WinRT API之间更改名称。但保持正确的类型很重要:
void Drag_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
请注意,我之前的评论仍然适用;订阅时不需要指定委托类型。您可以编写事件订阅,正如我最初在上面所示