【发布时间】:2022-01-21 11:17:13
【问题描述】:
我一直在思考这个问题,但我似乎无法弄清楚为什么它一直在说
“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?
【问题讨论】:
-
x:Name="lvwPrice" 。在 ListView 中使用 x:Name 没有意义,因为您可能正在创建视图的多个实例。
-
如何单独更改每个列表项的第二个标签而不是使用 x:name?
-
使用数据绑定。创建一个同时包含 Coin 和 Price 的模型并将您的数据合并到一个列表中,或者在 Coin 上创建一个 Property,它将从其他列表中返回相应的价格。
-
仅供未来读者参考,请参阅 Can't Access x:Name of the list from the code behind in Xamarin... 和许多类似问题,google
site:stackoverflow.com xamarin forms x name not found listview。
标签: c# xamarin xamarin.forms xamarin.android