简体   繁体   English

Xamarin Forms 中缺少默认构造函数错误

[英]Missing Default Constructor error in Xamarin Forms

I am having trouble figuring out how to solve this error.我无法弄清楚如何解决此错误。 It says "Missing Default Constructor" in the MainPage.xaml file.它在 MainPage.xaml 文件中显示“缺少默认构造函数”。 I would gratefully appreciate the help!我将不胜感激!

MainPage.xaml : contains the navigation menus. MainPage.xaml :包含导航菜单。

<?xml version="1.0" encoding="utf-8"?>
<TabbedPage
    xmlns:tasks="clr-namespace:TaskApp.Tasks"
    xmlns:notifications="clr-namespace:TaskApp.Notifications"
    xmlns:account="clr-namespace:TaskApp.Account"
    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="TaskApp.MainPage">

    <NavigationPage Title="Tasks" Icon="tasks.png">
        <x:Arguments>
            <tasks:TaskList /> // <-- THIS IS WHERE THE ERROR OCCURS
        </x:Arguments>
    </NavigationPage>

    <NavigationPage Title="Notifications" Icon="notification.png">
        <x:Arguments>
            <notifications:NotificationList />
        </x:Arguments>
    </NavigationPage>

    <NavigationPage Title="Account" Icon="account.png">
        <x:Arguments>
            <account:AccountPage />
        </x:Arguments>
    </NavigationPage>
</TabbedPage>

TaskList.xaml.cs : is the code-behind file that displays the list of tasks TaskList.xaml.cs :是显示任务列表的代码隐藏文件

namespace TaskApp.Tasks
{
    public partial class TaskList : ContentPage
    {
        public TaskList(string queue)
        {
            InitializeComponent();

            if (queue != null)
                queueSlug = queue;

            NavigationPage.SetBackButtonTitle(this, "Back");
        }

        //Overrides the back button on Android and Window devices
        protected override bool OnBackButtonPressed()
        {
            return true;
        }
    }
}

TaskQueues.xaml.cs : is the code-befind file that contains a popup to select a specific task queue such as "Uncompleted tasks, Completed tasks, Overdue tasks, etc." TaskQueues.xaml.cs :是代码查找文件,其中包含用于选择特定任务队列的弹出窗口,例如“未完成的任务、已完成的任务、过期任务等”。 and it will pass the data to TaskList.xaml.cs它会将数据传递给 TaskList.xaml.cs

namespace TaskApp.Popups
{
    public partial class TaskQueues : PopupPage
    {
        private const string Url = "...";
        private HttpClient _client = new HttpClient();
        private ObservableCollection<Queues> _queues;

        void Handle_SelectedQueue(object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
        {
            var queue = e.SelectedItem as Queues;
            PopupNavigation.Instance.PopAsync(true);
            new NavigationPage(new TaskList(queue.Slug));
        }

        public TaskQueues()
        {
            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            var content = await _client.GetStringAsync(Url);
            var queues = JsonConvert.DeserializeObject<List<Queues>>(content);

            _queues = new ObservableCollection<Queues>(queues);
            taskQueues.ItemsSource = _queues;

            // Adjusts the list height and scrollview height
            int i = _queues.Count;
            int heightRowList = 50;
            i = (i * heightRowList);
            taskQueues.HeightRequest = i;

            if (i > 400)
                taskQueuesScrollView.HeightRequest = 400;

            base.OnAppearing();
        }

        private void ClosePopup(object sender, EventArgs e)
        {
            PopupNavigation.Instance.PopAsync(true);
        }
    }
}

TaskList needs a default (no parameters) constructor to be used in XAML TaskList需要在 XAML 中使用默认(无参数)构造函数

public partial class TaskList : ContentPage
{
    public TaskList()
    {
        InitializeComponent();

        NavigationPage.SetBackButtonTitle(this, "Back");
    }

    public TaskList(string queue)
    {
        InitializeComponent();

        if (queue != null)
            queueSlug = queue;

        NavigationPage.SetBackButtonTitle(this, "Back");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 xamarin.forms mvvm 缺少默认构造函数 - xamarin forms mvvm Missing default constructor for 为什么 Xamarin Forms 应用程序的资源中的字符串会给出缺少默认构造函数错误? - Why does a String in the Resources of a Xamarin Forms application give a Missing default constructor error? Xamarin Forms -&gt; System.MissingMethodException:找不到 [Interface] 的默认构造函数 - Xamarin Forms -> System.MissingMethodException: Default constructor not found for [Interface] Xamarin Forms 缺少预览器 - Xamarin Forms Previewer is missing 空白 Xamarin.Forms 项目上缺少 netstandard 2.0 程序集错误 - Missing netstandard 2.0 assembly error on blank Xamarin.Forms project Xamarin Forms Android 构建错误 - 缺少文件,UniversalApiContract.winmd - Xamarin Forms Android Build error - Missing File, UniversalApiContract.winmd Xamarin.Forms:Android项目生成错误-AppData \\ Local \\ Xamarin文件夹内缺少文件 - Xamarin.Forms: Android project Build Error - Missing Files inside AppData\Local\Xamarin Folders 找不到Xamarin.Forms.Platform.Android.LabelRenderer(xamarin表单)的构造函数 - No constructor found for Xamarin.Forms.Platform.Android.LabelRenderer (xamarin forms) CarouselView xamarin 表单冲突 Xamarin 表单错误 - CarouselView xamarin forms conflicting Xamarin forms error Xamarin Forms BindableProperty 在构造函数之前更改 - Xamarin Forms BindableProperty Changed before constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM