Home > Software engineering >  Get the name of a element (.Net MAUI / Xamarin.Forms)
Get the name of a element (.Net MAUI / Xamarin.Forms)

Time:11-08

Hey i looking for a option to get the Element of a Control by finding the Parent. Does this function exist in Xamarin.Forms / .Net MAUI?

(Parents are GRID and Childrens are Frames)

It needs to be find the current Parent and remove the children. After that it should add the diced numbers the old Parent (name is in numbers) and Add to the children to new calculated Parent.

Example Player1 is on F4 and diced 10. Player1 should be removed from F4 and replaced on F14.

This all can be done with long redundant code but in WinForms you can easily find out everything you need. .Net MAUI doesn't look that familiar to me.

string field = "F";
int diced = 10; 
private void AddPlayer1(int diced)
{
    // What i want
    field = field   diced.ToString();
    field.Children.Add(player1)

    // what i have
    F1.Children.Add(player1);
}

Element parent;
private void RemovePlayer1()
{
    // What i want
    parent = GetParent(player1);
    parent.Children.Remove(player1);


    // what i have (long and redundant)
    if (player1.Parent == F0)
        F0.Children.Remove(player1);
    else if (player1.Parent == F1)
        F1.Children.Remove(player1);
    else if (player1.Parent == F2)
        F2.Children.Remove(player1);
    else if (player1.Parent == F3)
        F3.Children.Remove(player1);
    else if (player1.Parent == F4)
        F4.Children.Remove(player1);
    else if (player1.Parent == F5)
        F5.Children.Remove(player1);
    else if (player1.Parent == F6)
        F6.Children.Remove(player1);
    else if (player1.Parent == F7)
        F7.Children.Remove(player1);
    else if (player1.Parent == F8)
        F8.Children.Remove(player1);
    else if (player1.Parent == F9)
        F9.Children.Remove(player1);
    else if (player1.Parent == F10)
        F10.Children.Remove(player1);
    else if (player1.Parent == F11)
        F11.Children.Remove(player1);
    else if (player1.Parent == F12)
        F12.Children.Remove(player1);
    else if (player1.Parent == F13)
        F13.Children.Remove(player1);
    else if (player1.Parent == F14)
        F14.Children.Remove(player1);
    else if (player1.Parent == F15)
        F15.Children.Remove(player1);
    else if (player1.Parent == F16)
        F16.Children.Remove(player1);
    else if (player1.Parent == F17)
        F17.Children.Remove(player1);
    else if (player1.Parent == F18)
        F18.Children.Remove(player1);
}

CodePudding user response:

My Solution:

Here i can get the Parent via FindByName.

    //Declare
    Random dice = new Random();
    string field = "F";
    int current = 0;
    int diced;

    //Dice
    diced = dice.Next(1,7);  

    //FindByName
    Grid FRemove = FindByName(field   current.ToString()) as Grid; 
    Grid FAdd = FindByName(field   diced.ToString()) as Grid;

    //Remove and Add
    FRemove.Children.Remove(player1);
    FAdd.Children.Add(player1);

CodePudding user response:

If Parent is Grid and Children is frame then in this scenario you can get the children by specifying the row and column of Grid.

Sample Code:

var parent = grid.Children;
//loop through your rowNumber and columnNumber such as 1,1/1,2....
var children = parent .Children.Where(c => Grid.GetRow(c) == rowNumber && Grid.GetColumn(c)== columnNumber);
//Similarly you can find the children and replace by specifying the row and column
  • Related