Я новичок в Xamarin Forms и пытаюсь создать приложение для задач. В приложении будет страница со списком задач, и когда пользователь выбирает задачу, она перейдет на страницу просмотра задач. На странице просмотра задач отобразятся подробности задачи и подзадачи. Проблема в том, что я не могу понять, как отобразить связанные подзадачи для указанного идентификатора задачи c. Проблема в файле ViewTask.xaml.cs. Я подумал, что, возможно, делаю foreach l oop, где я могу видеть, есть ли у каждой подзадачи ссылка на указанный идентификатор задачи c, который работает, но я не могу понять, как отобразить эту указанную подзадачу c, если она совпадения.
Надеюсь, это имеет смысл, и я буду признателен за всю помощь!
Вот коды, которые у меня есть:
TaskList.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>
TaskList.xaml.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));
}
}
}
ViewTask.xaml
<?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>
ViewTask.xaml.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 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();
}
}
}
Task.cs
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; }
}
}
ToDo.cs
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
}
]
}