从 SQLite 保存和读取 Picker 值 - Xamarin.Forms [英] Saving and reading Picker values from SQLite - Xamarin.Forms

查看:31
本文介绍了从 SQLite 保存和读取 Picker 值 - Xamarin.Forms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你帮我解决一个我无法处理的问题.

在我的 Xamarin.Forms 应用程序中,用户可以添加到 SQLite 数据库:

公共类汽车{[主键,自动递增]公共 int ID { 获取;设置;}公共字符串文本 { 获取;设置;}公共 int PickerValue { 获取;设置;}}

PickerValue 是 INT 类型,因为我认为选择器的选定值给出了索引 INT 值.我错了吗?

用户添加项目使用:

在另一个页面上,使用 ListView 显示项目:

当我在 ListView 上选择一个项目时 - 编辑页面为我打开 - 我在同一页面中添加了新产品(上面的代码),并填充了数据库中的字段.我可以在那里编辑它们.

我希望能够将选定的 Picker 值保存到 SQLite,并在另一个页面上显示它(或选定值的索引).有人可以帮助我如何进行这样的绑定吗?

如果可能的话 - 我想用 XAML 来做.

我的项目基于:您可以直接设置此选择器.

首先,我在模型中添加一个属性,这里添加一个 Gender 属性.

 公共类注意{[主键,自动递增]公共 int ID { 获取;设置;}公共字符串文本 { 获取;设置;}公共日期时间日期{获取;设置;}公共字符串性别{获取;设置;}}

在将数据插入数据库之前,您应该像此代码一样设置此属性.您应该将 myPicker.SelectedItem; 转换为 string

 async void OnSaveButtonClicked(object sender, EventArgs e){var note = (Note)BindingContext;note.Date = DateTime.UtcNow;note.Gender = (string)myPicker.SelectedItem;等待 App.Database.SaveNoteAsync(note);等待 Navigation.PopAsync();}

在另一个页面中,我们只是绑定了 Gender 属性,就像这段代码.

 <ListView.ItemTemplate><数据模板><ViewCell><堆栈布局><Label Text="{Binding Text}"></Label><Label Text="{Binding Gender}"></Label></StackLayout></ViewCell></数据模板></ListView.ItemTemplate>

这是我的演示.

我只是添加了picker的SelectedItem,

 <Picker.ItemsSource><x:Array Type="{x:Type x:String}"><x:String>女性</x:String><x:String>男</x:String></x:数组></Picker.ItemsSource></选择器>

我再次更新我的项目.https://github.com/851265601/Datademo3/blob/master/Datademo2/Notes/App.xaml.cs

I would like you to help me solve a problem that I can not deal with.

In my Xamarin.Forms application, the user can add to the SQLite database:

public class Car
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string Text { get; set; }
        public int PickerValue { get; set; }
    }

PickerValue is of type INT because I think that the selected value of the picker gives the index INT value. Am I wrong?

The user adds item using:

<Editor Placeholder="Enter name" Text="{Binding Text}" />
<Picker x:Name="myPicker" Title="Value:">
            <Picker.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>value 1</x:String>
                    <x:String>value 2</x:String>
                </x:Array>
            </Picker.ItemsSource>
        </Picker>

On another page, items are displayed using ListView:

<Label Text="{Binding Text}"/>
<Label Text="{Binding PickerValue}"/>

When I select an item on the ListView - the edit page opens for me - the same page in which I added new products (code above) with filled fields from the database. I can edit them there.

I would like to be able to save the selected Picker value to SQLite, and on another page I can display it (or index of the selected value). Can someone help me how to make such a binding?

If it is possible - I would like to do it in XAML.

I made the project based on: https://docs.microsoft.com/pl-pl/xamarin/get-started/quickstarts/database?pivots=windows

解决方案

Here is my running GIF.

You can directly set this picker.

Firstly, I add a property in the model, here is add a Gender property.

   public class Note
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string Text { get; set; }
    public DateTime Date { get; set; }

    public  string Gender { get; set; }
}

Before you insert the data to your database, you should set this property like this code.You should convert the myPicker.SelectedItem; to string

 async void OnSaveButtonClicked(object sender, EventArgs e)
    {
        var note = (Note)BindingContext;
        note.Date = DateTime.UtcNow;
        note.Gender = (string)myPicker.SelectedItem;
        await App.Database.SaveNoteAsync(note);
        await Navigation.PopAsync();
    }

In the other page, we just binding the Gender property, like this code.

  <ListView x:Name="listView"
          Margin="20"
          ItemSelected="OnListViewItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
             <StackLayout>
                <Label Text="{Binding Text}"></Label>

                <Label Text="{Binding Gender}"></Label>
            </StackLayout>

            </ViewCell>
          </DataTemplate>
    </ListView.ItemTemplate>

Here is my demo.

https://github.com/851265601/databaseDemo

Update There is GIF about change the text of the edit page, it works normally.

I just add the SelectedItem of picker,

   <Picker x:Name="myPicker" Title="Value:" SelectedItem="{Binding Gender}">
        <Picker.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>female</x:String>
                <x:String>male</x:String>
            </x:Array>
        </Picker.ItemsSource>
    </Picker>

I update again for my project. https://github.com/851265601/Datademo3/blob/master/Datademo2/Notes/App.xaml.cs

这篇关于从 SQLite 保存和读取 Picker 值 - Xamarin.Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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