将路由事件处理程序应用于应用程序栏按钮
本文关键字:应用程序 按钮 应用于 程序 路由 事件处理 | 更新日期: 2023-09-27 18:27:27
我正在尝试将路由事件处理程序分配给包含路由事件参数的方法。我经常收到这个错误:
无法将类型"System.Windows.RoutedEventHandler"隐式转换为"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
部分所在的位置。对如何解决这个问题有什么建议吗?
我猜,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)
我看不出你需要RoutedEventHandler的任何原因。将lists_AddListButton_Click的RoutedEventArgs参数更改为简单的EventArgs-您甚至不在方法中使用它。然后您可以将代码更改为appBarButton。单击+=lists_AddListButton_Click;