在下面的代码IM结合记录形成视图模型,更新现在我有它使用navigation.pushasync方法去的按钮设置到一个新的页面,在那里更新数据库记录,我已经在这个新的页面创建了一个列表视图,并使用这里显示的绑定记录相同的绑定,每当我添加它更新的东西,但如果我按下后退按钮并来回到这个EntryTabPage绑定不更新,它只会更新如果我完全关闭应用程序,并重新启动它的数据显示。的ListView不被使用一个ObservableCollection,IM也提高inotifypropertychange自Binding
我想这个列表视图进行更新不知道怎么办呢,我一直在使用的Listviewname.itemssource =记录onappearing和打字审判;但是这显示错误“在当前情况下不存在”,任何帮助正确的方向将不胜感激。
注:绑定正在别处除了标签页内。 这是层次TabbedPage-> ContentPage-> StackLayout->的ListView(正是在这个列表视图,绑定没有更新。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bunk_Master.EntryPageTab1"
Title="Default">
<StackLayout x:Name="stacklayout">
<ListView ItemsSource="{Binding Records}"
x:Name="classListView">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding}">
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Text="new"
x:Name="addclassbutton">
</Button>
</StackLayout>
namespace Bunk_Master
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EntryPageTab1 : ContentPage
{
public EntryPageTab1()
{
InitializeComponent();
BindingContext = new ClassViewModel();
addclassbutton.Clicked += async (object sender, EventArgs e) =>
{
await this.Navigation.PushAsync(new AddClass());
};
}
protected override void OnAppearing()
{
base.OnAppearing();
}
}
}
这个视图模型
namespace Bunk_Master
{
public class ClassViewModel : BaseViewModel
{
readonly Database db;
public string classname { get; set; }
// public int absentnos { get; set; }
// public int presentnos { get; set; }
public ICommand AddCommand { get; set; }
public ICommand DeleteCommand { get; set; }
public ObservableCollection<string> Records { get; set; }
public ClassViewModel()
{
AddCommand = new Command(Add);
DeleteCommand = new Command(Delete);
db = new Database("classdb");
db.CreateTable<ClassModel>();
Records = new ObservableCollection<string>();
ShowAllRecords();
}
void Add()
{
var record = new ClassModel
{
classname = classname,
//absentnum = absentnos,
//presentnum = presentnos
};
db.SaveItem(record);
Records.Add(record.ToString());
RaisePropertyChanged(nameof(Records));
ClearForm();
}
private void Delete(object obj)
{
throw new NotImplementedException();
}
void ClearForm()
{
classname = string.Empty;
RaisePropertyChanged(nameof(classname));
}
void ShowAllRecords()
{
Records.Clear();
var classdb = db.GetItems<ClassModel>();
foreach (var classmodel in classdb)
{
Records.Add(classmodel.ToString());
}
}
}
}
这是我实现Inotifypropertychange
namespace Bunk_Master
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler == null) return;
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
请提供您的视图模型和代码在其中添加新项目 – Jason
Im相当肯定是工作的结合,只是它不是选项卡页面中工作。我编辑添加ViewModel – Zany
都是使用VM的相同*实例*的页面,还是添加页面创建它自己的实例? – Jason