【问题标题】:MvvmCross Visibility plugin does not hide my TextViewMvvmCross Visibility 插件不会隐藏我的 TextView
【发布时间】:2019-06-28 03:29:05
【问题描述】:

我正在尝试使用 MvvmCross Visibility 插件使项目不可见。我正在使用 MvvmCross 5.7 和 MvvmCross Visibility 插件 5.7。

我尝试通过布局和瑞士绑定来绑定对象的可见性。

我有一个 ViewModel,它有一个适当的 ShouldShowBackButton:

  public class TabViewModel : MvxViewModel
    {
        private bool _showBackButton;

        public IMvxCommand NavigateCommand => new MvxCommand(this.Act, this.CanAct);

        public bool ShouldShowBackButton
        {
            get => this._showBackButton;

            set
            {
                this._showBackButton = value;
                this.RaisePropertyChanged(() => this.ShouldShowBackButton);
            }
        }

        private void Act()
        {
            this.ShowViewModel<ProfileFragmentViewModel>();
        }

        private bool CanAct()
        {
            return true;
        }
    }

这个 ViewModel 继承自实际的 ViewModel:

 public class ProfileFragmentViewModel : TabViewModel
    {
        public ProfileFragmentViewModel()
        {
            this.ShouldShowBackButton = false;
        }
    }

viewModel 已正确绑定到 View,我已经对其进行了测试。 这是我试图使 TextView 不可见的方式:

    <TextView 
        android:id="@+id/testProp"
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:text="Test visibility"
        local:MvxBind="Visibility Visibility(ShouldShowBackButton)" />

还有瑞士法典:

 public class ProfileFragmentView : MvxFragment
    {
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var view = this.BindingInflate(Resource.Layout.ProfileFragmentView, null);
            var backButton = view.FindViewById<Button>(Resource.Id.toolbarBackButton);
            var testButton = view.FindViewById<TextView>(Resource.Id.testProp);
            var set = this.CreateBindingSet<ProfileFragmentView, ProfileFragmentViewModel>();

            set.Bind(testButton).For(v => v.Visibility).To(vm => vm.ShouldShowBackButton)
                .WithConversion<MvxVisibilityValueConverter>();

            set.Apply();
            return view;
        }
    }

这两种方法都不会使我的 TextView 不可见。

【问题讨论】:

  • 你能发布你的整个 ProfileFragmentView 布局文件吗?

标签: c# xamarin xamarin.android mvvmcross


【解决方案1】:

你应该将你的属性声明为响应式

[Reactive]
public bool ShouldShowBackButton
    {
        get => this._showBackButton;
       ....

【讨论】:

  • 找不到这个属性,可能是来自其他版本的MvvmCross。
  • @Kam 我的错,我总是将 MvvmCross 与 ReactiveUI 一起使用。注释来自后一个库
  • 问:xmlns:local: 是否指向""schemas.android.com/apk/res-auto""?
  • @Kam,@MihaiBC 刚刚问的解决方案是什么?添加xmlns:local="http://schemas.android.com/apk/res-auto"时无法重现您的问题。
  • 这是我的一个问题的解决方案。这个问题的主要解决方案是,而不是“local:MvxBind="Visibility Visibility(ShouldShowBackButton)””我使用“local:MvxBind="Visible ShouldShowBackButton"”
猜你喜欢
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
相关资源
最近更新 更多