以编程方式为 SQLite 表中动态创建的控件创建 xamarin.forms 应用程序,并使用 MVVM 中的绑定显示值

问题描述

我已经创建了 sqlite 数据库以及 fieldName 和 fieldType 的表,我有代码来检查字段类型并创建控件。

检查 FieldType 并创建控件

 async void ShowDynamicForm()
            {
                StackLayout stack = new StackLayout { Orientation = StackOrientation.Vertical,Padding = 25 };
                Grid myGrid = new Grid();
                myGrid.Padding = 10;
                myGrid.RowDeFinitions = new RowDeFinitionCollection
                {
          
                new RowDeFinition { Height = GridLength.Auto },new RowDeFinition { Height = GridLength.Auto },};
            int colCount = 0;
            int rowCount = 0;

            path = Path.Combine(personalPath,"dynamicfield.sqlite");
            var DynRepo = new DynFieldRepo(path);
            var ListObj = new ListObjRepo(path);

            var DynField = await DynRepo.GetItemAsync();
         

检查 FieldType

           foreach (var item in DynField)
               {
                 switch (item.FieldType)
                  {
                    case "Label":
                        Label lbl = new Label { HeightRequest = 0 };
                        lbl.Text = item.FieldName;
                        colCount = 0;
                        myGrid.Children.Add(lbl,colCount,rowCount);
                        rowCount++;
                        break;

                    case "RadioButton":
                        RadioButton abcs = new RadioButton { HeightRequest = 0};
                        Label lblRadioButton = new Label();
                        lblRadioButton.Text = item.FieldName;
                        myGrid.Children.Add(lblRadioButton,rowCount);
                        myGrid.Children.Add(abcs,rowCount);
                        rowCount++;
                        break;

                    case "Entry":
                        
                        var entry = new Entry{ Placeholder = item.FieldName};
                        Label lblEntry = new Label();
                        _myentries.Add(entry);
                        entry.BindingContext = item.FieldName;
                        lblEntry.Text = item.FieldName;
                        colCount = 0;
                        myGrid.Children.Add(lblEntry,rowCount);
                        rowCount++;
                        entry.Margin = 10;
                        myGrid.Children.Add(entry,rowCount);
                        rowCount++;
                        break;

                    case "TextArea":
                        Editor MyEditor = new Editor { Placeholder = item.FieldName};
                        //Label lblTextArea = new Label();
                        //lblTextArea.Text = item.FieldName;
                        //myGrid.Children.Add(lblTextArea,rowCount);
                        //rowCount++;
                        myGrid.Children.Add(MyEditor,rowCount);
                        rowCount++;
                        break;

                    case "CheckBox":
                        
                        CheckBox check = new CheckBox();
                        Label lblCheckBox = new Label();
                        lblCheckBox.Text = item.FieldName;
                        myGrid.Children.Add(lblCheckBox,rowCount);

                        myGrid.Children.Add(check,rowCount);
                        rowCount++;
                        break;

                    case "Dropdown":
                        var ddItems = await ListObj.GetItemAsync(item.FieldName);
                        //var ddItem = ddItems.Where(x => x.FieldName == item.FieldName).ToList();
                        var dd = new Picker();   
                        rowCount++;
                        List<string> listDtaobj = new List<string>();
                        foreach (var val in ddItems)
                        {
                            listDtaobj.Add(val.Values);
                        }
                        dd.ItemsSource = listDtaobj;
                        var selectedItem = dd.SelectedItem;
                        _mypicker.Add(selectedItem);
                        dd.Title = item.FieldName;
                        colCount = 0;
                        myGrid.Children.Add(dd,rowCount);
                        rowCount++;
                        break;

                    case "Button":
                        Button button = new Button();
                        button.Text = item.FieldName;
                        colCount = 0;
                        myGrid.Children.Add(button,rowCount);
                        rowCount++;
                        button.Clicked += (object sender,System.EventArgs e) => {
                            if(item.FieldName == "Submit")
                            {
                                Submit_Clicked();
                            }
                            else if (item.FieldName == "Close")
                            {
                                Close_Clicked();
                            }
                        };
                        break;
                };

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)