首页 > 解决方案 > 期待 xamarin Box-view 颜色动态变化

问题描述

我已经将 box clour 与值(在视图模型中)和 xaml 中的值绑定,如下所示在 xaml

<BoxView  Grid.Row="2" Color="{Binding ParentContext.BoColor,Source={x:Reference dashboardThree}}" />

在模型视图中如下

   public string BoColor { get; set; } = "#000080";

所以上面的代码每次应用程序运行时都会显示蓝色,问题是我在列表视图中动态创建了 10 个框,所有 10 个框都显示了上述颜色(因为我只有 1 个列表视图来显示我的 jason对象,它会动态创建 10,15 个等框),那么我可以为每个不同的框添加不同的 10 种颜色(随机或定义的六色)吗?预先感谢您对我们的支持。

标签: c#xamlxamarinxamarin.forms

解决方案


这实际上取决于您是否需要这些颜色对于每个 BoxView 都是可重复的。

这是一个随机分配颜色的示例。

Random random = new Random();
public string BoColor
{
    get
    {
        return String.Format("#{0:X6}", random.Next(0x1000000));
    }
}

例子:

<StackLayout>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
</StackLayout>

在此处输入图像描述


推荐阅读