【问题标题】:Xamarin Listview binding with object properties not workingXamarin Listview 绑定与对象属性不起作用
【发布时间】:2020-11-26 01:45:47
【问题描述】:

我正在使用:

  • VS 2019
  • Xamarin 16.1.0.545
  • Xamarin 表单 4.1.0

我有一个 Xamarin Android 项目在更新 VS 之前和之后都在工作,但 ListView 绑定停止工作。

我有一个包含此列表视图的内容页面:

<ListView x:Name="ListViewServicos" SelectionMode="None" ItemTapped="ListViewServicos_ItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell Height="80">
                <StackLayout Orientation="Vertical" Padding="10" Spacing="2" HeightRequest="80">
                    <StackLayout Orientation="Horizontal" VerticalOptions="StartAndExpand">
                        <Image Source="arrow" HorizontalOptions="Start"></Image>
                        <Label Text="{Binding Tipo}" TextColor="Black" HorizontalOptions="StartAndExpand"/>
                        <StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand" WidthRequest="100">
                            <Label Text="{Binding MargemTotalFormatada}" TextColor="#555555" HorizontalOptions="EndAndExpand"/>
                        </StackLayout>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal" VerticalOptions="StartAndExpand">
                        <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
                            <Label Text="Uso:" TextColor="#555555" HorizontalOptions="StartAndExpand"/>
                            <Label Text="{Binding MargemUtilizadaFormatada}" TextColor="#555555" HorizontalOptions="EndAndExpand"/>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand">
                            <Label Text="Disponível:" TextColor="#555555" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/>
                            <Label Text="{Binding MargemFormatada}" TextColor="#555555" HorizontalOptions="EndAndExpand" FontAttributes="Bold"/>
                        </StackLayout>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我在 .cs 中这样设置 ItemsSource(在 OnAppearing 内部调用的 LoadServicesList 方法中):

protected override void OnAppearing()
{
    base.OnAppearing();
    BindingContext = this;
    ConfigureScreen();
    LoadServicesList();
    servico = User.Servicos.First();
    LoadService(servico);
}

public void LoadServicesList()
{
    ListViewServicos.RowHeight = 80;
    ListViewServicos.HeightRequest = User.Servicos.Count() * 80;
    ListViewServicos.ItemsSource = User.Servicos;
}

用户是静态的,“Servicos”是“Servico”的列表:

public static class User
    {
        public static List<Servico> Servicos { get; set; }

这里是“Servico”类:

public class Servico
{
    public string Tipo { get; set; }
    public double Margem { get; set; }

    public string MargemFormatada { get; set; }
        
    public double MargemUtilizada { get; set; }

    public string MargemUtilizadaFormatada { get; set; }

    public string MargemTotalFormatada { get; set; }

    public string Color { get; set; }

    public List<Margem> Margens { get; set; }

    public Servico(string tipo, double margem, double margemUtilizada)
    {
        this.Tipo = tipo;
        Margem = margem;
        MargemUtilizada = margemUtilizada;
        MargemFormatada = string.Format(new CultureInfo("pt-BR"), "{0:C}", margem);
        MargemUtilizadaFormatada = string.Format(new CultureInfo("pt-BR"), "{0:C}", margemUtilizada);
        MargemTotalFormatada = string.Format(new CultureInfo("pt-BR"), "{0:C}", margem + margemUtilizada);
    }
}

所有这一切中最奇怪的部分是只呈现了“Tipo”属性,而其他绑定却没有显示(是的,我已经检查过它们有值)。

我一直在寻找解决方案很长时间,但到目前为止对我没有任何帮助。如果有人可以帮助我,我真的很感激。谢谢!

Ps:我已经尝试添加“BindingContext = this;”在 InitializeComponent() 之后没有任何改变。

更新:

我刚刚发现只有 Servico 的“Tipo”属性有效,因为在代码的其他部分我调用了 User.Servicos.First().Tipo。如果之前调用了属性“get”,它看起来只会显示在 listview 绑定中。太奇怪了。

所以解决方法是遍历 User.Servicos 并调用我将在 Listview 中使用的所有属性:

string x = servico.MargemTotalFormatada;
string y = servico.MargemUtilizadaFormatada;
string w = servico.MargemFormatada;

使用该代码,ListView 绑定工作。这是超级丑陋和错误的,但这就是我让它发挥作用的方式。

【问题讨论】:

  • 请不要将代码和错误发布为图片!无法读取/复制/粘贴/索引,因此很难为您提供帮助
  • 谢谢@Jason,刚刚更新并正确添加了代码。

标签: visual-studio xamarin xamarin.forms xamarin.android


【解决方案1】:

1.将您的List&lt;Servico&gt; Servicos 更改为ObservableCollection&lt;Servico&gt; Servicos

public static class User
{
    public static ObservableCollection<Servico> Servicos { get; set; }
}

2.在其他方法之后调用LoadServicesList

    protected override void OnAppearing()
    {
        base.OnAppearing();
        BindingContext = this;
        ConfigureScreen();
        servico = User.Servicos.First();
        LoadService(servico);

        LoadServicesList();

    }

参考:listview/data-and-databinding

更新:

这里的示例项目:data-binding-of-Listview

【讨论】:

  • 感谢您的回答。但是,这对我没有帮助,当我使用 ObservableCollection 时没有任何变化。
  • @tksask 我上传了一个我测试的示例项目,请检查。
  • 我更新了我的问题,展示了我是如何让它工作的。这是一个 hack,我不明白发生了什么,而且非常奇怪。
  • 如果你能分享一个最小的、可重现的例子,我会研究一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 2021-07-21
  • 2020-08-29
  • 1970-01-01
  • 2018-09-16
相关资源
最近更新 更多