Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindViewById on Xamarin Forms?

I need some way to find View/object by its ID. I heared about FindViewById function, but it's not present in my ContentPage class. Where can I find it?

Context: I have ListView with buttons inside. I don't know how many buttons are there. When user clicks on one of those buttons, I get its ID and store globally. What I want to accomplish is to find this specific button, which id was stored in variable.

<StackLayout x:Name="chooseObjectButtons">
      <ListView x:Name="SlotsList" ItemsSource="{Binding .}" >
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <StackLayout>
                  <Button Text="{Binding Text}" BackgroundColor="Gray" Clicked="SlotChosen" />
                </StackLayout>
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>
like image 387
Piotrek Avatar asked Jun 10 '16 17:06

Piotrek


1 Answers

Change the XAML to:

<ListView ItemsSource="{Binding Slots}" >
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <ViewCell.View>
            <StackLayout>
               <Button Text="{Binding Title}" BackgroundColor="Gray" Clicked="Handle_Clicked" Command="{Binding Select}" />
            </StackLayout>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>  

Handle click:

private Button LastButtonClicked;

void Handle_Clicked(object sender, System.EventArgs e)
{
    if (LastButtonClicked != null)
    {
        // Change background here
    }    
    LastButtonClicked = (Button)sender;
    // Do stuff here.
}

To process the specific command for each button use:

public List<SlotsButtons> Slots
{
    get
    {
        return new List<SlotsButtons>
        {
            new SlotsButtons
            {
                Title = "T1",
                Select = new Command(()=>{
                    // do stuff here when clicked.
                })
            },
            new SlotsButtons
            {
                Title = "T2",
                Select = new Command(()=>{
                    // do stuff here when clicked.
                })
            }
        };
    }
}

NOTE: Initial question answer.

In Xamarin Forms the class ContentPage is a Partial class. One part is automatically generated from XAML and the other represents the code behind. The XAML generated Partial class has the code to find the views by name.

The correct name is FindByName and you should't need to use this in your partial class because it its already made in the generated partial class.

If you want to access a view in your code behind just give it a name in XAML. There is an XAML example:

<Button x:Name="button" ></Button>

And in your code behind you could do something like:

button.BorderWidth = 3;

If you still need to find a view for some reason, do this:

var button = this.FindByName<Button>("button");
like image 54
jzeferino Avatar answered Oct 08 '22 04:10

jzeferino