如何在Xamarin.Forms的列表视图中检索自定义单元格的条目单元格的文本

时间:2016-03-25 12:36:30

标签: listview xamarin xamarin.forms

我是Xamarin.Forms的新手,我创建了一个列表,其中包含带有4个标签的自定义单元格& 1个条目。我设法显示标签&条目。但我想使用Entry cell中的文本。怎么做?我的用户界面就像this

下面是代码模型

public class DSO_beat_Retailer_mapping
{
    [PrimaryKey, AutoIncrement]
    public int _id{ get; set;}
    public string DSO_CD{ get; set;}
    public string Beat_id{ get; set;}
    public string Retailer_cd{ get; set;}
    public string Retailer_nm{ get; set;}
    public string email{ get; set;}
    public string mobile{ get; set;}
    public DateTime birth_dt{ get; set;}
    public DateTime Anniversary_dt{ get; set;}
    public DateTime Lst_sync_dt{ get; set;}

}

以下是Page

public class RetailerListPage : ContentPage
{
    public RetailerListPage()
    {
        BackgroundColor = Color.White;
        Title = "Retailer List";

        CreateNewDB database = new CreateNewDB();
        database.saveDSOReatilMap(retailerlst);

        // Fetch data from LOCAL TABLE DSO_beat_Retailer_mapping
        List<DSO_beat_Retailer_mapping> RetailerList = database.GetDSOReatilMap("Select * from DSO_beat_Retailer_mapping ").ToList();

        ListView listview = new ListView();
        listview.RowHeight = 100;
        listview.ItemsSource = RetailerList;
        listview.ItemTemplate = new DataTemplate(typeof(CustomCell));  

        this.Content = listview;
    }

public class CustomCell : ViewCell
    {
        public CustomCell()
        {
    //Please Condider 4 labes & 1 Entry cell is created though below code have 1 label & 1 entry

            AbsoluteLayout cellView = new AbsoluteLayout();
            var retailernameLabel = new Label();
            AbsoluteLayout.SetLayoutBounds(retailernameLabel, new Rectangle(5, 12 , AbsoluteLayout.AutoSize,AbsoluteLayout.AutoSize));
            retailernameLabel.SetBinding(Label.TextProperty, new Binding("Retailer_nm"));
            retailernameLabel.FontSize = 18;
            retailernameLabel.TextColor = Color.FromHex("#434343");
            cellView.Children.Add(retailernameLabel);

    //Remaining 3 labels goes here

            var txtAmt = new Entry();
            AbsoluteLayout.SetLayoutBounds(txtAmt, new Rectangle(5, 32, 500, 60));
            txtAmt.SetBinding(Entry.TextProperty, new Binding("inputAmt"));
            txtAmt.Keyboard = Keyboard.Numeric;
            txtAmt.TextColor = Color.Black;
            cellView.Children.Add(txtAmt);

            this.View = cellView;

            View = new StackLayout()
            {
                BackgroundColor = rowcolor,
                Children = { cellView }
            };

        }
    }

}

另请注明上述代码中的错误以及绑定数据的正确方法。

代码更改如下:

public class RetailerListPage : ContentPage
{
    public RetailerListPage()
    {
        BackgroundColor = Color.White;
        Title = "Retailer List";

        BindingContext = new RetailerListPageViewModel();

        ListView listview = new ListView();
        listview.RowHeight = 100;

        listview.ItemTemplate = new DataTemplate(typeof(CustomCell));

        listview.SetBinding(ListView.ItemsSourceProperty, "RetailerList");

        Content = listview;
    }

    public class CustomCell : ViewCell
    {
        public CustomCell()
        {
           #region Code that Customizes Cell

            AbsoluteLayout cellView = new AbsoluteLayout();
            var retailernameLabel = new Label();
            AbsoluteLayout.SetLayoutBounds(retailernameLabel, new Rectangle(5, 12, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
            retailernameLabel.SetBinding(Label.TextProperty, new Binding("Retailer_nm"));
            retailernameLabel.FontSize = 18;
            retailernameLabel.TextColor = Color.FromHex("#434343");
            cellView.Children.Add(retailernameLabel);

            var txtAmt = new Entry();
            AbsoluteLayout.SetLayoutBounds(txtAmt, new Rectangle(5, 32, 500, 60));
            txtAmt.SetBinding(Entry.TextProperty, new Binding("InputAmt"));
            txtAmt.Keyboard = Keyboard.Numeric;
            txtAmt.TextColor = Color.Black;
            cellView.Children.Add(txtAmt);

            this.View = cellView;

            View = new StackLayout()
            {
              Children = { cellView }
            };
            #endregion
        }
    }

}

将inputAmt的新属性添加到Model中如下

public class DSO_beat_Retailer_mapping: INotifyPropertyChanged
{
    public string _inputAmt;  //NEWLY ADDED
    public int _id;
    public string _DSO_CD;
    public string _Beat_id;
    public string _Retailer_cd;
    public string _Retailer_nm;
    public string _email;
    public string _mobile;
    public DateTime _birth_dt;
    public DateTime _Anniversary_dt;
    public DateTime _Lst_sync_dt;
    public string InputAmt
    {
        get
        {
            return _inputAmt;
        }

        set
        {
            _inputAmt = value;
            OnPropertyChanged("inputAmt");
        }
    }

    public int Id
    {
        get
        {
            return _id;
        }

        set
        {
            _id = value;
            OnPropertyChanged("ID");
        }
    }

    public string DSO_CD
    {
        get
        {
            return _DSO_CD;
        }

        set
        {
            _DSO_CD = value;
            OnPropertyChanged("DSO_CD");
        }
    }

    public string Beat_id
    {
        get
        {
            return _Beat_id;
        }

        set
        {
            _Beat_id = value;
            OnPropertyChanged("Beat_id");
        }
    }

    public string Retailer_cd
    {
        get
        {
            return _Retailer_cd;
        }

        set
        {
            _Retailer_cd = value;
            OnPropertyChanged("Retailer_cd");
        }
    }

    public string Retailer_nm
    {
        get
        {
            return _Retailer_nm;
        }

        set
        {
            _Retailer_nm = value;
            OnPropertyChanged("Retailer_nm");
        }
    }

    public string email
    {
        get
        {
            return _email;
        }

        set
        {
            _email = value;
            OnPropertyChanged("email");
        }
    }

    public string mobile
    {
        get
        {
            return _mobile;
        }

        set
        {
            _mobile = value;
            OnPropertyChanged("mobile");
        }
    }

    public DateTime birth_dt
    {
        get
        {
            return _birth_dt;
        }

        set
        {
            _birth_dt = value;
            OnPropertyChanged("birth_dt");
        }
    }

    public DateTime Anniversary_dt
    {
        get
        {
            return _Anniversary_dt;
        }

        set
        {
            _Anniversary_dt = value;
            OnPropertyChanged("Anniversary_dt");
        }
    }

    public DateTime Lst_sync_dt
    {
        get
        {
            return _Lst_sync_dt;
        }

        set
        {
            _Lst_sync_dt = value;
            OnPropertyChanged("Lst_sync_dt");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
            this.PropertyChanged(this, e);
        }
    }
}

ViewModel如下

 public class RetailerListPageViewModel : INotifyPropertyChanged
{
    private List<DSO_beat_Retailer_mapping> retailerList;

    public RetailerListPageViewModel()
    {
        #region Here data inserted for test purpose 
        //DSO_beat_Retailer_mapping retailerlst = new DSO_beat_Retailer_mapping
        //{
        //    InputAmt = "200",
        //    _id = 1,
        //    DSO_CD = "123",
        //    Beat_id = "111",
        //    Retailer_nm = "XYZ RETAILER",
        //    email = "ZYZ@ABCD.com",
        //    mobile = "1234567890",
        //    birth_dt = DateTime.Now,
        //    Anniversary_dt = DateTime.Now,
        //    Lst_sync_dt = DateTime.Now
        //};

        //DSO_beat_Retailer_mapping retailerlst1 = new DSO_beat_Retailer_mapping
        //{
        //    InputAmt = "200",
        //    _id = 1,
        //    DSO_CD = "123",
        //    Beat_id = "111",
        //    Retailer_cd = "R123",
        //    Retailer_nm = "XYZ RETAILER",
        //    email = "ZYZ@ABCD.com",
        //    mobile = "1234567890",
        //    birth_dt = DateTime.Now,
        //    Anniversary_dt = DateTime.Now,
        //    Lst_sync_dt = DateTime.Now
        //};
        #endregion

        CreateNewDB database = new CreateNewDB();
        //database.saveDSOReatilMap(retailerList);  
        //database.saveDSOReatilMap(retailerlst1);

       RetailerList = database.GetDSOReatilMap("Select * from DSO_beat_Retailer_mapping ").ToList();
    }

        public List<DSO_beat_Retailer_mapping> RetailerList
    {
        get { return retailerList; }

        set
        {
            retailerList = value;
            OnPropertyChanged("RetailerList");
        }
    }

    public Command btnSave
    {
        get
        {
            return new Command(() => {
                // Code to save List 

            });
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
            this.PropertyChanged(this, e);
        }
    }
}

以下CreateNewDB是访问数据库的类

public class CreateNewDB
{
    static object locker = new object ();
    SQLiteConnection database;

    public CreateNewDB ()
    {

        database = DependencyService.Get<ISQLite> ().GetConnection ();

        database.DropTable<DSO_beat_Retailer_mapping> ();
        database.CreateTable<DSO_beat_Retailer_mapping> ();
    }


    //DSO_beat_Retailer_mapping
    public IEnumerable<DSO_beat_Retailer_mapping> GetDSOReatilMap(string query)
    {
        lock (locker) {
            return database.Query<DSO_beat_Retailer_mapping> (query);
        }
    }

    public string saveDSOReatilMap(DSO_beat_Retailer_mapping item)
    {
        lock (locker) {
            database.Insert(item);
            return item.Retailer_cd;
        }
    }
//UPDATE DB  
public string UpdateDSOReatilMap(DSO_beat_Retailer_mapping item)
    {
        lock (locker)
        {
            database.Update(item);
            return item.Retailer_cd;
        }
    }
    public int DeleteDSOReatilMap(string empcd)
    {
        lock (locker) {
            return database.Delete<DSO_beat_Retailer_mapping> (empcd);
        }
    }


}

我的问题是:

  1. 仍然无法填充列表
  2. 我需要在RetailerListPage添加NEXT BUTTON(如附图中所示),以便在下一页上使用详细信息。
  3. 无法在数据库中更新详细信息,即(金额)
  4. 请指导。

1 个答案:

答案 0 :(得分:2)

Xamarin.Forms中广泛使用的模式是 MVVM 模式(代表模型视图ViewModel)。它用于将特定UI代码与业务逻辑代码分离。在该模式之后,您将创建ViewModel类,该类为View准备数据并允许将View与Model连接(因此名称)。允许将ViewModel实际绑定到View的重要部分是INotifyPropertyChanged接口。 ViewModel应该实现此接口以通知视图有关ViewModel中发生的更改。然后,当ViewModel中发生某些事情时,它应该引发实际发送通知OnPropertyChanged方法进行查看。其他重要的部分是视图必须知道ViewModel订阅了哪些通知。为此,我们使用BindingContext属性。请参阅以下代码,我认为应该清楚。

在您的情况下,我们可以为RetailerListPageViewModel创建RetailerListPage

public class RetailerListPageViewModel : INotifyPropertyChanged
{
    private List<DSO_beat_Retailer_mapping> retailerList;

    public RetailerListPageViewModel()
    {
        CreateNewDB database = new CreateNewDB();
        database.saveDSOReatilMap(retailerlst);

        // Fetch data from LOCAL TABLE DSO_beat_Retailer_mapping
        RetailerList = database
                               .GetDSOReatilMap("Select * from DSO_beat_Retailer_mapping ")
                               .ToList();
    }


    public List<DSO_beat_Retailer_mapping> RetailerList
    {
        get { return retailerList; }
        set 
        { 
            retailerList = value; 
            OnPropertyChanged("RetailerList");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
            this.PropertyChanged(this, e);
        }
    }
}

现在我们可以修改RetailerListPage以使用我们创建的ViewModel(使用BindingContext属性):

public class RetailerListPage : ContentPage
{
    public RetailerListPage()
    {
       BackgroundColor = Color.White;
       Title = "Retailer List";

       BindingContext = new RetailerListPageViewModel();

       ListView listview = new ListView();
       listview.RowHeight = 100;
       listview.ItemTemplate = new DataTemplate(typeof(CustomCell));

       listview.SetBinding(ListView.ItemsSourceProperty, "RetailerList");  

       this.Content = listview;
    }
}

当您将ListView绑定到某些数据集合时,每个CustomCell都会从该集合中为BindingContext指定一个项目。您可以认为它现在成为CustomCell的ViewModel。这实际上解释了为什么显示标签的代码有效。因此,您正在尝试将Entry.Text绑定到inputAmt属性,但inputAmt类中不存在名为DSO_beat_Retailer_mapping的属性。因此,您需要使用该属性和要显示的其他属性创建ViewModel:

public class BeatRetailerItemViewModel : INotifyPropertyChanged
{
    private string _inputAmt;  
    private string retailerNm;

    public string inputAmt
    {
        get { return _inputAmt; }
        set 
        { 
            _inputAmt= value; 
            OnPropertyChanged("inputAmt");
        }
    }

    public string Retailer_nm 
    { 
        get { return retailerNm; }
        set 
        { 
            retailerNm = value; 
            OnPropertyChanged("Retailer_nm");
        }
    }

    //and other properties that you need to display
    //and INotifyPropertyChanged implementation
}

好的,但我们还没有走出困境。现在我们必须同意修改RetailerListPageViewModel以使用新的ViewModel(我省略了INotifyPropertyChanged代码,因为你可以在上面的示例中看到它):

public class RetailerListPageViewModel : INotifyPropertyChanged
{
    private List<BeatRetailerItemViewModel> retailerList;

    public RetailerListPageViewModel()
    {
        CreateNewDB database = new CreateNewDB();
        database.saveDSOReatilMap(retailerlst);

        // Fetch data from LOCAL TABLE DSO_beat_Retailer_mapping
        RetailerList = database
                               .GetDSOReatilMap("Select * from DSO_beat_Retailer_mapping ")
                               .Select(x => new BeatRetailerItemViewModel { Retailer_nm = x.Retailer_nm })
                               .ToList();
    }

    public List<BeatRetailerItemViewModel> RetailerList
    {
        get { return retailerList; }
        set 
        { 
            retailerList = value; 
            OnPropertyChanged("RetailerList");
        }
    }

    //INotifyPropertyChanged implementation
}

我希望你能看到那里的模式:)