我正在尝试将路由事件处理程序分配给包含路由事件参数的方法。我不断收到此错误:
无法将类型'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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句