How to make an event work with xamarin.forms and C #

2

When I declare an event in my MainPage.xaml, I can not do the deployment. In the visual it says that the implantation was done successfully, but in my cell everything is white and can not even navigate in it, even music if I am listening I can not stop or move forward. If I remove the event declaration, it works. My 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"
             xmlns:local="clr-namespace:Teste1"
             x:Class="Teste1.MainPage">

    <StackLayout Orientation="Vertical">
        <StackLayout Padding="5,5,0,0">
            <Label Text="Adicionar um Produto" TextColor="Green" />
        </StackLayout>
        <StackLayout Padding="10,0,10,0">
            <Entry x:Name="txtNome" Placeholder="Nome do produto" HorizontalOptions="Start" 
                    VerticalOptions="StartAndExpand" HeightRequest="40" WidthRequest="300" FontSize="Small"/>
            <Entry x:Name="txtCategoria" Placeholder="Categoria do produto" HorizontalOptions="Start" 
                    VerticalOptions="StartAndExpand" HeightRequest="40" WidthRequest="300" FontSize="Small" />
            <Entry x:Name="txtPreco" Placeholder="Preço do produto" HorizontalOptions="Start" VerticalOptions="StartAndExpand" 
                    HeightRequest="40" WidthRequest="300" FontSize="Small" />
            <Button HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" HeightRequest="40" Text="Adicionar/Atualizar Produto" 
                    Clicked="btnAdicionar_Clicked" FontSize="Small"/>
        </StackLayout>

        <StackLayout Orientation="Vertical" Padding="10,5,10,0">
            <ListView x:Name="listaProdutos" BackgroundColor="Aqua" SeparatorColor="Blue">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.ContextActions>
                                <MenuItem Clicked="OnAtualizar" CommandParameter="{Binding .}" Text="Atualizar" />
                                <MenuItem Clicked="OnDeletar" CommandParameter="{Binding .}" Text="Deletar" IsDestructive="True" />
                            </ViewCell.ContextActions>
                            <StackLayout Padding="10,10" Orientation="Horizontal">
                                <Label Text="{Binding Nome}" HorizontalOptions="StartAndExpand"/>
                                <Label Text="{Binding Categoria}" TextColor="Blue" HorizontalOptions="Center"/>
                                <Label Text="{Binding Preco}" HorizontalOptions="End"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </StackLayout>

</ContentPage>

And my MainPage.xaml.cs with the event:

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();            
        }

        private async void btnAdicionar_OnClicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new MainPageModal());
        }
    }

What happens is: Sometimes when I start the project without the event declaration, the screen of my smartphone turns white, I switch off the application via VS and open the cel directly and it works. When I leave the event enabled, the screen turns white. I unplug the application by VS and when I go through the direct Cel, gives this message: Teste1.Android stopped. I do not really know what that can be.

    
asked by anonymous 29.08.2017 / 21:58

1 answer

3

Your method is called btnAdicionar_OnClicked while the Clicked button call is pointing to btnAdicionar_Clicked

Make the correction that your content will appear on the device / emulator

    
29.08.2017 / 22:16