如何绑定来自SQLite的完整响应? [英] how can I bind a full response from SQLite?

查看:80
本文介绍了如何绑定来自SQLite的完整响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#文件中制作一个ListView.但是与其相反,我还想添加从sqlite获得的数据以及具有数据绑定的xaml文件,因此我仍然可以使用xaml编辑布局.因此,来自sqlite的每个响应都需要添加为label(<TextCell Text="{Binding Name}" />).

I am making a ListView inside my C# file. But instead of that I want to add the data I get from sqlite too the xaml file with data binding so I can still edit the layout with xaml. So every response from sqlite needs to be added as label(<TextCell Text="{Binding Name}" />).

我的问题:如何将GetCategoryByMenuID的响应绑定到TextCell Text="{Binding Name}"?

My question: How can I bind the response from GetCategoryByMenuID to TextCell Text="{Binding Name}"?

xaml页面(CategoriePage.xaml):

xaml page (CategoriePage.xaml):

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AmsterdamTheMapV3.CategoriePage">
  <ListView x:Name="listView">
    <ListView.ItemTemplate>
      <DataTemplate>
        <TextCell Text="{Binding Name}" />
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</ContentPage>

后端/C#(CategoriePage.xaml.cs):

Back-end / C# (CategoriePage.xaml.cs) :

namespace AmsterdamTheMapV3
{
    public partial class CategoriePage : ContentPage
    {

        public CategoriePage(String txt)
        {
            InitializeComponent();
            var layout = new StackLayout { Padding = new Thickness(5, 10) };
            int page = Int32.Parse(txt);
            this.Content = layout;

            var categories = App.Database.GetCategoryByMenuID(page);

            var datatemplate = new DataTemplate(() =>
            {
                var nameLabel = new Label();
                nameLabel.SetBinding(Label.TextProperty, "Name");
                //nameLabel.SetBinding(Label.idProperty, "Name");

                return new ViewCell { View = nameLabel };
            });

            var listView = new ListView
            {
                ItemsSource = categories,
                ItemTemplate = datatemplate
            };

            layout.Children.Add(listView);
        }
    }
}

GetCategoryPage函数:

GetCategoryPage function:

public List<Categories> GetCategoryByMenuID(int menuID)
{
    lock (locker)
    {
        return db.Table<Categories>().Where(x => x.Menu_ID == menuID).ToList();
    }
}

推荐答案

给出

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AmsterdamTheMapV3.CategoriePage">
  <ListView x:Name="listView">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <ViewCell.View>
            <StackLayout>
              <Label Text="{Binding Name}" />
            </StackLayout>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</ContentPage>

更新后面的代码

namespace AmsterdamTheMapV3 {
    public partial class CategoriePage : ContentPage {

        public CategoriePage(string txt) {
            InitializeComponent();
            this.Appearing += (s,e) => {
                if(NotInDesignMode) {
                    int page = 0;
                    if(int.TryParse(txt, out page)){
                        var categories = App.Database.GetCategoryByMenuID(page);
                        this.listView.ItemsSource = categories;
                    }
                }
            };
        }

        bool NotInDesignMode {
            get { return Application.Current != null; }
        }
    }
}

这篇关于如何绑定来自SQLite的完整响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆