如何绑定SQLite的完整响应?

N. *_*ing 6 c# sqlite wpf xaml xamarin.forms

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

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

xaml页面(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>
Run Code Online (Sandbox Code Playgroud)

后端/ 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

GetCategoryPage函数:

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

Nko*_*osi 2

给定

<?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>
Run Code Online (Sandbox Code Playgroud)

更新后面的代码

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; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)