首页 > 解决方案 > 如何在 Xamarin 的 listviewmodel 中保存项目,以便当我退出应用程序并再次打开它时,它会打开用户输入的项目

问题描述

假设我有一个名为的视图模型类:ToDoListViewModel

看起来像这样:

public class ToDoListViewModel
{
    public ObservableCollection<ToDoItem> ToDoItems
    {
        get; set;
    }

    public ToDoListViewModel()
    {
        ToDoItems =  new ObservableCollection<ToDoItem>();

        ToDoItems.Add(new ToDoItem("to do 1", false));
    }

    public ICommand AddCommand => new Command(AddToDoItem);

    public string InputValue { get; set; }
    void AddToDoItem()
    {
        ToDoItems.Add(new ToDoItem(InputValue , false));
    }

    public ICommand RemoveCommand => new Command(RemoveItem);
    void RemoveItem(object o)
    {
        ToDoItem removeditem = o as ToDoItem;
        ToDoItems.Remove(removeditem);
    }
}

和实际项目:

public class ToDoItem
{
    public string ToDoText { get; set; }
    public bool Complete { get; set; }

    public ToDoItem(string ToDoText, bool Complete)
    {
        this.ToDoText = ToDoText;
        this.Complete = Complete;
    }
}
   

我知道有几种方法可以通过属性或偏好来做到这一点,但如果我知道如何做到这一点,我将不胜感激,因为我是初学者程序员

标签: xamarinxamarin.androidxamarin.ios

解决方案


如何在 Xamarin 的 listviewmodel 中保存项目,以便当我退出应用程序并再次打开它时,它会打开用户输入的项目

在 Jason 看来,你可以使用 Sqlite 数据库来存储数据,并从 Sqlite 数据中获取数据并显示到 ListView 中。

我创建了一个您可以查看的示例:

首先,通过Manage Nuget Packgae安装sqlite-net-pcl

然后创建一个模型来创建 Sqlite 表:

public class ToDoItem
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string ToDoText { get; set; }
    public bool Complete { get; set; }     
}

添加和显示 sqlite 数据 UI:

 <StackLayout>

        <StackLayout>
            <Label Text="to do item:" />
            <Entry Text="{Binding InputValue}" />
            <Button
                x:Name="btn1"
                Command="{Binding addcommand}"
                Text="add to do item" />
            <Button
                x:Name="btn2"
                Command="{Binding deletecommand}"
                Text="delete to do item" />
        </StackLayout>
        <ListView ItemsSource="{Binding items}" SelectedItem="{Binding selecteditem}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding ToDoText}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

将用户输入文本添加到 sqlite 数据库中,或删除 ListView selecteditem 以删除。

public class itemviewmodel:ViewModelBase
{
    public SQLiteConnection conn;
    private List<ToDoItem> _items;
    public List<ToDoItem> items
    {
        get { return _items; }
        set
        {
            _items = value;
            RaisePropertyChanged("items");
        }
    }
    private ToDoItem _selecteditem;
    public ToDoItem selecteditem
    {
        get { return _selecteditem; }
        set
        {
            _selecteditem = value;
            RaisePropertyChanged("selecteditem");
        }
    }
    private string _InputValue;
    public string InputValue
    {
        get { return _InputValue; }
        set
        {
            _InputValue = value;
            RaisePropertyChanged("InputValue");
        }

    }
    public ICommand addcommand { get; }

    public ICommand deletecommand { get; }
    public itemviewmodel()
    {
        conn = GetSQLiteConnection();
        conn.CreateTable<ToDoItem>();

      

        addcommand = new Command(()=> { 
        if(!string.IsNullOrEmpty(InputValue))
            {
                ToDoItem item = new ToDoItem();
                item.ToDoText = InputValue;
                conn.Insert(item);
                getdata();
            }
        });

        deletecommand = new Command(()=> { 
        if(selecteditem!=null)
            {
                conn.Delete(selecteditem);
                getdata();
            }
        });
        getdata();
    }
    private void getdata()
    {
        items = items = conn.Table<ToDoItem>().ToList();
    }
    public SQLiteConnection GetSQLiteConnection()
    {
        var fileName = "todoitem.db";
        var documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var path = Path.Combine(documentPath, fileName);

        var connection = new SQLiteConnection(path);
        return connection;
    }

}

ViewModelBase 是实现 INotifyPropertyChanged 的​​类,用于通知数据更改。

public class ViewModelBase : INotifyPropertyChanged
{      
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

将 ViewModel 绑定到当前 contentPage。

public Page12()
    {
        InitializeComponent();
        this.BindingContext = new itemviewmodel();
       
    }

推荐阅读