如何修复未找到的事件处理程序? [英] How do fix a not found event handler?
问题描述
我已经定义了一个使用New_CLicked事件的工具栏(在MainPage.xaml中)项目(请参阅您尝试过的内容)。这个New_Clicked事件在MainPage.xaml.cs中定义。
现在,如果我正在运行解决方案构建我得到错误
I have defined a Toolbar (in MainPage.xaml) Item which uses a "New_CLicked" Event (See What you have tried). This "New_Clicked" Event is defined in MainPage.xaml.cs.
Now, if i'm running the Solution Build i get the error
Position 8:33. EventHandler "New_Clicked" not found in type "TripLog.Views.MainPage
中找不到EventHandlerNew_Clicked也许我错过了什么?完整的解决方案代码可以查看: GitHub - saigkill / TripLog:我掌握Xamarin.Forms的解决方案 [ ^ ]
我尝试过:
MainPage.xaml:
Maybe i missed anything? The full solution code can be viewed on: GitHub - saigkill/TripLog: My Solution for Mastering Xamarin.Forms[^]
What I have tried:
MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TripLog.Views.MainPage"
Title="TripLog">
<ContentPage.ToolbarItems>
<ToolbarItem Text="New" Clicked="New_Clicked" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
<ListView x:Name="trips" ItemTapped="Trips_ItemTapped" ItemsSource="{Binding LogEntries}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Title}" Detail="{Binding Notes}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
MainPage.xaml.cs:
MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using TripLog.Models;
using TripLog.ViewModels;
namespace TripLog.Views
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
void New_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new NewEntryPage());
}
async void Trips_ItemTapped(object sender, ItemTappedEventArgs e)
{
var trip = (TripLogEntry)e.Item;
await Navigation.PushAsync(new DetailPage(trip));
// Clear selection
trips.SelectedItem = null;
}
}
}
}
推荐答案
将事件处理程序定义移到外面类构造函数。例如:
Move your event handler definition outside the class constructor. For example:
namespace TripLog.Views
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
void New_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new NewEntryPage());
}
async void Trips_ItemTapped(object sender, ItemTappedEventArgs e)
{
var trip = (TripLogEntry)e.Item;
await Navigation.PushAsync(new DetailPage(trip));
// Clear selection
trips.SelectedItem = null;
}
}
}
这篇关于如何修复未找到的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!