Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.forms ViewModel中的命令_Xamarin.forms - Fatal编程技术网

Xamarin.forms ViewModel中的命令

Xamarin.forms ViewModel中的命令,xamarin.forms,Xamarin.forms,我试图在互联网上找到一个解决方案,但我所发现的并没有真正回答我的问题,因为我是初学者,所以对我来说它似乎相当先进 我有一个页面,有10个按钮,每个按钮执行我正在使用的不同操作。然而,我现在在我的构造函数中有了它,因为我还有5个按钮,所以我的构造函数中还有5个命令,我感觉这是不对的。。如果我的构造函数中没有这些疯狂的东西,有什么方法可以做到这一点吗 public DictionaryViewModel() : base(listenCultureChanges: true

我试图在互联网上找到一个解决方案,但我所发现的并没有真正回答我的问题,因为我是初学者,所以对我来说它似乎相当先进

我有一个页面,有10个按钮,每个按钮执行我正在使用的不同操作。然而,我现在在我的构造函数中有了它,因为我还有5个按钮,所以我的构造函数中还有5个命令,我感觉这是不对的。。如果我的构造函数中没有这些疯狂的东西,有什么方法可以做到这一点吗

 public DictionaryViewModel()
            : base(listenCultureChanges: true)
        {
            Task.Run(async () => await LoadAllDataForDictionary()).Wait();
            PlayPauseCommand = new Command(() => StartOrStopPlaying());
            HideLabelsCommand = new Command(() => HideLabels());
            MakeWebViewLargeEnCommand = new Command(() => MakeWebViewLargeEN());
            MakeWebViewLargeCzCommand = new Command(() => MakeWebViewLargeCZ());
            RemoveWordFromUserDictionaryCommand = new Command(() => RemoveWordFromUserDictionary());
        }

如果在构造器中放入10个命令,这是可以的

但是,如果您将数据从SQLite加载到ViewModel中,您应该在出现的
on
bindingContext
。例如,您使用了
Task.Run(async()=>await LoadAllDataForDictionary()).Wait()
在构造函数中,应该在ViewModel中创建一个方法,如以下代码所示

 public void PopulateCollection()
        {
            Task.Run(async () => await LoadAllDataForDictionary()).Wait();
        }
然后在页面背景代码中的appearing方法中加载数据

           protected override async void OnAppearing()
            {
                base.OnAppearing();

                var pageVm = this.BindingContext as MyPageVm;

                if (pageVm != null)
                {
                    pageVm.PopulateCollection();
                }
            }

如果在构造器中放入10个命令,这是可以的

但是,如果您将数据从SQLite加载到ViewModel中,您应该在出现的
on
bindingContext
。例如,您使用了
Task.Run(async()=>await LoadAllDataForDictionary()).Wait()
在构造函数中,应该在ViewModel中创建一个方法,如以下代码所示

 public void PopulateCollection()
        {
            Task.Run(async () => await LoadAllDataForDictionary()).Wait();
        }
然后在页面背景代码中的appearing方法中加载数据

           protected override async void OnAppearing()
            {
                base.OnAppearing();

                var pageVm = this.BindingContext as MyPageVm;

                if (pageVm != null)
                {
                    pageVm.PopulateCollection();
                }
            }

把这个放在一个方法中,然后调用那个方法怎么样?可以对所有按钮使用一个命令。但您必须使用命令参数才能知道要在viewmodel中执行的确切操作。每个命令参数都有唯一的值,您可以在viewmodel中获得该值,然后执行相应的action@MarcelCallo好的,我想这是一个有点高级的水平。我会试试的,非常感谢。你把这个放在一个方法中并调用那个方法怎么样?可以对所有按钮使用一个命令。但您必须使用命令参数才能知道要在viewmodel中执行的确切操作。每个命令参数都有唯一的值,您可以在viewmodel中获得该值,然后执行相应的action@MarcelCallo好的,我想这是一个有点高级的水平。我会尝试,非常感谢。这其中有一个巨大的缺陷,例如,每当用户返回此页面时,SQLite DB将再次被调用以重新获取已经存在的数据,或者如果用户在后台返回,例如,这是一个巨大的缺陷,每当用户返回此页面时,SQLite DB将再次被调用,以重新获取已经存在的数据,或者如果用户在后台返回,例如