Cannot clear GridLayout in Xamarin

camillecamille Member ✭✭

Hello everyone,

I'm creating a grid in xamarin form in which I'm implementing buttons using a list values that a I want to change throught a popup ( using Rg.Plugins.Popup.Services) that open when I'm Clicking on any of those buttons.

The problem is that, the list in which i'm storing my buttons values (FindGridCollectionValue) is updating correctly, but the values displayed on my buttons doesn't change when I'm validating the value on my Popup.

Here is my code (the view)

-The gird implementation:

public AssesmentGridPage()
        {
            _agpm = new AssesmentGridPageModel();
            InitializeComponent();
            BindingContext = _agpm;
            _agpm.NewGridCollectionValue();

            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });

            gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = 183 });
            gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = 183 });
            gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = 183 });

            int ProductIndex = 0;
            for (int rowIndex = 0; rowIndex < 6; rowIndex++)
            {
                for (int columnIndex = 0; columnIndex < 3; columnIndex++)
                {
                    if (ProductIndex >= _agpm.GridCollection.Count)
                    {
                        return;
                    }
                    var grid = _agpm.GridCollection[ProductIndex];
                    GridButton = new Button
                    {
                        ClassId = "" + ProductIndex,
                        Text = grid.Name + "\r\n" + grid.Value,
                        BackgroundColor = Color.FromHex(grid.Color),
                    };
                    int request = grid.Value;
                    GridButton.Clicked += async (object o, EventArgs e) => await PopupNavigation.Instance.PushAsync(new PopupAssementGridPage(request, Convert.ToInt32(ClassId)));
                    gridLayout.Children.Add(GridButton, columnIndex, rowIndex);
                    ProductIndex += 1;
                }
            }
        }

and my pop up page code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PolQual.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Rg.Plugins.Popup.Services;
using System.Text.RegularExpressions;

namespace PolQual.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class PopupAssementGridPage
    {
        private AssesmentGridPageModel _agpm;
        private int indexButton;

        public static bool essai;
        public AssesmentGridPageModel AGPM { get => _agpm; }   

        //constructeur
        public PopupAssementGridPage(int request, int index) 
        {
            _agpm = new AssesmentGridPageModel();
            essai = true;
            indexButton = index;
            InitializeComponent();
            EntryValue.Placeholder = Convert.ToString(request);
        }
        //bouton d'annulation
        private void ReturnToOldPage(object sender, EventArgs e)
        {
            PopupNavigation.Instance.PopAsync(true);
        }
        //bouton de validation
        private void ValidateValue(object sender, EventArgs e)
        {
            int value = Convert.ToInt32(EntryValue.Text);
            _agpm.FindGridCollectionValue(value, indexButton);
            PopupNavigation.Instance.PopAsync(true);
        }
    }
}

and my methods in the view model code :

        public void FindGridCollectionValue(int ButtonValue, int ButtonIndex)
        {
            for (int search = 0; search < ButtonIndex+1; search++)
            {
                int essai = GridCollectionValue[ButtonIndex].Value = ButtonValue;
                GridCollectionValue[ButtonIndex].Value = ButtonValue;
            }
            _agp = new AssesmentGridPage(GridCollectionValue);
        }

finally, the method which is supposed to reload my grid :

        public AssesmentGridPage(ObservableCollection<ViewModels.Grid> UpdateList)
        {
            InitializeComponent();
            BindingContext = _agpm;

            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });
            gridLayout.RowDefinitions.Add(new RowDefinition { Height = 70 });

            gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = 183 });
            gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = 183 });
            gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width = 183 });

            int ProductIndex = 0;
            for (int rowIndex = 0; rowIndex < 6; rowIndex++)
            {
                for (int columnIndex = 0; columnIndex < 3; columnIndex++)
                {
                    if (ProductIndex >= UpdateList.Count)
                    {
                        return;
                    }
                    var grid = UpdateList[ProductIndex];
                    GridButton = new Button
                    {
                        ClassId = "" + ProductIndex,
                        Text = grid.Name + "\r\n" + grid.Value,
                        BackgroundColor = Color.FromHex(grid.Color),
                    };
                    int request = grid.Value;
                    GridButton.Clicked += async (object o, EventArgs e) => await PopupNavigation.Instance.PushAsync(new PopupAssementGridPage(request, Convert.ToInt32(ClassId)));
                    gridLayout.Children.Add(GridButton, columnIndex, rowIndex);
                    ProductIndex += 1;
                }
            }

        }

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    That's all just quick ... whack out a test ... stuff, right?
    I mean you're not really going to use that in a live production app; are you?

  • camillecamille Member ✭✭
    I don't understand how it's a little fast to do tests, I'm not trying to do a test, I'm trying to create a grid in which there are buttons with different nons to which a default value of 0 is associated. When I click on the button a popup appears and you can change the value. This I managed to do, but I can't delete the old grid and display the new one, because yes if you change the value, it creates a new grid with the new value or values linked to the button.
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I was trying to politely hint... that code is something nobody would have in a real life application.

    I honestly think the problems you're running into are a side affect of the old 1996 approach along with really bad coding approach. If you were to take some time to learn XAML and MVVM and other 2019 coding concepts then your entire approach to the app would be different, more modern, and less problematic. In other words, doing it right would cause you to run into far less problems both in creation and long term maintenance.

  • camillecamille Member ✭✭
    Concerning the code it is obvious that only the developers got access to it, but basically I was just asking for help on using the gridLayout.children.clear() property because I don't see how to use it in my case. I just wanted an outside opinion.

    This problem is basic but I can't solve it I went to another forum to see the problems related to gridLayout.children.clear(), but I couldn't find a solution.

    So by any chance do you have an idea to submit it?
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Let's approach it from a different direction

    but the values displayed on my buttons doesn't change when I'm validating the value on my Popup

    Why would they? What mechanism would cause an update to happen? There is no data binding in place. So they all get set one time... And are done. There's no connection or mechanism here to maintain continue updates.

    You can write a mountain of code to keep going through and micromanage every little property of every little control
    ie EntryValue.Placeholder = Convert.ToString(request);
    and marshal all of those calles back to the UI thread and hope nothing gets out of sync. Or write your code the way we do in the 21st century and just use MVVM binding. This is a prime example of why this type of coding approach was abandoned two decades ago along with Windows Forms.

  • camillecamille Member ✭✭

    All right, I see what you want to tell me.

    Concerning the mvvm mechanism it is that I am setting up with my views, views modeln my dao, and my dal.

    I'm not looking to create an update mechanism far from it, I just wanted to know if I could delete my old grid and display the new grid. But under no circumstances do I want to create an update system.

    For the moment I'm trying to manage this with lists and observableCollection. And then I'll implement my dal and dao.

    After that my code may not be correct and in that case I'm ready to listen to your opinion on it and learn. I'm code as I think I think after I say my code is fine I'm training by testing things that's all.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    You want to delete the entire Grid and build a new one.... ever time you have a value update?
    You want to make a bunch of code one way to see if it works - then change it all to MVVM later?

    I'm sorry - I think you and I are hitting a language barrier. I'm having a really hard time following the intention of your posts. I can't tell if its the translation to English that doesn't make sense, or something else.

  • camillecamille Member ✭✭
    edited February 1

    ok I'm going to try to make myself understood, currently I use the mvvm method I have a folder in which there are my views, a second file where there are my model views and then a folder where there is my dal and my dao, maybe I'm doing it wrong, maybe I didn't understand how mvvm works, in my opinion the view model is used to link my dal and dao to my views, in the view files there is mainly xaml or c# code but which is only used to create my views. in my views model I do all my methods that will allow me to do the actions of my views. then in my dal and dao there are all my queries and data from my database.

    That's it, do you understand me?

Sign In or Register to comment.