Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


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:
C#
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;
            }
		}
	}
}
Posted
Updated 16-Aug-18 10:40am

1 solution

Move your event handler definition outside the class constructor. For example:

C#
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;
            }
	}
}
 
Share this answer
 
Comments
Sascha Manns 17-Aug-18 14:26pm    
Thank you very much. Your tip works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900