我已将框颜色与值(在 View 模型中)和 xaml 绑定(bind),如下所示 在 xaml 中
<BoxView Grid.Row="2" Color="{Binding ParentContext.BoColor,Source={x:Reference dashboardThree}}" />
在模型 View 中如下
public string BoColor { get; set; } = "#000080";
所以上面的代码每次运行时都会显示蓝色, 问题是我在 ListView 中动态创建了 10 个框,所有 10 个框都显示了上述颜色(因为我只有 1 个 ListView 来显示我的 jason 对象,它动态创建了 10,15 个等框),所以是他们有什么办法可以为每个不同的盒子添加不同的 10 种颜色(随机或定义的六色)??预先感谢您的支持。
最佳答案
这实际上取决于您是否需要这些颜色对于每个 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>
关于c# - 期待xamarin Box-view颜色动态变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51890102/