将路由事件处理程序应用于应用程序栏按钮

Apply a routed event handler to an application bar button

我正在尝试将路由事件处理程序分配给包含路由事件参数的方法。我不断收到此错误:

Cannot implicitly convert type 'System.Windows.RoutedEventHandler' to 'System.EventHandler'

函数如下:

private void lists_AddListButton_Click(object sender, RoutedEventArgs e)
    {
        using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (Stream stream = storage.CreateFile("list.xml"))
            {
                XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("lists", new XElement("list", new XElement("name", "random list"), new XElement("date", DateTime.Now.ToString()))));
                document.Save(stream);

                var items = (from query in document.Descendants("list")
                            select new ListsXmlBinder
                            {
                                Name = query.Element("name").Value,
                                Date = query.Element("date").Value
                            }).ToList();

                lists_ListViewer.ItemsSource =  items;
            }
        }
    }

这是我尝试分配事件处理程序的地方:

private void BuildLocalizedApplicationBar()
    {
       // Set the page's ApplicationBar to a new instance of ApplicationBar.
        ApplicationBar = new ApplicationBar();

        // Create a new button and set the text value to the localized string from AppResources.
        ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        appBarButton.Text = AppResources.AppBarButtonText;
        appBarButton.Click += new RoutedEventHandler(lists_AddListButton_Click);
        ApplicationBar.Buttons.Add(appBarButton);

        // Create a new menu item with the localized string from AppResources.
        ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        ApplicationBar.MenuItems.Add(appBarMenuItem);
    }

事件处理程序是新 RoutedEventHandler 部分所在的位置。关于如何解决这个问题有什么建议吗?

我看不出您需要 RoutedEventHandler 的任何原因。 将 lists_AddListButton_Click 的 RoutedEventArgs 参数更改为简单的 EventArgs - 您甚至不在方法中使用它。 然后你可以将代码更改为 appBarButton.Click += lists_AddListButton_Click;

我猜,ApplicationBarIconButton.Click是EventHandler,不是RoutedEventHandler。所以尝试改变

appBarButton.Click += new RoutedEventHandler(lists_AddListButton_Click);

appBarButton.Click += new EventHandler(lists_AddListButton_Click);

或者只是

appBarButton.Click += lists_AddListButton_Click;

正如 Jogy 推荐的那样

void lists_AddListButton_Click(object sender, RoutedEventArgs e)

void lists_AddListButton_Click(object sender, EventArgs e)