庆军之xamarin.forms 动态页面构造及查询
两个页面,一个是用于菜单,一个是用于列表查询。
菜单 使用了 TabView,所有的代码写在了OnAppearing,但是它没有内容了。找了两天原因。在github提到这是一个bug。不会修复。但是可以在构造函数中加载,就不会有问题。
列表查询,我也放在了构造函数里面,但是绑定数据之后,数据显示错乱了。找了三天原因。然后改了写法。使用了
CollectionView,
原来的是写法是 CollectionView.ItemTemplate = new DataTemplate(new stacklayout()) new stacklayout在前面创建好的。
后面改成了 CollectionView.ItemTemplate = new DataTemplate(()=>{
return StackLayout();
});
首次,这样写,这句话的执行是在ItemSource赋值的时候才会处理。
然后,数据源是DataTable,所以ItemSource = DataTable.Rows,显示不了数据。
因为绑定不能 写 "列名",要写成ItemArray[序号]。所以这个要提前把顺序固定下来。
下面是我的垃圾代码。
<xct:TabView x:Name="tabPrinterView" TabStripPlacement="Bottom" TabIndicatorColor="#2396F3" > </xct:TabView>
foreach (var fitem in menus) { TabViewItem _tabViewItem = new TabViewItem() { Text = fitem.menuname, //TextColor = Color.Black, //Margin = new Thickness(9d), //BackgroundColor = Color.Red, }; var scollview = new ScrollView(); //StackLayout stackLayout = new StackLayout() { Orientation = StackOrientation.Vertical,BackgroundColor = Color.Blue}; Grid maingrid = new Grid() { //BackgroundColor = Color.Yellow }; scollview.Content = maingrid; maingrid.ColumnDefinitions = new ColumnDefinitionCollection { new ColumnDefinition() { Width = GridLength.Star }, new ColumnDefinition() { Width = GridLength.Star }, new ColumnDefinition() { Width = GridLength.Star } }; //stackLayout.Children.Add(maingrid); var rowid = 0; var colid = 0; if ((fitem.sonmenus?.Count ?? 0) > 0) { maingrid.RowDefinitions = new RowDefinitionCollection(); var maxrow = Math.Ceiling(fitem.sonmenus.Count * 1m / 3 * 1m); for (int rowindex = 0; rowindex < maxrow; rowindex++) { maingrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star }); } foreach (var sonitem in fitem.sonmenus) { Label label = new Label(); label.HorizontalTextAlignment = TextAlignment.Center; //label.Text = item.menuicon; //label.FontFamily = "FontAwesomeSolids"; label.FormattedText = new FormattedString(); Span span = new Span(); span.FontSize = 26; try { int testa = Convert.ToInt32(sonitem.iconname.Replace("\\x", "").Replace("\\n", ""), 16); span.Text = $"{char.ConvertFromUtf32(testa)}\n"; } catch (Exception ex) { span.Text = $"{sonitem.iconname}\n"; } //span.SetBinding(Span.FontFamilyProperty, "FontAwesomeSolids"); span.FontFamily = "FontAwesome5FreeSolid900.ttf#Regular";//(string)Application.Current.Resources["FontAwesomeSolids"]; if (!string.IsNullOrWhiteSpace(sonitem.iconcolor)) { span.TextColor = Color.Red;//.FromHex(sonitem.iconcolor); } Span span1 = new Span(); span1.Text = sonitem.title; span1.TextColor = Color.Blue; label.FormattedText.Spans.Add(span); label.FormattedText.Spans.Add(span1); var tapped = new TapGestureRecognizer() { NumberOfTapsRequired = 1, CommandParameter = sonitem.menukey, }; tapped.Tapped += Tapped_TappedAsync; label.GestureRecognizers.Add(tapped); maingrid.Children.Add(label, colid, rowid); if (colid == 2) { rowid += 1; colid = 0; } else { colid += 1; } } } _tabViewItem.Content = scollview; //= new Label() { Text = fitem.menuname, TextColor = Color.BlueViolet, Margin = new Thickness(10) }; this.tabPrinterView.TabItems.Add(_tabViewItem); }
以下是明细
private outReportDefine2 _reportdefine = null; public ReaportShowPage(outReportDefine2 datas) { InitializeComponent(); _reportdefine = datas; //绑定源定义,因为body执行是绑定数据源的时候。才会处理,所以要提前指定好列和序号的关系。 this.buildBodySource(); //单头定义 //this.filter.ContentTemplate = null; this.buildHead(); //单尾定义 this.buildFoot(); //单body定义 this.buildBody(); //this._reportid = reportid; } private outReportHeaderDefine selectStore = null; private Dictionary<outReportHeaderDefine, View> filters = new Dictionary<outReportHeaderDefine, View>(); void buildHead() { var headStackLayout = new StackLayout(); headStackLayout.Orientation = StackOrientation.Vertical; this.Title = this._reportdefine.tabledefine.reportname ?? ""; foreach (var item in this._reportdefine.tabledefine.headeritems) { FlexLayout flexLayout = new FlexLayout() { Wrap = FlexWrap.Wrap, AlignItems = FlexAlignItems.Center, }; foreach (var sonitem in item.items) { sonitem.cshowvalue = ""; sonitem.cvalue = ""; if (sonitem.ishidden) { if (string.Compare(sonitem.controltype, "label",true) != 0 && string.IsNullOrWhiteSpace(sonitem.defaultvalue)) { sonitem.cvalue = sonitem.defaultvalue; } filters.Add(sonitem, null);// continue; } if (string.Compare(sonitem.controltype, "text", true) == 0) { Entry sonentry = new Entry(); sonentry.Placeholder = sonitem.holdtext ?? ""; if (double.TryParse(sonitem.style.fontSize, out double fontsize)) { sonentry.FontSize = fontsize; } if (!string.IsNullOrWhiteSpace(sonitem.style.color)) { sonentry.TextColor = Color.FromHex(sonitem.style.color); } double.TryParse(sonitem.style.width.Replace("px", ""), out double swidthd); if (swidthd > 0) { sonentry.WidthRequest = swidthd; } if (sonitem.widthtype == 1) { //sonentry.MinimumWidthRequest = swidthd; } else if (sonitem.widthtype == 0) { //FlexLayout.SetGrow(sonentry, 1); } else if (sonitem.widthtype == 2) { FlexLayout.SetGrow(sonentry, 1f); } if (double.TryParse(sonitem.style.height.Replace("px", ""), out double sheight) && sheight > 0) { sonentry.HeightRequest = sheight; } if (!string.IsNullOrWhiteSpace(sonitem.style.textAlign)) { if (string.Compare(sonitem.style.textAlign, "center", true) == 0) { sonentry.HorizontalTextAlignment = TextAlignment.Center; } else if (string.Compare(sonitem.style.textAlign, "right", true) == 0) { sonentry.HorizontalTextAlignment = TextAlignment.End; } else { sonentry.HorizontalTextAlignment = TextAlignment.Start; } } filters.Add(sonitem, sonentry); //sonentry.Id = ""; //flexLayout.Children[0].AutomationId //filters.Add(sonitem.) flexLayout.Children.Add(sonentry); } else if ( string.Compare(sonitem.controltype, "label", true) == 0) { Label sonlabel = new Label() { Text = sonitem.datapropertyname, }; if (double.TryParse(sonitem.style.fontSize, out double fontsize)) { sonlabel.FontSize = fontsize; } if (!string.IsNullOrWhiteSpace(sonitem.style.color)) { sonlabel.TextColor = Color.FromHex(sonitem.style.color); } double.TryParse(sonitem.style.width.Replace("px", ""), out double swidthd); if (swidthd > 0) { sonlabel.WidthRequest = swidthd; } if (sonitem.widthtype == 1) { //sonentry.MinimumWidthRequest = swidthd; } else if (sonitem.widthtype == 0) { //FlexLayout.SetGrow(sonentry, 1); } else if (sonitem.widthtype == 2) { FlexLayout.SetGrow(sonlabel, 1f); } if (double.TryParse(sonitem.style.height.Replace("px", ""), out double sheight) && sheight > 0) { sonlabel.HeightRequest = sheight; } if (!string.IsNullOrWhiteSpace(sonitem.style.textAlign)) { if (string.Compare(sonitem.style.textAlign, "center", true) == 0) { sonlabel.HorizontalTextAlignment = TextAlignment.Center; } else if (string.Compare(sonitem.style.textAlign, "right", true) == 0) { sonlabel.HorizontalTextAlignment = TextAlignment.End; } else { sonlabel.HorizontalTextAlignment = TextAlignment.Start; } } filters.Add(sonitem, sonlabel); flexLayout.Children.Add(sonlabel); } else if ( string.Compare(sonitem.controltype, "date", true) == 0) { DatePicker sondatePicker = new DatePicker() { Format="yyyy-MM-dd" }; if (double.TryParse(sonitem.style.fontSize, out double fontsize)) { sondatePicker.FontSize = fontsize; } if (!string.IsNullOrWhiteSpace(sonitem.style.color)) { sondatePicker.TextColor = Color.FromHex(sonitem.style.color); } double.TryParse(sonitem.style.width.Replace("px", ""), out double swidthd); if (swidthd > 0) { sondatePicker.WidthRequest = swidthd; } if (sonitem.widthtype == 1) { //sonentry.MinimumWidthRequest = swidthd; } else if (sonitem.widthtype == 0) { //FlexLayout.SetGrow(sonentry, 1); } else if (sonitem.widthtype == 2) { FlexLayout.SetGrow(sondatePicker, 1f); } if (double.TryParse(sonitem.style.height.Replace("px", ""), out double sheight) && sheight > 0) { sondatePicker.HeightRequest = sheight; } filters.Add(sonitem, sondatePicker); flexLayout.Children.Add(sondatePicker); } else if(string.Compare(sonitem.controltype, "sysstore", true) == 0) { Label sonlabel = new Label() { Text = sonitem.datapropertyname, }; if (double.TryParse(sonitem.style.fontSize, out double fontsize)) { sonlabel.FontSize = fontsize; } if (!string.IsNullOrWhiteSpace(sonitem.style.color)) { sonlabel.TextColor = Color.FromHex(sonitem.style.color); } double.TryParse(sonitem.style.width.Replace("px", ""), out double swidthd); if (swidthd > 0) { sonlabel.WidthRequest = swidthd; } if (sonitem.widthtype == 1) { //sonentry.MinimumWidthRequest = swidthd; } else if (sonitem.widthtype == 0) { //FlexLayout.SetGrow(sonentry, 1); } else if (sonitem.widthtype == 2) { FlexLayout.SetGrow(sonlabel, 1f); } if (double.TryParse(sonitem.style.height.Replace("px", ""), out double sheight) && sheight > 0) { sonlabel.HeightRequest = sheight; } if (!string.IsNullOrWhiteSpace(sonitem.style.textAlign)) { if (string.Compare(sonitem.style.textAlign, "center", true) == 0) { sonlabel.HorizontalTextAlignment = TextAlignment.Center; } else if (string.Compare(sonitem.style.textAlign, "right", true) == 0) { sonlabel.HorizontalTextAlignment = TextAlignment.End; } else { sonlabel.HorizontalTextAlignment = TextAlignment.Start; } } filters.Add(sonitem, sonlabel); var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += async (sender, e) => { selectStore = sonitem; await Navigation.PushAsync(new SelectWarehousePage() , true); //Options.FlashTappedAction?.Invoke(); }; sonlabel.GestureRecognizers.Add(tapGestureRecognizer);//.Clear(); flexLayout.Children.Add(sonlabel); } } headStackLayout.Children.Add(flexLayout); } this.filter.ContentTemplate = new DataTemplate(()=>headStackLayout); } private Dictionary<outReportHeaderDefine, View> feeters = new Dictionary<outReportHeaderDefine, View>(); void buildFoot() { var headStackLayout = new StackLayout(); headStackLayout.Orientation = StackOrientation.Vertical; //this.Title = this._reportdefine.tabledefine.reportname ?? ""; if ((this._reportdefine.tabledefine.footeritems?.Count ?? 0) > 0) { foreach (var item in this._reportdefine.tabledefine.footeritems) { FlexLayout flexLayout = new FlexLayout() { Wrap = FlexWrap.Wrap, AlignItems = FlexAlignItems.Center, }; foreach (var sonitem in item.items) { //if (sonitem.field == "sysrowid") //{ Label sonlabel = new Label() { }; if (!sonitem.islabel) { FlexLayout.SetGrow(sonlabel, 1f); sonlabel.SetBinding(Label.TextProperty, sonitem.field); sonlabel.SetBinding(Label.TextColorProperty, sonitem.style.color); sonlabel.SetBinding(Label.FontSizeProperty, sonitem.style.fontSize); } else { sonlabel.Text = sonitem.field ?? ""; } //FlexLayout.SetGrow(sonlabel, 1); if (double.TryParse(sonitem.style.fontSize, out double fontsize)) { sonlabel.FontSize = fontsize; } if (!string.IsNullOrWhiteSpace(sonitem.style.color)) { sonlabel.TextColor = Color.FromHex(sonitem.style.color); } if (double.TryParse(sonitem.style.height, out double sheight) && sheight > 0) { sonlabel.HeightRequest = sheight; } if (!string.IsNullOrWhiteSpace(sonitem.style.textAlign)) { if (string.Compare(sonitem.style.textAlign, "center", true) == 0) { sonlabel.HorizontalTextAlignment = TextAlignment.Center; } else if (string.Compare(sonitem.style.textAlign, "right", true) == 0) { sonlabel.HorizontalTextAlignment = TextAlignment.End; } else { sonlabel.HorizontalTextAlignment = TextAlignment.Start; } } flexLayout.Children.Add(sonlabel); } headStackLayout.Children.Add(flexLayout); } this.fts.Children.Add(headStackLayout); } } void buildBodySource() { if ((this._reportdefine.tabledefine.bodyitems?.Count ?? 0) > 0) { foreach (var item in this._reportdefine.tabledefine.bodyitems) { foreach (var sonitem in item.items) { if (sonitem.ishidden) continue; if (string.IsNullOrWhiteSpace(sonitem.field)) continue; //var fieldindex = colnames.IndexOf(sonitem.field); if (!colnames.Contains(sonitem.field)) { dt.Columns.Add(new DataColumn(sonitem.field, typeof(string))); colnames.Add(sonitem.field); //fieldindex = colnames.Count - 1; } } } } } private List<string> colnames = new List<string>(50); void buildBody() { this.cv.ItemTemplate = new DataTemplate(() => { var fFrame = new Frame() { BorderColor = Color.Gray }; var headStackLayout = new StackLayout(); headStackLayout.Orientation = StackOrientation.Vertical; fFrame.Content = headStackLayout; //this.Title = this._reportdefine.tabledefine.reportname ?? ""; if ((this._reportdefine.tabledefine.bodyitems?.Count ?? 0) > 0) { //Grid grid = new Grid { Padding = 8, BackgroundColor = Color.Yellow }; //fFrame.Content = grid; var indexleft = 0; var indextop = 0; foreach (var item in this._reportdefine.tabledefine.bodyitems) { //grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); FlexLayout flexLayout = new FlexLayout() { Wrap = FlexWrap.Wrap, AlignItems = FlexAlignItems.Stretch, }; foreach (var sonitem in item.items) { if (sonitem.ishidden) continue; buildBodyLable(flexLayout,null, sonitem, indexleft, indextop); indexleft += 1; if (string.IsNullOrWhiteSpace(sonitem.field)) { continue; } Label sonfeild = new Label() { }; var fieldindex = colnames.IndexOf(sonitem.field); if (fieldindex < 0) { dt.Columns.Add(new DataColumn(sonitem.field, typeof(string))); colnames.Add(sonitem.field); fieldindex = colnames.Count - 1; } sonfeild.SetBinding(Label.TextProperty, $"ItemArray[{fieldindex}]");// sonitem.field); if (double.TryParse(sonitem.style.fontSize, out double ffontsize)) { sonfeild.FontSize = ffontsize; } if (!string.IsNullOrWhiteSpace(sonitem.style.color)) { sonfeild.TextColor = Color.FromHex(sonitem.style.color); } if (double.TryParse(sonitem.style.width, out double fswidthd) && fswidthd > 0) { sonfeild.WidthRequest = fswidthd; } if (sonitem.widthtype == 1 ) { //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); } else if (sonitem.widthtype == 0) { //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); //FlexLayout.SetGrow(sonentry, 1); } else if (sonitem.widthtype == 2) { //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1d, GridUnitType.Star) }); FlexLayout.SetGrow(sonfeild, 1f); } if (double.TryParse(sonitem.style.height, out double fsheight) && fsheight > 0) { sonfeild.HeightRequest = fsheight; } if (!string.IsNullOrWhiteSpace(sonitem.style.textAlign)) { if (string.Compare(sonitem.style.textAlign, "center", true) == 0) { sonfeild.HorizontalTextAlignment = TextAlignment.Center; } else if (string.Compare(sonitem.style.textAlign, "right", true) == 0) { sonfeild.HorizontalTextAlignment = TextAlignment.End; } else { sonfeild.HorizontalTextAlignment = TextAlignment.Start; } } //grid.Children.Add(sonfeild, indexleft, indextop); indexleft += 1; flexLayout.Children.Add(sonfeild); } indexleft = 0; indextop += 1; headStackLayout.Children.Add(flexLayout); } } return fFrame; }); //this.cv.ItemTemplate = new DataTemplate(() => // { // Grid grid = new Grid { Padding = 10 }; // grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); // grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); // grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); // grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); // //Image image = new Image { Aspect = Aspect.AspectFill, HeightRequest = 60, WidthRequest = 60 }; // //image.SetBinding(Image.SourceProperty, "ImageUrl"); // Label nameLabel = new Label { FontAttributes = FontAttributes.Bold }; // nameLabel.SetBinding(Label.TextProperty, "ITEM_CODE"); // Label locationLabel = new Label { FontAttributes = FontAttributes.Italic, VerticalOptions = LayoutOptions.End }; // locationLabel.SetBinding(Label.TextProperty, "ITEM_NAME"); // //Grid.SetRowSpan(image, 2); // //grid.Children.Add(image); // grid.Children.Add(nameLabel, 1, 0); // grid.Children.Add(locationLabel, 1, 1); // return grid; // });//有效写法。 //this.cv.ItemTemplate = new DataTemplate(() => fFrame);//无效写法 } private static void buildBodyLable(FlexLayout flexLayout,Grid grid, outReportBodyDefine sonitem,int left,int top) { var sonlabel = new Label() { }; //if (string.Compare(sonitem.field, "sysrowid", true) != 0 && // !string.IsNullOrWhiteSpace(sonitem.label)) //{ sonlabel.Text = $"{sonitem.label}"; //} //if (sonitem.field == "sysrowid") //{ //sonlabel.SetBinding(Label.TextColorProperty, sonitem.headstyle.color); //sonlabel.SetBinding(Label.FontSizeProperty, sonitem.headstyle.fontSize); //FlexLayout.SetGrow(sonlabel, 1); if (double.TryParse(sonitem.headstyle.fontSize, out double fontsize)) { sonlabel.FontSize = fontsize; } if (!string.IsNullOrWhiteSpace(sonitem.headstyle.color)) { sonlabel.TextColor = Color.FromHex(sonitem.headstyle.color); } if (double.TryParse(sonitem.headstyle.width, out double swidthd) && swidthd > 0) { sonlabel.WidthRequest = swidthd; } if (sonitem.widthtype == 1 ) { //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); } else if (sonitem.widthtype == 0) { //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); //FlexLayout.SetGrow(sonentry, 1); } else if (sonitem.widthtype == 2) { //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1d, GridUnitType.Star) }); FlexLayout.SetGrow(sonlabel, 1f); } if (double.TryParse(sonitem.headstyle.height, out double sheight) && sheight > 0) { sonlabel.HeightRequest = sheight; } if (!string.IsNullOrWhiteSpace(sonitem.headstyle.textAlign)) { if (string.Compare(sonitem.headstyle.textAlign, "center", true) == 0) { sonlabel.HorizontalTextAlignment = TextAlignment.Center; } else if (string.Compare(sonitem.headstyle.textAlign, "right", true) == 0) { sonlabel.HorizontalTextAlignment = TextAlignment.End; } else { sonlabel.HorizontalTextAlignment = TextAlignment.Start; } } //filters.Add(sonitem, sonlabel); //grid.Children.Add(sonlabel, left, top); flexLayout.Children.Add(sonlabel); } DataTable dt = new DataTable("body"); DataTable dtfeet = new DataTable("feet"); private async void Button_Clicked(object sender, EventArgs e) { try { List<outReportHeaderDefine> defines = new List<outReportHeaderDefine>(filters.Count); foreach (var item in filters) { var sonitem = item.Key; defines.Add(sonitem); if (sonitem.controltype == "text") { if(item.Value is Entry eitem) sonitem.cvalue = ((eitem).Text??"").Trim(); } else if (sonitem.controltype == "label") { } else if (sonitem.controltype == "date") { if(item.Value is DatePicker eitem) sonitem.cvalue = eitem.Date.ToString("yyyy-MM-dd HH:mm:ss");//.Trim(); } else if (sonitem.controltype == "sysstore") { if (item.Value is Label eitem) sonitem.cvalue = "";// eitem.Text??"";// } } (var isok, var errorinfo, var datas) = await ReaportsService.FetchReportDatass(new inFetchReport() { shopid = CustomerConfig.Instance.ShopId, dbkey = CustomerConfig.Instance.DbKey, filterobj = JsonConvert.SerializeObject(new { items = defines }), reportcode = _reportdefine.tabledefine.reportkey }); if (!isok) { await AppMsg.ShowErrorDialog($"{errorinfo}"); return; } var dataset = JsonConvert.DeserializeObject<System.Data.DataSet>(datas); foreach (DataRow item in dataset.Tables[0].Rows) { var newitem = dt.NewRow(); foreach (DataColumn coldefine in dt.Columns) { if (dataset.Tables[0].Columns.IndexOf(coldefine.ColumnName) > -1) { newitem[coldefine.ColumnName] = item[coldefine.ColumnName] ==null?"":item[coldefine.ColumnName].ToString(); } } dt.Rows.Add(newitem); } //this.cv.BindingContext = table1Tests; //this.cv.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepItemsInView;//= //this.cv.VerticalScrollBarVisibility = ScrollBarVisibility.Always; this.cv.ItemsSource = dt.Rows;// DatatoTable( dataset.Tables[0]); //this.cv.ItemsSource = new Dictionary<string, List<Dictionary<string, object>>>(); } catch(Exception ex) { await AppMsg.ShowErrorDialog($"{ex.Message}"); } } public List<Dictionary<string, object>> DatatoTable(DataTable dt) { List<Dictionary<string, object>> list = new List<Dictionary<string, object>>(); foreach (DataRow dr in dt.Rows)//每一行信息,新建一个Dictionary<string,object>,将该行的每列信息加入到字典 { Dictionary<string, object> result = new Dictionary<string, object>(); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc].ToString()); } list.Add(result); } return list; }