从列表视图xamarin中的标签获取值

tnkciper  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(133)

我从firebase实时数据库中获取一个id密钥,然后将其绑定到listview中作为label.text
我想做的是将这个idkey作为字符串变量分配给label.text,并在将来的XML.CS页面中使用这个字符串变量。
尝试将其指定为字符串时,无法识别标签的x:name
XML代码

<StackLayout>    
                <ListView x:Name="xListView" ItemsSource="{Binding xlist}" HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>

                            <StackLayout Orientation="Horizontal" Padding="5">
                                
                                <StackLayout HorizontalOptions="StartAndExpand">
                                   
                                    <Label x:Name="entryvalue" Text="{Binding Id}" FontSize="Medium"/>
                                    
                                </StackLayout>
                               
                            </StackLayout>

                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>

XML CS代码

protected override async void OnAppearing()
        {

             var xlist = await GetAlles();          

             xListView.ItemsSource = xlist;

             //Tries to assing value to string variable

             string getentryvalue = entryvalue.Text;

             }

从XML CS文件内的数据库函数获取全部

public async Task<List<xModel>> GetAlles()
    {
       
        return (await firebaseClient.Child(nameof(xModel)).OnceAsync<xModel>()).Select(item => new xModel
        {
            
            Id = item.Key

        }).ToList();


    }

和类

public class xModel
{
    
    public string Id { get; set; }

}
hivapdat

hivapdat1#

就像评论说的,你把事情复杂化了很多。
您的xlist变量已包含所有ID。
要获得特定的id,您需要做的就是类似于

var id = xlist[0].Id;

为了更深入地回答不适合您的问题,您不能在ListView或CollectionView中通过x:name访问XAML元素。由于每个项都使用相同的模板,当您说entryvalue时,代码如何知道您需要哪个模板?

相关问题