How to make an event work with Xamarin.Forms and c#

Asked

Viewed 321 times

2

When I declare an event on my Mainpage.xaml, I cannot do the implantation. In the visual says that the implantation was made successfully, but in my cell is all white and can not even navigate it, even music if I hear I can not stop or advance. If I remove the event statement, 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 the following: Sometimes when I start the project without the event statement, my smartphone screen turns all white, turn off the application by VS and open by the cell directly and it works. When however, I leave the event enabled, the screen turns white. I turn off the application by VS and when I go by Cel direct, gives this message: Test1.Android stopped. I don’t even know what can be.

1 answer

3


His method is called btnAdicionar_OnClicked while the call of the event Clicked button is pointing to btnAdicionar_Clicked

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

  • I didn’t even notice it, I stayed so deep in the house that passed beaten this. I’m testing, it’s just that it’s very slow everything here. Anyway, thanks for getting me out of the house.

  • All right. And then study PRISM. It will make you leave the little house even more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.