在运行时将变量传递给方法

时间:2013-10-01 06:53:48

标签: c# windows-runtime windows-store-apps winrt-xaml bing-maps

我正在开发一个Windows应用商店应用。在我的应用程序中,我想在运行时为pushpin.tapped事件创建一个图钉和方法。我的目标是将变量传递给puhpin_Tapped方法,并使用消息对话框显示变量的值。这是我的代码:

    Pushpin pushpin = new Pushpin();
    pushpin.Background = new SolidColorBrush(Colors.Blue);
    MapLayer.SetPosition(pushpin, loc);
    pushpin.Name = sd.name;
    pushpin.Tapped +=pushpin_Tapped(pushpin.Name);
    map.Children.Add(pushpin);

这是我的pushpin_Tapped方法:

    private async void pushpin_Tapped(string _name)
    {
        MessageDialog myMessage = new MessageDialog(_name);
        await myMessage.ShowAsync();
    }

但是,当我编写这些代码块时,会出现错误,因为“无法将类型'void'隐式转换为'Windows.UI.Xaml.Input.TappedEventHandler'”,在“pushpin_Tapped(pushpin.Name)”下面绘制一条红线”。

那么我做错了什么呢?谢谢......

2 个答案:

答案 0 :(得分:0)

private async void pushpin_Tapped(object sender, TappedRoutedEventArgs e)
     {
        MessageDialog myMessage = new MessageDialog(pushpin.Name);
        await myMessage.ShowAsync();
     }

答案 1 :(得分:0)

试试这个。

private async void pushpin_Tapped(object sender, TappedRoutedEventArgs e)
{
    MessageDialog myMessage = new MessageDialog(((Pushpin)sender).Name);
    await myMessage.ShowAsync();
}