c# - 期待 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 种颜色(随机或定义的六色)吗?预先感谢您对我们的支持。
解决方案
这实际上取决于您是否需要这些颜色对于每个 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>
推荐阅读
- ansible - Ansible 的屏幕命令
- c# - 更改具有 TwoWay 绑定的控制属性
- acumatica - 无法在 Acumatica 中发布单个自定义包
- sql-server - Perl DBI / FreeTDS / SQL-Server:如何插入/更新 BLOB varbinary(max) 数据?
- c++ - 将 c 转换为 legv8 时遇到问题
- powershell - 如何检查特定 OU 的用户权限?
- .net - 抛出异常使用流利的api从组中获取项目
- python - Python会员柜台
- spring-boot - 我们如何传递包含邮递员文件的嵌套请求正文
- loops - 在 Ansible 中从 xml 创建字典