Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

c# - 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. I would gratefully appreciate the help!

MainPage.xaml : contains the navigation menus.

<?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

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." and it will pass the data to 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);
        }
    }
}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

TaskList needs a default (no parameters) constructor to be used in 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");
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...