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

标签 c# xamarin xamarin.forms constructor mobile-development

我不知道如何解决这个错误。它在 MainPage.xaml 文件中显示“缺少默认构造函数”。非常感谢您的帮助!

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 :是显示任务列表的代码隐藏文件

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:是包含弹出窗口的代码查找文件,用于选择特定任务队列,例如“未完成任务、已完成任务、过期任务等”。并将数据传递给 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 需要在 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");
    }

关于c# - Xamarin Forms 中缺少默认构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60839032/

相关文章:

xamarin - Xamarin 标签中可单击的部分文本

c# - Prism VM 与页面代码中的 View 绑定(bind)

C# Xamarin Forms 从 ViewModel 填充 CollectionView 始终为 null

c# - Azure SQL - 单个或多个数据库连接?

C# 从 Windows Azure 存储下载返回空文件,没有任何异常

c# - 如何覆盖 Xamarin.IOS UIButton 的 LayoutSublayersOfLayer 行为?

c# - 从注册表中读取字符串值会产生一些垃圾字符

c# - Xamarin 表单级联样式表 (CSS) 不工作。加载样式文件失败

xamarin.forms - Prism Forms 中的 MasterDetail 和 NavigationPage 深度链接

c# - Xamarin - 现在已过时的 ParentView 的替代方案