다:어떻게 제거하는 항목에서 특정 행에서 그리드

0

질문

나는 그리드가 내 제거하려는 항목에서 특정 라인입니다. 그것은 루프에서,그 때가 두 번째 라인을 제거하고,품목에서 두 번째 라인에서 첫 번째 줄 수 있습니다. 는 방법을 조정할 수 있습니만 제거하는 줄을 내가 원하는가?

참고는 무엇을 하려고 해요 아무것도 보이지 않지만,게 첫 번째 줄을 이어 두 번째 해결 pow(b2),그리고 세 번째 줄에 나는 복사 두 번째 라인,하지만 해결 -4*5(-4*a).

이것은 미리 보기다.

이것 은 나의 코드를 팽창시키는 이러한 줄을 이해하기: 에 들어가 처음에 if(fields == null) 기 때문에 포로,그 후,가 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);
            }
        }
c# dynamic grid xamarin
2021-11-23 23:29:00
2

최고의 응답

1

여기에의 완전한 예제와 함께 작업의 세포니다.

개념은 당신이 처음 만드는 모든 세포입니다. 여기서 사용 Label 에서 각각의 그리드 셀입니다. 그것을 쉽게 이해하고,내가 설정한 각각의 세포의 텍스트를"CellNumber"나는 할당합니다. 면 무슨 일이 일어나고 있는지 이해,그냥 빈 문자열 "" 초기 값입니다.

을 쉽게 찾을 수 있 셀,가 Dictionary 을 포함하는 그들. CellNumber(row, column) 을 제공합 key 하는 사전을 찾기 위해,해당 셀입니다.

Calculate 버튼은 일부 변경세포,그래서 당신은 수행하는 방법을 볼 수 있습니다.

나는 권하지 않을 추가하거나 제거하 세포(어린이의 그리드)한 번이 초기화됩니다. 대신,를 표시하거나 숨깁니다. 여기에서,당신은 이것을 변경하여 TextLabel. 다른 종류의 Views 로 변경할 수 있습니다 View.IsVisible 을 참 또는 거짓입니다.

MainPage.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);
        }
    }
}
2021-11-24 19:14:51

감사합니다,나는 다음에는 아이디어를 만들의 목록은 그리드 및 그냥 보면 그것의 보이거나 없습니다.
Que44
0

조금 무엇을 달성하기 위해 노력하고,하지만 여기에 실행하는 무제.

는 것을 추천한된 이 패턴이다. 조작 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>

MainViewModel.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
}

의 모든 변경 할 목록 될 것입니다 visibile 에서 대부분의 작업을 수행합니다.

2021-11-24 19:06:24

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................