C# Xamarin-如何将RESTAPI中的数据绑定到StackLayout而不是ListView

C# Xamarin-如何将RESTAPI中的数据绑定到StackLayout而不是ListView,c#,visual-studio,xamarin,xamarin.forms,data-binding,C#,Visual Studio,Xamarin,Xamarin.forms,Data Binding,我试图使用从RESTAPI获取的数据将数据绑定到StackLayout中的标签中,但它没有显示任何数据 我能够获取数据,但我想知道如何使用BindingContext绑定数据?我不想将数据绑定到ListView,而是在StackLayout中 ViewTask.xaml: ViewTask.xaml.cs private int TaskId; 公共HttpResponseMessage响应; 私有可观察收集任务; 公共视图任务(int tId) { TaskId=tId; FetchTas

我试图使用从RESTAPI获取的数据将数据绑定到StackLayout中的标签中,但它没有显示任何数据

我能够获取数据,但我想知道如何使用BindingContext绑定数据?我不想将数据绑定到ListView,而是在StackLayout中

ViewTask.xaml:


ViewTask.xaml.cs

private int TaskId;
公共HttpResponseMessage响应;
私有可观察收集任务;
公共视图任务(int tId)
{
TaskId=tId;
FetchTaskView();
初始化组件();
}
异步void FetchTaskView()
{
Title=“Task#::”+TaskId;
if(Connectivity.NetworkAccess==NetworkAccess.Internet)
{
var apiResponse=RestService.For(UserSettings.Url);
response=等待apiResponse.GetTasks();
if(响应。IsSuccessStatusCode)
{
var taskContent=JsonConvert.DeserializeObject(wait response.Content.ReadAsStringAsync());
_任务=新的ObservableCollection(任务内容);
BindingContext=_task.Where(t=>t.Id==TaskId.ToList();
}
}
其他的
{
UserDialogs.Instance.Toast(“没有internet连接。无法获取任务数据。”,TimeSpan.FromSeconds(5));
}
}

您的任务类应该包含带有getter和setter的属性

public class Task
{
   public string Title { get; set; }
   public string Creater { get; set; }
   .....
}
然后使用

BindingContext = _task.Where(t => t.Id == TaskId).SingleOrDefault();

下面是如何将列表绑定到stacklayout的代码

如果需要BindableLayout-
public class Task
{
   public string Title { get; set; }
   public string Creater { get; set; }
   .....
}
BindingContext = _task.Where(t => t.Id == TaskId).SingleOrDefault();
  <StackLayout BindableLayout.ItemsSource="{Binding YourList}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Your PropertyName}"/>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>