我有一個網格,我正在嘗試洗掉特定行中的專案。它處于回圈中,然后當它轉到第二行時,它會洗掉第二行中的專案,并洗掉第一行中的專案。如何調整以僅洗掉我想要的行?
請注意,我想要做的是將第一行復制到第二行,僅解決 pow (b2),在第三行中,我將復制第二行,但解決 -4 * 5 (-4 * a)。
這是正在發生的事情的預覽。
這是我的代碼來膨脹這些行,以了解:它首先進入if(fields == null)因為是 pow,然后進入else:
for (int i = 0; i < qntLines; i )
{
string field = lines[i].Substring(0, lines[i].IndexOf('#'));
string operation = lines[i].Substring(lines[i].IndexOf('#') "#".Length);
CreateResultLine(field, operation, i, listTexts);
}
private void CreateResultLine(string field, string operation, int i, List<string> listTexts)
{
string[] fields = null;
List<string> texts = new List<string>();
string text = string.Empty;
dynamic textResult;
int count = new int();
if (field.Contains(','))
fields = field.Split(',', (char)StringSplitOptions.RemoveEmptyEntries);
if (fields == null)
{
text = listTexts[int.Parse(field)];
text = RemovePow(text);
texts.Add(text);
textResult = ExecuteOperation(texts, operation);
gridFrame.Children.RemoveAt(int.Parse(field) 1);
gridFrame.Children.Add(new Label() { Text = textResult.ToString(), HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center }, int.Parse(field) 1, i);
}
else
{
foreach (var item in fields)
{
string noPow = string.Empty;
noPow = RemovePow(listTexts[int.Parse(item)]);
texts.Add(noPow);
count;
}
textResult = ExecuteOperation(texts, operation);
for (int i2 = int.Parse(fields[0]); i2 <= int.Parse(fields[1]);)
{
gridFrame.Children.RemoveAt(int.Parse(fields[0]));
i2;
}
gridFrame.Children.Add(new Label() { Text = textResult.ToString(), HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center }, int.Parse(fields[0]) 1, i);
}
}
uj5u.com熱心網友回復:
這是使用網格單元的完整示例。
這個概念是您首先創建所有單元格。在這里,我Label在每個網格單元中使用了 a 。為了更容易理解,我將每個單元格的文本設定為我分配給它的“CellNumber”。一旦您了解發生了什么,只需將一個空字串""作為初始值即可。
為了更容易找到單元格,有一個Dictionary包含它們。CellNumber(row, column)給key那個字典,找到相應的單元格。
Calculate 按鈕對單元格進行了一些更改,因此您可以了解如何執行此操作。
我建議不要在初始化后添加或洗掉單元格(網格的孩子)。相反,顯示或隱藏單元格。在這里,您可以通過更改Texta 的 來完成此操作Label。對于其他型別的Views,您可能會更改View.IsVisible為 true 或 false。
主頁.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ManipulateGridChildren.MainPage">
<Grid x:Name="Formulas" BackgroundColor="#A0A0A0"
RowSpacing="2" ColumnSpacing="2" Padding="2"
ColumnDefinitions="*,*,*,*,*,*,*,*"
RowDefinitions="40,40,40,Auto">
<Button Grid.Row="3" Text="Calc" Command="{Binding CalcCommand}" />
</Grid>
</ContentPage>
MainPage.xaml.cs:
using System.Collections.Generic;
using Xamarin.Forms;
namespace ManipulateGridChildren
{
public partial class MainPage : ContentPage
{
const int NRows = 3;
static int NColumns = 8;
const int MaxColumns = 100;
private Dictionary<int, Label> cells = new Dictionary<int, Label>();
public MainPage()
{
InitializeComponent();
AddCells();
CalcCommand = new Command(Calculate);
BindingContext = this;
}
public Command CalcCommand { get; set; }
private void Calculate()
{
SetCellText(0, 0, "A");
SetCellText(0, 2, "B");
SetCellText(1, 0, "C");
SetCellText(2, 3, "D");
SetCellBackground(2, 3, Color.Pink)
}
private void SetCellText(int row, int column, string text)
{
cells[CellNumber(row, column)].Text = text;
}
private void SetCellBackground(int row, int column, Color color)
{
cells[CellNumber(row, column)].BackgroundColor = color;
}
private void AddCells()
{
// Grid rows and columns are numbered from "0".
for (int row = 0; row < NRows; row ) {
for (int column = 0; column < NColumns; column ) {
var cell = AddCell(row, column);
cells[CellNumber(row, column)] = cell;
}
}
// Add "frames" around some rows.
// AFTER adding the cells, so drawn on top.
AddFrame(0, 1, 0, NColumns);
AddFrame(1, 2, 0, NColumns);
}
private Label AddCell(int row, int column)
{
var cell = new Label();
// For debugging - show where each cell is.
// Once you understand, change this to an empty string.
cell.Text = $"{CellNumber(row, column)}";
cell.BackgroundColor = Color.White;
cell.HorizontalTextAlignment = TextAlignment.Center;
cell.VerticalTextAlignment = TextAlignment.Center;
Formulas.Children.Add(cell, column, row);
return cell;
}
// Assign unique number to each cell.
private int CellNumber(int row, int column)
{
return row * MaxColumns column;
}
private void AddFrame(int row, int rowSpan, int column, int columnSpan)
{
var rect = new Xamarin.Forms.Shapes.Rectangle {
Stroke = new SolidColorBrush(Color.Black),
StrokeThickness = 2
};
Formulas.Children.Add(rect, column, column columnSpan, row, row rowSpan);
}
}
}
uj5u.com熱心網友回復:
對您要實作的目標有些困惑,但這里有一個建議。
我建議采用 MVVM 模式。在代碼中操作 UI 并不是最好的主意。
<ContentPage.BindingContext>
<local: MainViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<ListView ItemSource={Binding ListOfItems}>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text={Binding .}/>
<ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
主視圖模型.cs
public class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<string> ListOfItems {get;set;} = new ObservableCollection<string>();
public void DeleteAnItem(string s)
{
ListOfItems.Remove(s);
}
public void AddItem(string s)
{
ListOfItems.Add(s);
}
public void RemoveAtIndex(int i)
{
ListOfItems.RemoveAt(i);
}
#region TheRestOfPropertyChanged
...
#endregion
}
您對串列所做的每項更改都將在您的 UI 中可見。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371243.html
標籤:C# 沙马林 xamarin.forms 动态的 网格
