【问题标题】:Assign Web Service method to ItemSource of ListView in Xamarin Forms将 Web Service 方法分配给 Xamarin Forms 中 ListView 的 ItemSource
【发布时间】:2016-07-19 06:58:30
【问题描述】:

我需要在我的应用程序中使用 Azure 移动服务,并在我的项目中添加 Azure 移动客户端 SDK。为了在我的项目中实现 Web 服务,我像这样设置了我的 AzureService 类-

    public partial class AzureService 
    {
        static AzureService defaultInstance = new AzureService ();

        MobileServiceClient client;

        IMobileServiceTable<modelLifeStyleDiets> modelLifeStyleDietsTable;

        public AzureService ()
        {
            this.client = new MobileServiceClient(url);

            this.modelLifeStyleDietsTable = client.GetTable<modelLifeStyleDiets>();

        }

        public static AzureService DefaultManager{      
            get{
                return defaultInstance;
                }
            private set{ 
                defaultInstance = value;
            }
        }

        public MobileServiceClient CurrentClient{
            get{ return client;}
        }

        public bool IsOfflineEnabled{
            get{ return modelLifeStyleDietsTable is Microsoft.WindowsAzure.MobileServices.Sync.IMobileServiceSyncTable<modelLifeStyleDiets>; }
        }


        public async Task<ObservableCollection<modelLifeStyleDiets>> GetTodoItemsAsync (bool syncItems = false)
        {
            try {

                IEnumerable<modelLifeStyleDiets> items = await modelLifeStyleDietsTable
                    .Where(Item => !(Item.done)).ToEnumerableAsync ();

                return new ObservableCollection<modelLifeStyleDiets> (items);
            } catch (MobileServiceInvalidOperationException msioe) {
                Debug.WriteLine (@"Invalid sync operation: {0}", msioe.Message);
            } catch (Exception e) {
                Debug.WriteLine (@"Sync error: {0}", e.Message);
            }
            return null;
        }


        public async Task SaveTaskAsync (modelLifeStyleDiets item)
        {
            if (item.id == null) {
                await modelLifeStyleDietsTable.InsertAsync (item);
            } else {
                await modelLifeStyleDietsTable.UpdateAsync (item);
            }
        }
    }

我的服务模型如下-

    public class modelLifeStyleDiets
    {
        public string id { get; set; }
        public string itemName { get; set; }
        public bool done{ get; set;}
    }

我有 ListView,我需要从服务中获取数据并将其分配给 ListView 的 ItemSource。 我试过这个 - listof_lifestyleDiets.ItemsSource=AzureService.DefaultManager.GetTodoItemsAsync();

但在上面的代码中,我收到了缺少类型转换的错误。如何解决?

【问题讨论】:

    标签: c# listview azure xamarin


    【解决方案1】:

    您需要等待异步任务。

    listof_lifestyleDiets.ItemsSource = await AzureService.DefaultManager.GetTodoItemsAsync();
    

    您还需要确保您绑定的是要显示的属性。

    <ListView.ItemTemplate>
      <DataTemplate>
        <TextCell Text="{Binding itemName}" />
      </DataTemplate>
    </ListView.ItemTemplate>
    

    或者如果您正在以编程方式构建用户界面

    nameLabel.SetBinding(Label.TextProperty, "itemName");
    

    【讨论】:

    • 感谢您的回复先生。但我的 ListView 就像 - ListView listof_lifestyleDiets=new ListView { RowHeight=30, IsVisible=false, HeightRequest=150, };
    • 我真的不知道,这种情况下如何绑定itemName。
    • 你有ItemTemplate和标签吗?
    • 好的。让我自定义我的 ListView。
    • 现在我的 ListView 就像 - ListView ListView listof_lifestyleDiets=new ListView { RowHeight=30, IsVisible=false, HeightRequest=150, ItemTemplate=new DataTemplate (typeof(lifestyleDietsCell)), ItemsSource=AzureService .DefaultManager.GetTodoItemsAsync(), }; 它的模板就像 -
    猜你喜欢
    • 2019-12-29
    • 2021-12-02
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2018-01-21
    • 2018-03-17
    相关资源
    最近更新 更多