2017-05-30 32 views
0

在下面的代码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)); 
     } 
    } 
} 
+0

请提供您的视图模型和代码在其中添加新项目 – Jason

+0

Im相当肯定是工作的结合,只是它不是选项卡页面中工作。我编辑添加ViewModel – Zany

+0

都是使用VM的相同*实例*的页面,还是添加页面创建它自己的实例? – Jason

回答

0

如果有人碰巧找到了一流的工作在一类,但不是在另一个类,你的代码是完美的,它只是因为你实例化的类必须使用的,而不是实例化一个新的。

刚加入这一行固定我的代码

public static Workingclass classInstance = new Workingclass; 

然后我用的classIncstance,而不是创建新实例,在其他类和数据被正确地更新。

注:我想删除这篇文章,但网络上有很多其他类似帖子,没有人解决这个简单的问题,也许有人会发现它有用。

+0

你能否详细解释一下如何使用下面的代码实现public static class classInstance = new Workingclass;我很困惑,在viewmodel或codebehind中添加这一行的位置。 – Ganesh

+0

@Ganesh你需要在你将要使用它的方法的地方实例化类。这个问题发生在c#中的基础知识很差时,我强烈建议你从基础知识开始。我没有做基础知识,然后我后悔,并回到学习所有的基础知识,并回到编码应用程序。 – Zany

0

变化跟随着你的ClassViewModel和尝试。

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; 
     public ObservableCollection<string> Records 
     { 
      get { return _records; } 
      set 
      { 
       _records = value; 
       RaisePropertyChanged(); 
      } 
     } 
     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.classname.ToString()); 
      ClearForm(); 
     } 

     private void Delete(object obj) 
     { 
      throw new NotImplementedException(); 
     } 

     void ClearForm() 
     { 
      classname = string.Empty; 
     } 

     void ShowAllRecords() 
     { 
      _records.Clear(); 
      var classdb = db.GetItems<ClassModel>(); 
      foreach (var classmodel in classdb) 
      { 
       _records.Add(classmodel.ToString()); 
      } 
     } 
    } 
} 
相关问题