C# Xamarin应用程序在实现MasterDetailPage后崩溃,没有任何异常

C# Xamarin应用程序在实现MasterDetailPage后崩溃,没有任何异常,c#,android,xamarin,cross-platform,C#,Android,Xamarin,Cross Platform,在我实现MasterDeatilPage之后,我的Xamarin应用程序毫无例外地崩溃了。我坚持使用Xamarin教程()并将我的解决方案与Microsoft Docs for MasterDetailPage进行了比较。 总的来说,我已经实现了文档,唯一的区别是文件位置和我设置母版页的ItemsSource的方式。我听说母版页的未设置标题可能会导致与我相同的问题,但我确实指定了Title属性 以下是我的解决方案文件系统的摘录: 这是我的密码: MasterMenuItem.cs: using

在我实现MasterDeatilPage之后,我的Xamarin应用程序毫无例外地崩溃了。我坚持使用Xamarin教程()并将我的解决方案与Microsoft Docs for MasterDetailPage进行了比较。 总的来说,我已经实现了文档,唯一的区别是文件位置和我设置母版页的ItemsSource的方式。我听说母版页的未设置标题可能会导致与我相同的问题,但我确实指定了Title属性

以下是我的解决方案文件系统的摘录:

这是我的密码:

MasterMenuItem.cs:

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace WhaleEstimate.Models
{
    public class MasterMenuItem
    {
        public string Title { get; set; }
        public string IconSource { get; set; }
        public Color BackgroundColor { get; set; }
        public Type TargetType { get; set; }

        public MasterMenuItem(string title, string iconSource, Color color, Type type)
        {
            this.Title = title;
            this.IconSource = iconSource;
            this.BackgroundColor = color;
            this.TargetType = type;
        }
    }
}
MasterDetail.xaml:

<?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:local="clr-namespace:WhaleEstimate.Views.Menu;assembly=WhaleEstimate"
             xmlns:detailviews="clr-namespace:WhaleEstimate.Views.DetailViews;assembly=WhaleEstimate"
             x:Class="WhaleEstimate.Views.Menu.MasterDetail">
    <MasterDetailPage.Master>
        <local:MasterPage x:Name="masterpage"/>
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <detailviews:InfoScreen1/>
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>
MasterPage.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:WhaleEstimate.Models"
             x:Class="WhaleEstimate.Views.Menu.MasterPage"
             Title="Test Project">
    <ContentPage.Content>
        <StackLayout x:Name="MasterStack" VerticalOptions="FillAndExpand">
            <StackLayout x:Name="TopStack">
                <Label Text="TestProject App" HorizontalOptions="Center" FontSize="Large"/>
            </StackLayout>

            <StackLayout x:Name="MidStack" VerticalOptions="FillAndExpand">
                <ListView x:Name="listView" SeparatorVisibility="None" x:FieldModifier="public">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid BackgroundColor="{Binding BackgroundColor}">
                                    <Image Source="{Binding IconSource}" Margin="0,10,0,10"/>
                                    <Label Grid.Column="1" Text="{Binding Title}" TextColor="Black" FontSize="Medium"/>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>

            <StackLayout x:Name="BottomStack" VerticalOptions="EndAndExpand">
                <Button Text="Do some"/>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

MasterPage.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WhaleEstimate.Models;
using WhaleEstimate.Views.DetailViews;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace WhaleEstimate.Views.Menu
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MasterPage : ContentPage
    {
        public ListView ListView { get { return ListView; } }
        public List<MasterMenuItem> items;

        public MasterPage ()
        {
            InitializeComponent ();
            SetItems();
        }

        private void SetItems()
        {
            items = new List<MasterMenuItem>();
            items.Add(new MasterMenuItem("InfoScreen1", "maus.jpg", Color.White, typeof(InfoScreen1)));
            items.Add(new MasterMenuItem("InfoScreen2", "maus.jpg", Color.White, typeof(InfoScreen2)));
            ListView.ItemsSource = items;
            //listView.ItemsSource = items;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用WhaleeEstimate.模型;
使用WhaleEstimate.Views.DetailViews;
使用Xamarin.Forms;
使用Xamarin.Forms.Xaml;
命名空间WhaleEstimate.Views.Menu
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类母版页:ContentPage
{
公共ListView ListView{get{return ListView;}}
公共清单项目;
公共母版页()
{
初始化组件();
SetItems();
}
私有void SetItems()
{
项目=新列表();
添加(新的主菜单项(“InfoScreen1”、“maus.jpg”、颜色.白色、字体(InfoScreen1)));
添加(新主菜单项(“InfoScreen2”、“maus.jpg”、颜色.白色、字体(InfoScreen2));
ListView.ItemsSource=项目;
//listView.ItemsSource=项目;
}
}
}

我必须返回ListView,它是在xaml中定义的,而不是在代码隐藏文件中定义的。因此,线索是将行
public ListView ListView{get{return ListView;}}}
更改为
public ListView ListView{get{return ListView;}}}

为什么添加了:public ListView ListView{get{return ListView;}。变量名应该与listView不同。崩溃后你检查过“输出”框了吗?它显示了错误我没有检查输出框,我在哪里找到它?还是说控制台输出?谢谢@bhavya joshi,我发现了问题!我必须返回ListView,它是在xaml中定义的,而不是在代码隐藏文件中定义的。因此,线索是将行更改为“public ListView ListView{get{return ListView;}}}”。这不是一个好做法。我建议您为MasterPage@DisDes如果问题已解决,请提供以下解决方案并将其标记为答案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WhaleEstimate.Models;
using WhaleEstimate.Views.DetailViews;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace WhaleEstimate.Views.Menu
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MasterPage : ContentPage
    {
        public ListView ListView { get { return ListView; } }
        public List<MasterMenuItem> items;

        public MasterPage ()
        {
            InitializeComponent ();
            SetItems();
        }

        private void SetItems()
        {
            items = new List<MasterMenuItem>();
            items.Add(new MasterMenuItem("InfoScreen1", "maus.jpg", Color.White, typeof(InfoScreen1)));
            items.Add(new MasterMenuItem("InfoScreen2", "maus.jpg", Color.White, typeof(InfoScreen2)));
            ListView.ItemsSource = items;
            //listView.ItemsSource = items;
        }
    }
}