Saving and reading Picker values from SQLite - Xamarin.Forms

谁说我不能喝 提交于 2021-01-29 11:09:20

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/56989126/saving-and-reading-picker-values-from-sqlite-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!