c# - 缺少默认构造函数
问题描述
所以,一切都很完美,当突然一个异常阻止我运行我的程序时,我可以看到我的代码的漂亮显示。我肯定做错了什么,但我不知道在哪里或做什么,因为我正在处理两个内容页面,一个工作正常,另一个内容页面几乎相同,除了显示更详细的信息。
错误:
错误 XFC0004 缺少“AoFStructures.StructureDepth”的默认构造函数`
我允许导航的主页:
<TabbedPage 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"
xmlns:local="clr-namespace:AoFStructures"
mc:Ignorable="d"
x:Class="AoFStructures.MainPage">
<NavigationPage Title="Structures">
<x:Arguments>
<local:StructureOverview/>
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Structure info">
<x:Arguments>
<local:StructureDepth/>
</x:Arguments>
</NavigationPage>
</TabbedPage>
我的结构深度.xaml.cs
namespace AoFStructures
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StructureDepth : ContentPage
{
Structure _selectedStructure;
StructureInfo _structured = null;
public StructureInfo ChosenStructure
{
get { return _structured; }
set { _structured = value; OnPropertyChanged("MyStructureName"); }
}
public StructureDepth(Structure structure)
{
BindingContext = this;
InitializeComponent();
_selectedStructure = structure;
}
private void ContentPage_Appearing(object sender, EventArgs e)
{
}
}
}
结构深度.xaml
<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"
xmlns:local="clr-namespace:AoFStructures"
Appearing="ContentPage_Appearing"
x:Class="AoFStructures.StructureDepth">
<ContentPage.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackLayout>
<Label Grid.Column="0" Text="Name:"/>
<Label Grid.Column="1" Text="{Binding ChosenStructure.Name}"/>
<Label Grid.Column="0" Text="Age:"/>
<Label Grid.Column="1" Text="{Binding ChosenStructure.Age}"/>
<Label Grid.Column="0" Text="Cost:"/>
<Label Grid.Column="1" Text="{Binding ChosenStructure.Cost}"/>
<Label Grid.Column="0" Text="Build time:"/>
<Label Grid.Column="1" Text="{Binding ChosenStructure.Build_item}"/>
</StackLayout>
</Grid>
</ContentPage.Content>
</ContentPage>
解决方案
错误是不言自明的,您的类必须有一个默认构造函数(无参数构造函数),还要注意InitializeComponent
and的顺序BindingContext
:
public StructureDepth()
{
InitializeComponent();
BindingContext = this;
}
public StructureDepth(Structure structure) : this()
{
_selectedStructure = structure;
}
这样,无论何时调用StructureDepth(Structure structure)
它都会首先调用默认构造函数,然后执行它 bloc (在这种情况下为赋值),这可以避免代码重复。
推荐阅读
- php - Laravel 的困惑点与实体关系的实现
- python-3.x - 使用 Beautiful Soup 和 Python 从 wiki 抓取表格数据
- html - Xojo 计算 HTML 中的字数 - 删除标签和其他可能影响计数的不需要的内容?
- csvhelper - 有条件地忽略参考映射
- python - 如何根据熊猫中的字典映射包含多个字符串的列
- python - 我似乎无法使用 Python/Selenium 找到输入框元素
- python - 键入注释稍后定义的类(前向参考)
- r - 如何将没有 am 或 pm 的所有数字替换为标准字符串?
- javascript - 是否可以在 express 中将渲染函数放入 for 循环中?
- mongodb - Mongo嵌套数组字段选择