xamarin 如何在集合视图中绑定渐变颜色

tjjdgumg  于 2022-12-31  发布在  其他
关注(0)|答案(1)|浏览(150)

我必须在一个网格集合视图中绑定渐变颜色。我试过使用xmal.cs side,但它不起作用。

foreach(var item in abc)
                {
                    if ((item.index % 2) == 0)
                    {
                        GradientStopCollection gradientStops = new GradientStopCollection();
                        gradientStops.Add(new GradientStop() { Color = Color.FromHex("#840CA3"), Offset = (float)0.1 });
                        gradientStops.Add(new GradientStop() { Color = Color.FromHex("#F4B6BF"), Offset = (float)1.0 });
                        LinearGradientBrush linearGradientBrush = new LinearGradientBrush()
                        {
                            EndPoint = new Point(0, 1),
                            GradientStops = gradientStops
                        };
                        item.BackgroundColor = linearGradientBrush;
    
                    }
                    else
                    {
                        GradientStopCollection gradientStops = new GradientStopCollection();
                        gradientStops.Add(new GradientStop() { Color = Color.FromHex("#A30C42"), Offset = (float)0.1 });
                        gradientStops.Add(new GradientStop() { Color = Color.FromHex("#F4B6BF"), Offset = (float)1.0 });
                        LinearGradientBrush linearGradientBrush = new LinearGradientBrush()
                        {
                            EndPoint = new Point(0, 1),
                            GradientStops = gradientStops
                        };
                        item.BackgroundColor = linearGradientBrush;
                    }
                }
MycollectionView.itemsource = abc;
5cnsuln7

5cnsuln71#

快速解答:您需要背景,而不是背景颜色。
实际上,你需要做的是:
避免这种糟糕的设计。你需要使用绑定,而不是添加可视化元素,你需要使用数据触发器,样式和项模板。
你操纵你的模型数据,你不应该迭代和分配你的视觉元素的颜色。
花几天时间学习如何正确设计应用程序,这将为您节省数年的时间来解决问题。

相关问题