[英]How to retrieve text of entry cell of custom cell in a list view in Xamarin.Forms
我是Xamarin.Forms的新手,我創建了一個包含4個標簽和1個條目的“自定義”單元格的列表。 我設法顯示標簽和條目。 但是我想使用Entry單元格中的文本。 怎么做? 我的UI是像這樣
下面是代碼模型
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;}
}
以下是頁面
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添加了一個新屬性,如下所示
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);
}
}
}
我的問題是:
RetailerListPage
上添加NEXT BUTTON(如附件圖像所示),以便可以在下一頁上使用詳細信息。 請指導。
Xamarin.Forms
廣泛使用的模式是MVVM模式(代表Model View ViewModel)。 它用於將特定的UI代碼與業務邏輯代碼分開。 按照該模式,您可以創建ViewModel類,該類為View准備數據並允許將View與Model連接(因此得名)。 重要的部分是INotifyPropertyChanged
接口,它允許將ViewModel實際綁定到View。 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
屬性,但是DSO_beat_Retailer_mapping
類中不存在名為inputAmt
屬性。 因此,您需要使用該屬性和要顯示的其他屬性來創建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
}
我希望你在那里看到模式:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.