C# 如何显示具有特定任务Id的所有关联子任务?[沙马林表格]

C# 如何显示具有特定任务Id的所有关联子任务?[沙马林表格],c#,visual-studio,xaml,xamarin,xamarin.forms,C#,Visual Studio,Xaml,Xamarin,Xamarin.forms,我是Xamarin表单的新手,我正在尝试创建一个任务应用程序。在应用程序中,将出现任务列表页面,当用户选择任务时,它将进入任务视图页面。在任务视图页面中,它将显示任务详细信息和子任务。问题是我无法确定如何显示特定任务id的关联子任务。问题在ViewTask.xaml.cs文件中。我想也许可以执行foreach循环,在这个循环中,我可以看到每个子任务是否都有一个对特定任务id的引用,这是可行的,但我不知道如何显示匹配的特定子任务 我希望这是有意义的,我将感谢所有的帮助 以下是我目前掌握的代码: 任

我是Xamarin表单的新手,我正在尝试创建一个任务应用程序。在应用程序中,将出现任务列表页面,当用户选择任务时,它将进入任务视图页面。在任务视图页面中,它将显示任务详细信息和子任务。问题是我无法确定如何显示特定任务id的关联子任务。问题在ViewTask.xaml.cs文件中。我想也许可以执行foreach循环,在这个循环中,我可以看到每个子任务是否都有一个对特定任务id的引用,这是可行的,但我不知道如何显示匹配的特定子任务

我希望这是有意义的,我将感谢所有的帮助

以下是我目前掌握的代码:

任务列表.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage Title="Tasks" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TaskApp.Tasks.TaskList">
    <ContentPage.ToolbarItems>
        <ToolbarItem IconImageSource="plus.png" Order="Primary" Priority="1" />
        <ToolbarItem IconImageSource="ellipsis.png" Order="Primary" Priority="1" />
    </ContentPage.ToolbarItems>

    <StackLayout BackgroundColor="#f7f7f7">
        <ListView x:Name="taskList" HasUnevenRows="True" BackgroundColor="Transparent" SeparatorVisibility="None" ItemSelected="ViewTask">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="20, 10">
                            <Frame BackgroundColor="White" CornerRadius="5" HasShadow="False" Padding="0" BorderColor="LightGray" x:Name="{Binding Id}">
                                <StackLayout Orientation="Horizontal" Padding="10">
                                    <CheckBox HorizontalOptions="Start" Scale="1.3"/>
                                    <Label Text="{Binding Title}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" TextColor="#027fff" Font="Bold" />


                                    <StackLayout Orientation="Horizontal" Padding="10, 0" HorizontalOptions="End" VerticalOptions="Center">
                                        <Label Text="{Binding CompletedTasks}" />
                                        <Label Text="/" />
                                        <Label Text="{Binding TotalTasks}" />
                                    </StackLayout>
                                </StackLayout>
                            </Frame>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage Title="Task View" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TaskApp.Tasks.ViewTask">
    <ContentPage.ToolbarItems>
        <ToolbarItem IconImageSource="info.png" Order="Primary" Priority="1" />
        <ToolbarItem IconImageSource="ellipsis.png" Order="Primary" Priority="1" />
    </ContentPage.ToolbarItems>

    <StackLayout BackgroundColor="#f7f7f7" VerticalOptions="FillAndExpand">
        <TableView Intent="Form" HasUnevenRows="True">
            <TableRoot>
                <TableSection Title="Task Details">
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="13, 7">
                            <Label Text="Task ID: " VerticalOptions="Center" Font="Bold"/>
                            <Label Text="{Binding Id}" HorizontalOptions="EndAndExpand" VerticalOptions="Center" />
                        </StackLayout>
                    </ViewCell>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="13, 7">
                            <Label Text="Task Name: " VerticalOptions="Center" Font="Bold"/>
                            <Label Text="{Binding Title}" HorizontalOptions="EndAndExpand" VerticalOptions="Center" />
                        </StackLayout>
                    </ViewCell>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="13, 7">
                            <Label Text="Creator: " VerticalOptions="Center" Font="Bold"/>
                            <Label Text="{Binding Creator}" HorizontalOptions="EndAndExpand" VerticalOptions="Center" />
                        </StackLayout>
                    </ViewCell>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="13, 7">
                            <Label Text="Status: " VerticalOptions="Center" Font="Bold"/>
                            <Label Text="{Binding Status}" HorizontalOptions="EndAndExpand" VerticalOptions="Center" />
                        </StackLayout>
                    </ViewCell>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="13, 7">
                            <Label Text="Date Created: " VerticalOptions="Center" Font="Bold"/>
                            <Label Text="{Binding Timestamp}" HorizontalOptions="EndAndExpand" VerticalOptions="Center" />
                        </StackLayout>
                    </ViewCell>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="13, 7">
                            <Label Text="Ticket Reference: " VerticalOptions="Center" Font="Bold"/>
                            <Label Text="{Binding Ref}" HorizontalOptions="EndAndExpand" VerticalOptions="Center" />
                        </StackLayout>
                    </ViewCell>
                    <ViewCell>
                        <StackLayout Padding="13, 7">
                            <Label Text="Description: " VerticalOptions="Center" Font="Bold"/>
                            <Label Text="{Binding Description}" />
                        </StackLayout>
                    </ViewCell>
                    <ViewCell>
                        <StackLayout Padding="13">
                            <Label Text="{Binding TotalTasks, StringFormat='To Do List ({0:F0})'}" Font="Bold"/>

                            <ListView x:Name="toDoList" HasUnevenRows="True" BackgroundColor="Transparent" SeparatorVisibility="None">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <StackLayout Padding="0, 10">
                                                <Frame BackgroundColor="White" CornerRadius="5" HasShadow="False" Padding="0" BorderColor="LightGray" x:Name="{Binding Id}">
                                                    <StackLayout Orientation="Horizontal" Padding="10">
                                                        <CheckBox HorizontalOptions="Start" Scale="1.3" x:Name="{Binding Id}"/>
                                                        <Label Text="{Binding Title}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" TextColor="#027fff" Font="Bold" />
                                                    </StackLayout>
                                                </Frame>
                                            </StackLayout>
                                        </ViewCell>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </StackLayout>
                    </ViewCell>
                </TableSection>
            </TableRoot>
        </TableView>
    </StackLayout>
</ContentPage>

ToDo.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
using Newtonsoft.Json;
using TaskApp.Models;
using Xamarin.Forms;

namespace TaskApp.Tasks
{
    public partial class TaskList : ContentPage
    {
        private const string Url = ".../jsontaskurl";
        private HttpClient _client = new HttpClient();
        private ObservableCollection<Task> _tasks;

        public TaskList()
        {
            InitializeComponent();
        }

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

            _tasks = new ObservableCollection<Task>(tasks);

            taskList.ItemsSource = _tasks;

            base.OnAppearing();
        }

        async void ViewTask(object sender, SelectedItemChangedEventArgs e)
        {
            var t = e.SelectedItem as Task;
            await Navigation.PushAsync(new ViewTask(t));
        }
    }
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
using Newtonsoft.Json;
using TaskApp.Models;
using Xamarin.Forms;

namespace TaskApp.Tasks
{
    public partial class ViewTask : ContentPage
    {
        private const string ToDoUrl = ".../jsontodourl";
        private HttpClient _client = new HttpClient();
        private ObservableCollection<ToDo> _todo;

        private int TaskId;
        public ViewTask(Task task)
        {
            TaskId = task.Id;

            if (task == null)
                throw new ArgumentNullException();

            BindingContext = task;

            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            var toDoContent = await _client.GetStringAsync(ToDoUrl);
            var todo = JsonConvert.DeserializeObject<List<ToDo>>(toDoContent);
            _todo = new ObservableCollection<ToDo>(todo);

            toDoList.ItemsSource = _todo;

            //foreach(var td in _todo)
            //{
            //    if (td.Id == TaskId)
            //    {
            //        toDoList.ItemsSource = _todo.Contains(Id);
            //    }
            //}

            base.OnAppearing();
        }
    }
}
using System;
namespace TaskApp.Models
{
    public class Task
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Creator { get; set; }
        public string Status { get; set; }
        public string Ref { get; set; }
        public int CompletedTasks { get; set; }
        public int TotalTasks { get; set; }
        public string Timestamp { get; set; }
        public string Description { get; set; }
    }
}
using System;
namespace TaskApp.Models
{
    public class ToDo
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Status { get; set; }
        public int Ref { get; set; }
    }
}
下面是我的json数据文件的样子:

{
     "tasks": [
         {
               "id": 3830,
               "title": "Multi-home test",
               "creator": "John Doe",
               "status": "Open",
               "ref": 1848394,
               "completedTasks": 0,
               "totalTasks": 3,
               "timestamp": "9/21/2019 5:46 PM",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
         },
         {
               "id": 3831,
               "title": "Compose design doc",
               "creator": "Bob Junior",
               "status": "Closed",
               "ref": 1848394,
               "completedTasks": 2,
               "totalTasks": 5,
               "timestamp": "9/22/2019 1:15 PM",
               "description": "It is a long established fact that a reader will be distracted."
         },
         {
               "id": 3832,
               "title": "Choose event theme",
               "creator": "Bob Junior",
               "status": "Open",
               "ref": 1848395,
               "completedTasks": 1,
               "totalTasks": 4,
               "timestamp": "9/22/2019 1:15 PM",
               "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
         },
         {
               "id": 3833,
               "title": "Finalize event budget",
               "creator": "Tom Fontenot",
               "status": "Open",
               "ref": 1848394,
               "completedTasks": 1,
               "totalTasks": 1,
               "timestamp": "9/22/2019 1:15 PM",
               "description": "Praesent imperdiet, nisl vitae vehicula sagittis, ipsum tortor lobortis tortor, ac aliquam justo dui ut purus."
         },
         {
               "id": 3834,
               "title": "Book all team travel",
               "creator": "George Olsen",
               "status": "Open",
               "ref": "",
               "completedTasks": 2,
               "totalTasks": 3,
               "timestamp": "9/22/2019 1:15 PM",
               "description": "Vestibulum eros magna, interdum a dolor tristique, molestie placerat nulla. Sed molestie nunc eros."
         },
         {
               "id": 3835,
               "title": "Create hi-fidelity designs in Sketch",
               "creator": "George Olsen",
               "status": "Open",
               "ref": "",
               "completedTasks": 3,
               "totalTasks": 7,
               "timestamp": "9/22/2019 1:15 PM",
               "description": "Nam dignissim leo et risus hendrerit rhoncus. Curabitur mauris dolor, ultricies non leo non, scelerisque hendrerit nisi."
         },
         {
               "id": 3836,
               "title": "Design interior page templates",
               "creator": "Barack Obama",
               "status": "Open",
               "ref": "",
               "completedTasks": 0,
               "totalTasks": 2,
               "timestamp": "9/22/2019 1:15 PM",
               "description": "Praesent ut velit nec erat vestibulum luctus. Suspendisse lacinia malesuada blandit. Suspendisse quis interdum tortor. Donec est lacus, vulputate ut erat sed, consectetur malesuada odio. Morbi lobortis dapibus nisi, quis commodo dui tincidunt non."
         },
         {
               "id": 3837,
               "title": "Publish blog post",
               "creator": "Victoria Rickshaw",
               "status": "Open",
               "ref": "",
               "completedTasks": 1,
               "totalTasks": 5,
               "timestamp": "9/22/2019 1:15 PM",
               "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
         }
    ],

    "todo": [
         {
              "id": 58383,
              "title": "Subtask 1",
              "status": "Open",
              "ref": 3831
         },
         {
              "id": 93928,
              "title": "Subtask 2",
              "status": "Completed",
              "ref": 3831
         },
         {
              "id": 58386,
              "title": "Subtask 12",
              "status": "Open",
              "ref": 3832
         },
         {
              "id": 93929,
              "title": "Subtask 2s",
              "status": "Completed",
              "ref": 3832
         }
    ]
}

使用LINQ查找与父任务ID匹配的todo项

toDoList.ItemsSource = _todo.Where(t => t.Ref == taskID).ToList();