Xamarin Forms 无法识别 x:name"lvwPrice"

Xamarin Forms not recognising x:name"lvwPrice"

我已经为这个问题苦恼了一段时间,我似乎不明白为什么它一直在说

"lvwPrice does not exist in this current context"

我的Xaml代码:

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="ProjectCrypto.Views.Overview">
    <ContentPage.Content>
        <ListView x:Name="lvwOverview" RowHeight="100">
            <ListView.ItemTemplate ItemsSource="{Binding Coin}">
                <DataTemplate>
                    <ViewCell>
                        <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            
                            <Label Grid.Column="1" Text="{Binding Key}" VerticalOptions="Start" />
                            <Label x:Name="lvwPrice" Grid.Column="2" Text="test" VerticalOptions="Center"/>
                            <Label Grid.Column="3" Text=">" HorizontalOptions="End" Margin="0,0,16,0" VerticalOptions="Center" TextColor="Black" FontAttributes="Bold"/>
                            
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

我的xaml.cs代码:

using System.Collections.Generic;
using ProjectCrypto.Repositories;
using Xamarin.Forms;

namespace ProjectCrypto.Views
{
    public partial class Overview : ContentPage
    {
        public Overview()
        {
            InitializeComponent();
            LoadData();
        }
        private async void LoadData()
        {
            lvwOverview.ItemsSource = await CryptoRepository.GetCoins();

            var Coin = await CryptoRepository.GetCoins();

            foreach (var item in Coin)
            {
                lvwPrice.ItemsSource = await CryptoRepository.GetPrice(item.Key);
            }
            
        }
    }
}

有谁能帮我弄清楚为什么它不想检测 lvwPrice

为了能够按名称和您喜欢的任何方式访问模板化元素,请创建一个自定义视图,将您用作单元格。在单元格的 code-behind 中,您将能够通过名称访问所有内容。您的初始 parent 列表现在如下所示:

     <ListView x:Name="lvwOverview" RowHeight="100">
        <ListView.ItemTemplate ItemsSource="{Binding Coin}">
              <DataTemplate>
                 <user:MyViewCell/>
              </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

您的全新手机将如下所示:

XAML:

  <?xml version="1.0" encoding="UTF-8"?>
               <ViewCell>
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        
                        <Label Grid.Column="1" Text="{Binding Key}" VerticalOptions="Start" />
                        <Label x:Name="lvwPrice" Grid.Column="2" Text="test" VerticalOptions="Center"/>
                        <Label Grid.Column="3" Text=">" HorizontalOptions="End" Margin="0,0,16,0" VerticalOptions="Center" TextColor="Black" FontAttributes="Bold"/>
                        
                    </Grid>
                </ViewCell>

祝你好运:)