我已将Silverlight工具包添加到我的WP7应用程序中。
<Button Content="Country">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="England" Tag="1" Click="set_country" />
<toolkit:MenuItem Header="Germany" Tag="2" Click="set_country" />
<toolkit:MenuItem Header="France" Tag="3" Click="set_country" />
<toolkit:MenuItem Header="Spain" Tag="4" Click="set_country" />
<toolkit:MenuItem Header="Italy" Tag="5" Click="set_country" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Button>
处理程序:
private void set_country(object sender, MouseEventArgs e)
{
var menuItem = (MenuItem)sender;
var tag = menuItem.Tag.ToString();
MessageBox.Show(tag);
}
但这是MainPage.g.i.cs中此字符串中的问题:
this.LayoutRoot = ((System.Windows.Controls.Grid)(this.FindName("LayoutRoot")));
错误:无法分配属性'Microsoft.Phone.Controls.MenuItem.Click'。 [行:31位置:72]
答案 0 :(得分:2)
您的事件处理程序签名错误:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = (MenuItem)sender;
MessageBox.Show("You chose to " + menuItem.Header.ToString(),"Result",MessageBoxButton.OK);
}