Home > Enterprise >  xamarin forms not recognising x:name"lvwPrice"
xamarin forms not recognising x:name"lvwPrice"

Time:12-20

i've been breaking my head on this for a while now and i can't seem to figure out why it keeps saying "lvwPrice does not exist in this current context"

My Xaml code:

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

My xaml.cs code:

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

Is anyone able to help me out on why it doesnt want to detect lvwPrice?

CodePudding user response:

To be able to access templated elements by name and by whatever way you like, create a custom view that will serve You as a cell. And in the cell's code-behind you will be able to access everything by name. Your initial parent list will now look like:

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

Your brand new cell will look like:

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>

Good Luck :)

  • Related