Xamarin 应用程序在实现 MasterDetailPage 后无任何异常崩溃
Posted
技术标签:
【中文标题】Xamarin 应用程序在实现 MasterDetailPage 后无任何异常崩溃【英文标题】:Xamarin Application crashes without any exception after implementing MasterDetailPage 【发布时间】:2020-07-30 21:14:48 【问题描述】:在我实现 MasterDeutilPage 后,我的 Xamarin-Application 崩溃无一例外。我坚持使用 Xamarin-Tutorial (https://www.youtube.com/watch?v=K2be1RfDYK4),并将我的解决方案与 Microsoft-Docs for MasterDetailPage 进行了比较。 总的来说,我已经实现了文档,唯一的区别是文件位置和我设置 MasterPage 的 ItemsSource 的方式。我听说过这个问题,没有设置 MasterPage 的标题可能会导致我遇到同样的问题,但我确实指定了 Title-Property。
这是我的解决方案的文件系统的摘录:
这是我的代码:
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>
MasterDetail.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WhaleEstimate.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace WhaleEstimate.Views.Menu
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MasterDetail : MasterDetailPage
public MasterDetail ()
InitializeComponent();
masterpage.ListView.ItemSelected += OnItemSelected;
private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
var item = e.SelectedItem as MasterMenuItem;
if (item != null)
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
masterpage.ListView.SelectedItem = null;
IsPresented = false;
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;
【问题讨论】:
为什么添加:public ListView ListView get return ListView; 。变量名称应该与 listView 不同。并且您在崩溃后检查了“输出”框。它显示了错误 我没有检查输出框,我在哪里找到它?还是您的意思是控制台输出? 谢谢@bhavya joshi,我找到了问题!我必须返回 ListView,它是在 xaml 中定义的,而不是在代码隐藏文件中。所以线索是将行改为`public ListView ListView get return listView; ` . 这不是一个好习惯。我建议您为 MasterPage 创建 ViewModel @DisDes 如果问题已解决,请提供以下解决方案并将其标记为答案。 【参考方案1】:我必须返回在 xaml 而不是在代码隐藏文件中定义的 ListView。所以线索是把public ListView ListView get return ListView;
换成public ListView ListView get return listView;
【讨论】:
以上是关于Xamarin 应用程序在实现 MasterDetailPage 后无任何异常崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Facebook Audience Network 的 Xamarin.Forms 实现