Xamarin Android 进度条

Xamarin Android ProgressBar

我很难在 Xamarin 的 MVVMCross 结构中使用 ProgressBar Android。最初,我想将 ProgressBar 的可见性绑定到我的 ViewModel 中的一个字段。事实证明这很困难,我尝试了几种排列并尝试了这里的建议:

我确实有其他元素,例如按钮,我已经能够在项目的其他地方成功绑定,所以我不确定为什么这是一个如此大的问题。经过多次尝试后,我决定尝试在 ViewModel 中实用地设置可见性。我尝试了几种方法,但我最终认为肯定行不通。

Xaml:

<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 文档。非常感谢任何帮助或指点。

谢谢。

原来问题是链接器删除了可见性 属性。我从这个post得到了这个想法:Visibility binding fails

我在 LinkerPleaseInclude 中添加了以下代码:

public static void Include(ProgressBar progressBar)
{
    progressBar.Click += (s, e) => progressBar.Visibility = progressBar.Visibility - 1;
}

如果有人好奇我是怎么做到的,下面是代码:

Xaml:

<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 class。