如何解决如何在 Xamarin 的 listviewmodel 中保存项目,以便当我退出应用程序并再次打开它时,它会打开用户输入的项目
假设我有一个视图模型类:
AudioPlaylist.java:123: error: cannot find symbol
VBoxBuilder.create().children(
symbol: variable VBoxBuilder
location: class AudioPlaylist
AudioPlaylist.java:132: error: cannot find symbol
HBoxBuilder.create().spacing(10).alignment(Pos.CENTER).children(next,play,stop,prog,mediaView).build()
^
symbol: variable HBoxBuilder
location: class AudioPlaylist
5 errors
看起来像这样:
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);
}
}
我知道有几种方法可以通过属性或偏好来做到这一点,但如果我知道如何做到这一点,我将不胜感激,因为我是一名初级程序员
解决方法
如何将项目保存在 Xamarin 的列表视图模型中,以便当我退出应用程序并再次打开它时,它会以用户输入的项目打开
Jason 认为可以使用Sqlite 数据库来存储数据,并从Sqlite 数据中获取数据并显示到ListView 中。
我创建了一个您可以查看的示例:
首先,通过Manage Nuget Packgae安装sqlite-net-pcl。
然后创建一个模型来创建Sqlite Table:
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();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。