Xamarin表单上的FindViewById? [英] FindViewById on Xamarin Forms?

查看:278
本文介绍了Xamarin表单上的FindViewById?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些方法来通过其ID查找视图/对象".我听说过FindViewById函数,但是在ContentPage类中不存在.在哪里可以找到它?

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?

上下文:我有带按钮的ListView.我不知道那里有多少个按钮.当用户单击这些按钮之一时,我将获得其ID并在全球存储.我要完成的是找到这个特定的按钮,其ID存储在变量中.

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>

推荐答案

将XAML更改为:

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

处理点击:

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.
                })
            }
        };
    }
}


注意:最初的问题答案.

在Xamarin Forms中,类ContentPagePartial class. 一部分是从XAML自动生成的,另一部分代表了后面的代码. XAML生成的Partial class具有通过名称查找视图的代码.

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.

正确的名称是 FindByName ,并且您不需要在您的局部类中使用它,因为它已经在生成的局部类中制作了.

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.

如果要访问后面代码中的视图,只需在XAML中为其命名. 有一个XAML示例:

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");

这篇关于Xamarin表单上的FindViewById?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆