Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Listview 如何在Xamarin.Forms的列表视图中检索自定义单元格的输入单元格文本_Listview_Xamarin_Xamarin.forms - Fatal编程技术网

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

Listview 如何在Xamarin.Forms的列表视图中检索自定义单元格的输入单元格文本,listview,xamarin,xamarin.forms,Listview,Xamarin,Xamarin.forms,我是Xamarin.Forms的新手,我创建了一个列表,其中有4个标签和1个条目的自定义单元格。我设法显示标签和条目。但我想使用输入单元格中的文本。怎么做?我的UI就像 下面是代码模型 public class DSO_beat_Retailer_mapping { [PrimaryKey, AutoIncrement] public int _id{ get; set;} public string DSO_CD{ get; set;} public strin

我是Xamarin.Forms的新手,我创建了一个列表,其中有4个标签和1个条目的自定义单元格。我设法显示标签和条目。但我想使用输入单元格中的文本。怎么做?我的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 }
            };

        }
    }

}
为模型中的InputMT添加了一个新属性,如下所示

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);
        }
    }
}
视图模型如下所示

 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);
        }
    }
}
我的问题是:

  • 仍然无法填充列表
  • 我需要在
    RetailerListPage
    上添加下一步按钮(如附图所示),以便在下一页使用详细信息
  • 无法在数据库中更新详细信息,即(金额)

  • 请指导。

    Xamarin.Forms
    中广泛使用的模式是MVVM模式(代表模型视图模型)。它用于将特定的UI代码与业务逻辑代码分离。按照该模式创建ViewModel类,该类为视图准备数据,并允许将视图与模型连接(因此得名)。允许将ViewModel实际绑定到视图的重要部分是
    INotifyPropertyChanged
    接口。ViewModel应实现此接口,以通知view有关ViewModel中发生的更改。然后,当ViewModel中发生某些事情时,它应该引发实际向view发送通知的
    OnPropertyChanged
    方法。另一个重要部分是视图必须知道订阅通知的ViewModel。为此,我们使用
    BindingContext
    属性。参考下面的代码,我认为应该很清楚

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

    公共类RetailerListPageViewModel:INotifyPropertyChanged
    {
    私人名单零售商;
    公共RetailerListPageViewModel()
    {
    CreateNewDB数据库=新建CreateNewDB();
    数据库.saveDSOReatilMap(RetailerList);
    //从本地表DSO\U beat\U DETALLER\U映射获取数据
    RetailerList=数据库
    .GetDSOReatilMap(“从DSO\u beat\u零售商\u映射中选择*)
    .ToList();
    }
    公共列表零售商列表
    {
    获取{return retailerList;}
    设置
    { 
    零售商列表=价值;
    OnPropertyChanged(“零售列表”);
    }
    }
    公共事件属性更改事件处理程序属性更改;
    公共void OnPropertyChanged(字符串propertyName)
    {
    if(this.PropertyChanged!=null)
    {
    PropertyChangedEventArgs e=新的PropertyChangedEventArgs(propertyName);
    本。财产变更(本,e);
    }
    }
    }
    
    现在,我们可以修改
    RetailerListPage
    以使用我们创建的ViewModel(通过使用
    BindingContext
    属性):

    公共类RetailerListPage:ContentPage
    {
    公共零售列表页()
    {
    背景颜色=颜色。白色;
    Title=“零售商列表”;
    BindingContext=new RetailerListPageViewModel();
    ListView ListView=新建ListView();
    listview.RowHeight=100;
    listview.ItemTemplate=新数据模板(typeof(CustomCell));
    SetBinding(listview.ItemsSourceProperty,“RetailerList”);
    this.Content=listview;
    }
    }
    
    当您将
    ListView
    绑定到某个数据集合时,每个
    CustomCell
    将作为
    BindingContext
    从该集合中分配一个项。您可以认为它现在成为
    CustomCell
    的视图模型。这就是为什么显示标签的代码可以工作的原因。因此,您正在尝试将
    Entry.Text
    绑定到
    inputAmt
    属性,但名为
    inputAmt
    的属性不存在于
    DSO\u beat\u Retailer\u mapping
    类中。因此,您需要使用该属性和要显示的其他属性创建ViewModel:

    公共类BeatRetailerItemViewModel:INotifyPropertyChanged
    {
    私有字符串输入;
    私人字符串零售商;
    公共字符串输入
    {
    获取{return\u inputAmt;}
    设置
    { 
    _输入=值;
    OnPropertyChanged(“inputAmt”);
    }
    }
    公共字符串\u nm
    { 
    获取{return retailerNm;}
    设置
    { 
    零售额M=价值;
    OnProperty变更(“零售商”);
    }
    }
    //以及需要显示的其他属性
    //和INotifyPropertyChanged实现
    }
    
    好吧,但我们还没有走出困境。现在我们必须相应地修改
    RetailerListPageViewModel
    以使用新的ViewModel(我省略了
    INotifyPropertyChanged
    代码,因为您可以在上面的示例中看到它):

    公共类RetailerListPageViewModel:INotifyPropertyChanged
    {
    私人名单零售商;
    公共RetailerListPageViewModel()
    {
    CreateNewDB数据库=新建CreateNewDB();
    数据库.saveDSOReatilMap(RetailerList);
    //从本地表DSO\U beat\U DETALLER\U映射获取数据
    RetailerList=数据库
    .GetDSOReatilMap(“从DSO\u beat\u零售商\u映射中选择*)
    .Select(x=>new BeatRetailerItemViewModel{Retailer\u nm=x.Retailer\u nm})
    .ToList();
    }
    公共列表零售商列表
    {
    获取{return retailerList;}
    设置
    { 
    零售商列表=价值;
    OnPropertyChanged(“零售列表”);
    }
    }
    //INotifyPropertyChanged实现
    }
    

    我希望您能在那里看到模式:)

    Xamarin中广泛使用的模式。Forms
    MVVM模式(代表模型视图模型)。它用于将特定的UI代码与业务逻辑代码分离。按照该模式创建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);
            }
        }
    }
    
    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);
            }
        }
    
    
    }