Xamarin: कैसे को दूर करने के लिए एक आइटम में एक विशिष्ट पंक्ति ग्रिड में

0

सवाल

मैं एक ग्रिड है कि मैं कोशिश कर रहा हूँ करने के लिए किसी आइटम को हटाने में विशिष्ट लाइन है । यह एक पाश में, तो जब यह हो जाता है, दूसरी पंक्ति के लिए, इसे हटा मद में दूसरी पंक्ति और यह भी पहली पंक्ति में. कैसे मैं समायोजित कर सकते हैं दूर करने के लिए ही लाइन है कि मैं करना चाहते हैं?

ध्यान दें कि मैं क्या कर रहा हूँ करने की कोशिश कर रहा है, नकल करने के लिए पहली पंक्ति के दूसरे ही हल पाउ (बी 2), और तीसरी पंक्ति में मैं नकल करेंगे दूसरी लाइन है, लेकिन हल -4 * 5 (-4 * एक).

इस का पूर्वावलोकन है क्या हो रहा है.

यह है मेरे कोड के लिए फुलाना इन पंक्तियों को समझने के लिए: यह पहली बार में में प्रवेश करती है 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 बटन कुछ परिवर्तन करने के लिए कोशिकाओं है, तो आप देख सकते हैं कि कैसे करना है.

मैं की सिफारिश नहीं जोड़ने या हटाने कोशिकाओं (बच्चों के लिए ग्रिड) एक बार जब यह प्रारंभ किया है. इसके बजाय, दिखाने या छिपाने के लिए एक सेल. यहाँ, आप ऐसा करते हैं बदलने के द्वारा Text के एक Label. के अन्य प्रकारों के लिए Viewएस, आप बदल सकता है 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.सीएस:

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

थोड़ा उलझन में क्या आप की कोशिश कर रहे हैं को प्राप्त करने के लिए है, लेकिन यहाँ एक sugestion.

मैं जा रहा करने की अनुशंसा के लिए एक MVVM पैटर्न. छेड़छाड़ यूआई कोड में सबसे अच्छा विचार नहीं है.

<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.सीएस

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ý
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में