1
resposta

Menu Lateral - Xamarin Forms

Olá Paessoal,

Estou tentando implementar um menu lateral usando Master/Detail page e estou com um problema. No caso esse menu deve exibir uma lista de opções e quando clicar em uma dessas opções deve abrir uma outra pagina(View), mas quando eu clico em um dos itens que coloquei no menu ele gera uma exception. O código é o seguinte:

CodeBehind

public partial class MasterView : ContentPage
    {
        public MasterViewModel ViewModel { get; set; }

        public MasterView (ItensMenu menu)
        {
            InitializeComponent ();
            this.ViewModel = new MasterViewModel();
            this.BindingContext = this.ViewModel;

         }


        protected async override void OnAppearing()
        {
            base.OnAppearing();

            MessagingCenter.Subscribe<ItensMenu>(this, "ItemSelecionadoMenu",
                (msg) =>
                {
                    Navigation.PushAsync(new NovaView(empresa));
                   //DisplayAlert("", "", "ok");
                });


        }

    }

View:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ZonaAzulAraguaina.Views.MasterView"
             Title="Master"
             xmlns:vm="clr-namespace:ZonaAzulAraguaina.ViewModel">

    <StackLayout>
        <ListView x:Name="listaEmpresas" ItemsSource="{Binding ListaMenu}" SelectedItem="{Binding ItemSelecionado}">
            <ListView.Header>
                <StackLayout BackgroundColor="Gray" WidthRequest="100"  HeightRequest="40">
                    <Label Text="Menu de Navegação" TextColor="White" FontSize="18" VerticalOptions="CenterAndExpand" HorizontalOptions="Center"></Label>
                </StackLayout>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <StackLayout>
                                <Label Text="{Binding Nome}" FontSize="15" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"></Label>
                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

ViewModel:

public class MasterViewModel : BaseViewModel
    {
        public string teste { set; get; }
        public List<ItensMenu> ListaMenu { get; set; }

        public MasterViewModel()
        {
            this.teste = "Teste";
            this.ListaMenu = new List<ItensMenu>
            {
                new ItensMenu {Nome = "Creditos", Id = "1" },
                new ItensMenu {Nome = "Editar Perfil", Id = "2"},
                new ItensMenu {Nome = "Veiculos", Id = "3"},
                new ItensMenu {Nome = "Historico", Id = "4"},
                new ItensMenu {Nome = "Alertas", Id = "5"}
            };
        }

        private ItensMenu itemSelecionado;

        //Pegar o valor do item Selcionado do Menu
        public ItensMenu ItemSelecionado
        {
            get
            {
                return itemSelecionado;
            }
            set
            {
                itemSelecionado = value;

                if (value != null)
                {
                    MessagingCenter.Send<ItensMenu>(itemSelecionado, "ItemSelecionadoMenu");
                }
            }
        }
    }

Quando eu toco em um dos itens gera essa exception :

Unhandled Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ocorreu

Ela não aparece quando eu coloco no lugar de um "NavigationAsync" um "DisplayAlert". Desde já agradeço a quem puder ajudar.

1 resposta

Olá, Wesley

Fiz uns testes aqui, e pelo que vi o problema está na linha

Navigation.PushAsync(new NovaView(empresa));

Pode debugar ou postar o código da view NovaView aqui, por favor? Obrigado!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software