Когда я пытаюсь перейти со страницы входа на masterDetailpage, я получаю сообщение об ошибке « System.Reflection.TargetInvocationException Message = Исключение было сгенерировано целью вызова. "
Я попытался сделать это в функции входа в систему, когда пользователь успешно вошел в систему
async void Login()
{
try
{
var current = Connectivity.NetworkAccess;
if (current == NetworkAccess.Internet)
{
if (IsValidated)
{
await navigationRef.PushPopupAsync(new LoaderPage()).ConfigureAwait(false);
userDetails = await webServicesRepository.UserLogin(Phone, Password, "123113123", "Android").ConfigureAwait(false);
if (userDetails != null && userDetails.status == "200")
{
await navigationRef.PopPopupAsync().ConfigureAwait(false);
MasterDetailPage fpm = new MasterDetailPage
{
Master = new MainMenuPageMaster(),
Detail = new NavigationPage(new HomePage())
};
Application.Current.MainPage = fpm;
//Application.Current.MainPage = new NavigationPage(new MainMenuPage());
//await navigationRef.PushAsync(new MainMenuPage()).ConfigureAwait(false);
}
else
{
await navigationRef.PopPopupAsync().ConfigureAwait(false);
await Application.Current.MainPage.DisplayAlert(Constants.DISPLAY_POPUP_TITLE, Constants.NO_DATA_FOUND, Constants.DISPLAY_POPUP_BUTTON).ConfigureAwait(false);
}
}
else
{
await Application.Current.MainPage.DisplayAlert(Constants.DISPLAY_POPUP_TITLE, "You must fill all areas correctly!", Constants.DISPLAY_POPUP_BUTTON).ConfigureAwait(false);
}
}
else
{
await Application.Current.MainPage.DisplayAlert(Constants.DISPLAY_NETWORK_ERROR_TITLE, Constants.DISPLAY_NETWORK_ERROR_MESSAGE, Constants.DISPLAY_POPUP_BUTTON).ConfigureAwait(false);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
await Application.Current.MainPage.DisplayAlert(Constants.DISPLAY_POPUP_TITLE, Constants.NO_DATA_FOUND, Constants.DISPLAY_POPUP_BUTTON).ConfigureAwait(false);
}
}
MasterDetailPage:
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="intSetSet.MainMenuPage"
xmlns:views="clr-namespace:XXXXXX"
NavigationPage.HasNavigationBar="False"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true">
<MasterDetailPage.Master>
<views:MainMenuPageMaster x:Name="masterPage" Title="Menu" Icon="ListIcon.png" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<views:HomePage />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
c #
public partial class MainMenuPage : MasterDetailPage
{
public MainMenuPage()
{
InitializeComponent();
try
{
masterPage.MenuItemsListView.ItemSelected += ListView_ItemSelected;
MessagingCenter.Subscribe<EventArgs>(this, "OpenMenu", args =>
{
IsPresented = !IsPresented;
});
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as MainMenuPageModel;
if (item == null)
return;
var page = (Page)Activator.CreateInstance(item.TargetType);
//var page = item.TargetType;
page.Title = item.Title;
Detail = new NavigationPage(page);
IsPresented = false;
masterPage.MenuItemsListView.SelectedItem = null;
}
}
ContentPage для генерации параметров списка:
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="XXXXX.MainMenuPageMaster"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true">
<StackLayout>
<ListView x:Name="MenuItemsListView"
SeparatorVisibility="None"
x:FieldModifier="public"
HasUnevenRows="true"
ItemsSource="{Binding MenuItems}">
<ListView.Header>
<StackLayout BackgroundColor="#CEDAEE">
<StackLayout Orientation="Horizontal"
Padding="20">
<Image Margin="10,10,0,0"
Source="setsetlogo.png"
WidthRequest="80"
HeightRequest="80"
HorizontalOptions="Start"></Image>
<Label HorizontalOptions="StartAndExpand"
VerticalTextAlignment="Center"
Text="IntSetSet"
TextColor="White"
FontSize="24" />
</StackLayout>
<BoxView BackgroundColor="Teal"
HorizontalOptions="FillAndExpand"
HeightRequest="1"></BoxView>
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HorizontalOptions="FillAndExpand"
BackgroundColor="#CEDAEE">
<StackLayout Padding="10,10"
Orientation="Horizontal">
<Image Source="{Binding Icon}"
WidthRequest="40"
HeightRequest="40"
VerticalOptions="Center"
HorizontalOptions="Start"></Image>
<Label VerticalOptions="Center"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start"
HorizontalOptions="StartAndExpand"
TextColor="White"
Text="{Binding Title}"
FontSize="Medium" />
</StackLayout>
<BoxView BackgroundColor="Teal"
HorizontalOptions="FillAndExpand"
HeightRequest="1"></BoxView>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<StackLayout BackgroundColor="#CEDAEE" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<Button Command="{Binding LogoutCommand}" Text="LogOut" BackgroundColor="#CEDAEE" TextColor="Yellow" HorizontalOptions="EndAndExpand">
</Button>
<!--<Button Clicked="LogoutButton_Clicked" Text="LogOut" BackgroundColor="#CEDAEE" TextColor="Yellow" HorizontalOptions="EndAndExpand">
</Button>-->
</StackLayout>
</ListView.Footer>
</ListView>
</StackLayout>
</ContentPage>
C #
public partial class MainMenuPageMaster : ContentPage
{
public MainMenuPageMaster()
{
InitializeComponent();
BindingContext = new MainMenuPageViewModel();
}
}
Просмотр модели:
public class MainMenuPageViewModel : INotifyPropertyChanged
{
public ObservableCollection<MainMenuPageModel> MenuItems { get; set; }
public ICommand LogoutCommand { get; }
public MainMenuPageViewModel()
{
try
{
MenuItems = new ObservableCollection<MainMenuPageModel>(new[]
{
new MainMenuPageModel { Id = 0, Title = "Home", Icon="shop.png", TargetType= typeof(HomePage) },
new MainMenuPageModel { Id = 1, Title = "Profile", Icon="shop.png", TargetType= typeof(ProfilePage) },
new MainMenuPageModel { Id = 2, Title = "Referral History", Icon="paper_money.png", TargetType= typeof(ReferralHistoryPage) },
});
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged == null)
return;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Я хочу перейти со страницы входа на страницу с основными данными при успешном входе