gpt4 book ai didi

xamarin - 我应该将代码放在 viewModel 中的按钮单击后面吗?如果是,我该如何绑定(bind)它?

转载 作者:行者123 更新时间:2023-12-02 14:51:23 25 4
gpt4 key购买 nike

我的 XAML 代码如下所示:

<Button Grid.Column="0" x:Name="nButton" Text="Don't Know" />

和我链接的 viewModel:

public CardViewModel card = new CardViewModel();
phraseGrid.BindingContext = card;

到目前为止,ViewModel 看起来像这样:

public class CardViewModel : ObservableProperty
{
string cvmLabel;

public CardViewModel() { }

public string CvmLabel
{
get { return cvmLabel; }
set {
if (value != cvmLabel) {
cvmLabel = value;
NotifyPropertyChanged("CvmLabel");
}
}
}
...
...

目前我已经在后端 C# 中对此进行了编码。

        nButton.Clicked += (sender, e) =>
{
App.DB.IncrementScore(App.cfs, phrase, 1);
App.correctButtonPressed = 1;
ResetTimer2();
};

我想知道的是这在 viewModel 中是否更合适,然后如果我把它放在那里,最好的方法是如何编码并链接按钮的绑定(bind)。

最佳答案

是的,在 View 模型中实现所有逻辑要好得多。在后面的代码中处理单击事件会将您的 View 与后面的代码结合起来,并使单元测试变得更加困难。总的来说,尽可能减少耦合总是最好的。

您应该使用按钮的 Command 参数来指向 View 模型中的 ICommand,如下所示:

XAML

<Button Grid.Column="0" x:Name="nButton" Text="Don't Know" Command="{Binding ButtonClickedCommand}" />

View 模型

public ICommand ButtonClickedCommand {get; set;}

public CardViewModel()
{
var ButtonClickedCommand = new Command (() => Debug.WriteLine ("Command executed"));
}

关于xamarin - 我应该将代码放在 viewModel 中的按钮单击后面吗?如果是,我该如何绑定(bind)它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46033414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com