c# - Xamarin Android 进度条
问题描述
我正在努力在 Xamarin Android 的 MVVMCross 结构中使用 ProgressBar。最初,我想将 ProgressBar 的可见性绑定到我的 ViewModel 中的一个字段。事实证明这很困难,我尝试了几种排列并尝试了这里的建议:How to set visibility for ProgressBar in android MvvmCross Xamarin
我确实有其他元素,例如按钮,我已经能够在我的项目的其他地方成功绑定,所以我不确定为什么这是一个如此大的问题。经过多次尝试,我决定尝试在 ViewModel 中务实地设置 Visibility。我尝试了几种方法,但我最终认为肯定行不通的方法。
xml:
<ProgressBar
android:id="@+id/progressBarMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
style="@android:style/Widget.ProgressBar.Large" />
我尝试过使用和不使用默认可见性。我已经尝试了 local:MvxBind 与几种普通的绑定方法,然后使用自定义转换器或 Visibility 插件。
在我的 Fragment 中,我遗憾地在 ViewModel 中设置了一个 FragmentActivity 属性,只是为了看看我是否可以让它以某种形式工作(OnCreateView):
ViewModel.FragActivity = this.Activity;
ViewModel.Progress = view.FindViewById<Android.Widget.ProgressBar>(Resource.Id.progressBarMap);
请注意,我还尝试将 ProgressBar 设置为名为 Progress 的字段,并在我的 ViewModel 中使用它。
这是我在 ViewModel 中操作 ProgressBar 的地方:
private void AddOfflinePoints()
{
try
{
FragActivity.RunOnUiThread(() =>
{
//I've also tried using the above Progress property. That wasn't null but did not update.
ProgressBar barOfProgression = FragActivity.FindViewById<ProgressBar>(Resource.Id.progressBarMap);
//barOfProgression is null :(
barOfProgression.Visibility = ViewStates.Visible;
barOfProgression.Enabled = true;
});
//Code to run while spinning the progress bar (It is inside Task.Run)
}
catch (Exception)
{
//Exception Handling code...
}
FragActivity.RunOnUiThread(() => Progress.Visibility = ViewStates.Gone);
}
我究竟做错了什么?我在 Google 上尝试了很多排列方式,并查阅了有限的 Xamarin 文档。非常感谢任何帮助或指示。
谢谢你。
解决方案
原来问题是链接器删除了 Visibility 属性。我从这篇文章中得到了这个想法:可见性绑定失败
我在 LinkerPleaseInclude 中添加了以下代码:
public static void Include(ProgressBar progressBar)
{
progressBar.Click += (s, e) => progressBar.Visibility = progressBar.Visibility - 1;
}
如果有人好奇我是怎么做到的,下面是代码:
xml:
<ProgressBar
android:id="@+id/progressBarMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
local:MvxBind="Visibility Visibility(ShowProgress)"
style="@android:style/Widget.ProgressBar.Large" />
视图模型:
private bool _showProgress;
public bool ShowProgress
{
get => _showProgress;
set => SetProperty(ref _showProgress, value);
}
其中 SetProperty 来自 MvxNotifyPropertyChanged 类。
推荐阅读
- python - 创建混合数据类型的 np.void 对象,以在 np.full 中使用
- c# - 如何修复从 DB 格式化日期的错误
- tcl - 两个多边形的交点坐标
- ios - CoreStore 映射关系同时导入唯一对象
- python - Python中的多行正则表达式
- javascript - 为什么 Javascript 不断更改我的数据?
- azure - 使用 powershell 更改 Azure SQL Server 的“允许访问 Azure 服务”设置
- php - Laravel 将 excel 文件导入数据库
- javascript - 如何从 Ionic 4 中的 Native Storage ZoneAwarePromise 获取价值
- swift - 从json字符串保存实体对象的问题-如何从可编码继承?