Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin表单如何在listView(RESTAPI)上显示数据_C# - Fatal编程技术网

C# Xamarin表单如何在listView(RESTAPI)上显示数据

C# Xamarin表单如何在listView(RESTAPI)上显示数据,c#,C#,我是VisualStudio中xamarin跨平台开发的新手。 我想在listView上显示项目列表 <?xml version="1.0" encoding="utf-8" ?> x:Class=“Envelope\u Internal.mainsign”> 名称空间信封\u内部 { [XamlCompilation(XamlCompilationOptions.Compile)] 公共部分类mainsign:ContentPage { 数据服务数据服务; 列表分配列表

我是VisualStudio中xamarin跨平台开发的新手。 我想在listView上显示项目列表

<?xml version="1.0" encoding="utf-8" ?>

x:Class=“Envelope\u Internal.mainsign”>

名称空间信封\u内部
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类mainsign:ContentPage
{
数据服务数据服务;
列表分配列表;
公共mainsign()
{
初始化组件();
dataService=新的dataService();
刷新数据();
}
异步无效刷新数据()
{
assignmentList=await dataService.GetAssignmentItemsAsync();
assignmentList.item;
}
}
}
名称空间信封
{
公共类数据服务
{
HttpClient=新的HttpClient();
公共数据服务()
{
}
公共异步任务GetAssignmentItemsAsync()
{
尝试
{
var response=wait client.GetStringAsync(“https://munipoiapp.herokuapp.com/api/applications/New");
var assignmentItems=JsonConvert.DeserializeObject(响应);
归还转让物品;
}
捕获(System.Exception异常)
{
返回null;
}
}
}
}

列表中缺少DataTemplate部分,使用绑定时出错。 通过使用绑定,可以将ViewModel的示例与BindingContext关联起来

   <ListView
            HasUnevenRows="True"
            ItemsSource="{Binding ItemsSource}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Name}"/>
                    </ViewCell>
                 </DataTemplate>
            </ListView.ItemTemplate>
    </ListView>



   public partial class MainAssign : ContentPage
        {

            public MainAssign()
            {
                InitializeComponent();
                BindingContext = new MainAssignViewModel();
            }


            protected override async void OnAppearing() {
                base.OnAppearing();
                await (BindingContext as MainAssignViewModel)?.LoadData();
            }

        }

        public class MainAssignViewModel:INotifyPropertyChanged {

            List<AssignListModel> _assignmentList;

            public MainAssignViewModel() {
                _dataService = new DataService();
            }
            public List<AssignListModel> AssignmentList {
                get { return _assignmentList; }
                set {
                    _assignmentList = value;
                    OnPropertyChanged(nameof(AssignmentList));
                }
            }

            readonly DataService _dataService;

            public async Task LoadData() {
                AssignmentList = await _dataService.GetAssignmentItemsAsync();
            }

            public event PropertyChangedEventHandler PropertyChanged;

            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

公共部分类mainsign:ContentPage
{
公共mainsign()
{
初始化组件();
BindingContext=新的MainAssignViewModel();
}
受保护的重写异步void OnAppearing(){
base.OnAppearing();
等待(BindingContext作为MainAssignViewModel)?.LoadData();
}
}
公共类MainAssignViewModel:INotifyPropertyChanged{
列表\ u分配列表;
公共MainAssignViewModel(){
_dataService=新的dataService();
}
公共列表分配列表{
获取{return\u assignmentList;}
设置{
_赋值列表=值;
OnPropertyChanged(名称(转让列表));
}
}
只读数据服务\u数据服务;
公共异步任务LoadData(){
AssignmentList=await_dataService.GetAssignmentItemsAsync();
}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null){
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}

有关在列表视图中显示数据的信息,请参阅此链接:

您是否在视图模型中尝试过此方法?我不确定它是否能正常工作。您也可以尝试在加载事件中使用异步“indigentApplicationDetails”:{“householdDetail”:[{“currentApplicationRefNo”:“1000048609”,“personDetail”:{“gender”:“Male”在构造函数中添加
DataService
,“姓氏”:“Motlhamme”,}我想显示currentApplicationRefNo和个人详细信息将Text=“{Binding Name}”替换为Text=“{Binding currentApplicationRefNo.householdDetail.currentApplicationRefNo}”在ViewCellHey中,我想我会给你一个感谢,感谢你提供了如此丰富的信息。以及清晰简洁的说明!很棒的工作
namespace Envelope_Internal
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainAssign : ContentPage
    {
        DataService dataService;
        List<AssignListV> assignmentList;
        public MainAssign()
        {
            InitializeComponent();
            dataService = new DataService();
            RefreshData();
        }



        async void RefreshData()
        {
            assignmentList = await dataService.GetAssignmentItemsAsync();
            assignmentList.item;
        }

    }
}

namespace Envelope_Internal
{
    public class DataService
    {
      HttpClient client = new HttpClient();
        public DataService()
        {
        }

        public async Task<List<AssignListV>> GetAssignmentItemsAsync()
        {

            try
            {
                var response = await client.GetStringAsync("https://munipoiapp.herokuapp.com/api/applications/New");
                var assignmentItems = JsonConvert.DeserializeObject<List<AssignListV>>(response);
                return assignmentItems;
            }
            catch (System.Exception exception)
            {
                return null;
            }
        }

    }
}
   <ListView
            HasUnevenRows="True"
            ItemsSource="{Binding ItemsSource}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Name}"/>
                    </ViewCell>
                 </DataTemplate>
            </ListView.ItemTemplate>
    </ListView>



   public partial class MainAssign : ContentPage
        {

            public MainAssign()
            {
                InitializeComponent();
                BindingContext = new MainAssignViewModel();
            }


            protected override async void OnAppearing() {
                base.OnAppearing();
                await (BindingContext as MainAssignViewModel)?.LoadData();
            }

        }

        public class MainAssignViewModel:INotifyPropertyChanged {

            List<AssignListModel> _assignmentList;

            public MainAssignViewModel() {
                _dataService = new DataService();
            }
            public List<AssignListModel> AssignmentList {
                get { return _assignmentList; }
                set {
                    _assignmentList = value;
                    OnPropertyChanged(nameof(AssignmentList));
                }
            }

            readonly DataService _dataService;

            public async Task LoadData() {
                AssignmentList = await _dataService.GetAssignmentItemsAsync();
            }

            public event PropertyChangedEventHandler PropertyChanged;

            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }