Visual Studio 2012中无法编译的编译错误

时间:2013-09-24 17:02:36

标签: c# xaml windows-phone-7 visual-studio-2012

Visual Studio的C#编译器会指出一个没有基础的错误: “'ItemSelection'没有重载与MainPage.xaml中的委托'OnItemSelected'相匹配” (或者我是盲目的,看不到错误)早期的项目可以编译,尽管出现错误的事实,但现在它没有(有什么奇怪的)。 谢谢您的帮助。

//MainPage.xaml
<ListBox.ItemTemplate>
    <DataTemplate>
        <local:WordBlock   Word="{Binding}" OnSlideOccured="OnSlide"  OnItemSelection="ItemSelection"/>
    </DataTemplate>
</ListBox.ItemTemplate>
//MainPage.xaml.cs
private void ItemSelection(string word)
{
    ToSearch = word;
    (Panorama.Items[0] as PanoramaItem).Visibility = Visibility.Collapsed;
    Panorama.SetValue(Panorama.SelectedItemProperty, Panorama.Items[0]);
    Panorama.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    (Panorama.Items[0] as PanoramaItem).Visibility = Visibility.Visible;
}
//WordBlock.xaml.cs
public delegate void OnItemSelected(string word);
public event OnItemSelected OnItemSelection;

//MainPage.xaml <ListBox.ItemTemplate> <DataTemplate> <local:WordBlock Word="{Binding}" OnSlideOccured="OnSlide" OnItemSelection="ItemSelection"/> </DataTemplate> </ListBox.ItemTemplate> //MainPage.xaml.cs private void ItemSelection(string word) { ToSearch = word; (Panorama.Items[0] as PanoramaItem).Visibility = Visibility.Collapsed; Panorama.SetValue(Panorama.SelectedItemProperty, Panorama.Items[0]); Panorama.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); (Panorama.Items[0] as PanoramaItem).Visibility = Visibility.Visible; } //WordBlock.xaml.cs public delegate void OnItemSelected(string word); public event OnItemSelected OnItemSelection;

1 个答案:

答案 0 :(得分:0)

显然,xaml中使用的事件需要第一个object类型的参数。 将代表更改为:

public delegate void OnItemSelected(object sender,string word);

和处理程序:

private void ItemSelection(object sender,string word)

似乎解决了编译问题(或者您可以使用EventHandler<string>作为事件类型)。