Estou criando uma projeto em Xamarin.Forms
quem implementa um menu lateral (Master/Detail
). Até agora consegui exibir o menu corretamente e listar alguns itens nele, a ideia é ao clicar em um desses itens do menu abrir uma outra página e aqui é onde esta meu problema.
Como estou usando modelo MVVM
, minha View
está fazendo binding com uma ViewModel
, quando eu seleciono um dos itens do menu gera uma exception
que não estou conseguindo resolver.
Mostrarei o codigo para ficar mais claro:
View:
<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"/>
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding Nome}"
FontSize="15"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Code Behind:
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 VeiculoView());
App.MasterDetail.Detail.Navigation.PushAsync(new VeiculoView());
});
}
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 clico em um dos itens ocorre a seguinte exception:
Unhandled Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ocorreu
Um detalhe é que quando eu troco o codigo Navigation.Async
por um DisplayAlert
no CodeBehind ele funciona normal.
try/catch
no bloco de código que está dentro do delegate anônimo que está sendo passado noMessagingCenter
pode ajudar. Eu acredito que deva estar avendo uma vialação de acesso de thread. Já tentou escrever oPushAsync
dentro de um blocoDevice.BeginInvokeOnMainThread
? Percebi também que você está fazendo o push duas vezes... é isso mesmo? Foi intencional?catch
que você adicionou no método para testar.