Mvvm 在视图模型中登录到xamarin.forms中的master details页时出错?

Mvvm 在视图模型中登录到xamarin.forms中的master details页时出错?,mvvm,xamarin.forms,navigation,master-detail,Mvvm,Xamarin.forms,Navigation,Master Detail,当我试图从登录页面导航到masterDetailpage时,我得到一个错误提示 “System.Reflection.targetingException” Message=调用目标已引发异常。“ 当用户成功登录时,我尝试了登录功能 async void Login() { try { var current = Connectivity.NetworkAccess; if (current == Netwo

当我试图从登录页面导航到masterDetailpage时,我得到一个错误提示 “System.Reflection.targetingException” 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);
        }
    }
母版详细信息页:

<?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>
视图模型:

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
    }
public类MainMenuPageViewModel:INotifyPropertyChanged
{
公共ObservableCollection菜单项{get;set;}
公共ICommand LogoutCommand{get;}
公共MainMenuPageViewModel()
{
尝试
{
MenuItems=新的ObservableCollection(新[]
{
新的MainMenuPageModel{Id=0,Title=“Home”,Icon=“shop.png”,TargetType=typeof(主页)},
新的MainMenuPageModel{Id=1,Title=“Profile”,Icon=“shop.png”,TargetType=typeof(ProfilePage)},
新的MainMenuPageModel{Id=2,Title=“转介历史”,Icon=“paper\u money.png”,TargetType=typeof(ReferralHistoryPage)},
});
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
#区域INotifyPropertyChanged实现
公共事件属性更改事件处理程序属性更改;
void OnPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
if(PropertyChanged==null)
返回;
调用(这是新的PropertyChangedEventArgs(propertyName));
}
#端区
}

我想在成功登录时从登录页面导航到主控详细信息页面

您需要查看InnerException以了解有关根本问题的更多详细信息is@jason如果您能共享一些引用,那就太好了Exception对象有一个InnerException属性。对于TargetInvocationException,InnerException通常包含有关异常根本原因的更多信息;我明白了:无法在未调用Looper的线程[thread-16,5,main]内创建处理程序。prepare()10-25 17:32:37.952 I/mono stdout(3397):无法在未调用Looper的线程[thread-16,5,main]内创建处理程序。prepare()@jason我已经更新了我的问题,以便您能够清楚地了解这个问题。另外,在登录函数catch块中打印内部异常。
<?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>
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
    }