首页 > 解决方案 > 缺少默认构造函数

问题描述

所以,一切都很完美,当突然一个异常阻止我运行我的程序时,我可以看到我的代码的漂亮显示。我肯定做错了什么,但我不知道在哪里或做什么,因为我正在处理两个内容页面,一个工作正常,另一个内容页面几乎相同,除了显示更详细的信息。

错误:

错误 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> 

标签: c#xamarin.forms

解决方案


错误是不言自明的,您的类必须有一个默认构造函数(无参数构造函数),还要注意InitializeComponentand的顺序BindingContext

        public StructureDepth()
        {
            InitializeComponent();
            BindingContext = this;
        }

        public StructureDepth(Structure structure) : this()
        {
            _selectedStructure = structure;
        }

这样,无论何时调用StructureDepth(Structure structure)它都会首先调用默认构造函数,然后执行它 bloc (在这种情况下为赋值),这可以避免代码重复。


推荐阅读