我在这件事上绞尽脑汁已经有一段时间了,我似乎不明白为什么它总是说“lvwPrice在当前环境下不存在”
我的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吗?
为了能够通过名称和您喜欢的任何方式访问模板化的元素,请创建一个自定义视图,该视图将作为单元使用。在手机后面的代码中,你可以通过名字访问所有内容。您的初始父列表现在将如下所示:
<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>
祝你好运:)