android - 动态绑定到 ToolbarItem 图标以从文件切换图像
问题描述
我正在尝试使用带有过滤器图标的 MVVM 构建工具栏。当您单击它时,您会导航到设置过滤器的位置,然后导航回来。如果过滤器对选定的项目处于活动状态,我希望工具栏图标现在是不同的图像。
但是绑定似乎不起作用。每当我将图标设置为“Filter.png”等文件中的图像字符串时,工具栏只会显示文本“FILTER”。当我将绑定到 FileImageSource 的值转换为错误时
那么我该如何正确地做到这一点呢?我已经尝试过使用转换器,但结果相同。我的代码是:
XAML:
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" Icon="{Binding FilterIconActive}" Text="Filter" Priority="0" Command="{Binding FilterCommand}" />
</ContentPage.ToolbarItems>
我的视图模型:
private string _filterIcon;
public string FilterIcon
{
get => _filterIcon;
set => SetProperty(ref _filterIcon, value);
}
FilterIcon = _selectedFilter.Any() ? "FilterDone.png" : "Filter.png";
正如我已经说过的,将 itemtype 从 string 更改为 FileImageSource 因为 Icon 是 MenuItem.Icon 类型也不起作用。多谢你们
解决方案
我看到您FilterIconActive
在更改属性时正在绑定FilterIcon
属性。
我注意到的另一件事是您没有使用INotifyPropertyChanged事件来通知您的属性其值发生了变化。
推荐阅读
- hibernate - 使用 JPA 标准省略不需要的 JOIN
- c# - “程序集加载”只是“读取元数据表”的另一个术语吗?
- maven - 在 azure-webapp-maven-plugin 中配置运行状况检查
- php - caldav php 客户端 - 获得空闲时间
- c# - 使用数据注释将十进制值截断至 2 点
- tensorflow - 保存 TensorFlow 神经网络 KFold 交叉验证模型
- git - 识别所有只有一个主分支的存储库
- mysql - 我可以在 MySQL 中动态/逻辑别名列吗?
- ansible - ansible jinja2 selectattr() 匹配多个值
- elasticsearch - Is mapping property names with dot is allowed in Elasticsearch Index Management?