【发布时间】:2016-03-27 00:13:57
【问题描述】:
我是 Xamarin.Forms 的新手,我创建了一个包含 4 个标签和 1 个条目的自定义单元格的列表。我设法显示标签和条目。但我想使用条目单元格中的文本。怎么做?我的用户界面就像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;}
}
下面是页面
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
}
}
}
在Model中新增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上添加下一个按钮(如附图所示),以便在下一页上使用详细信息。 - 无法更新数据库中的详细信息,即(金额)
请指导。
【问题讨论】:
-
向我们展示与问题相关的代码以及到目前为止您实际尝试过的内容
-
@ArkadiuszK 请查看更新和指南
-
您是否包括对
RetailerListPageViewModel的修改,即关于RetailerList属性的修改?除了填充列表之外,您的其他问题与您提出的问题无关。我认为您必须为他们创建单独的问题。 -
您没有包含
RetailerListPageViewModel的代码。这就是我问的原因。 -
我很抱歉。我也在 ViewModel 中粘贴了相同的代码模型。但现在我对帖子进行了更正。
标签: listview xamarin xamarin.forms