C# 与xamarin表单中相同内容的代码隐藏相比,MVVM具有巨大的代码

C# 与xamarin表单中相同内容的代码隐藏相比,MVVM具有巨大的代码,c#,xaml,xamarin,mvvm,xamarin.forms,C#,Xaml,Xamarin,Mvvm,Xamarin.forms,我有一种方法可以在每个按钮单击时更改按钮背景色。这一点我可以很容易地从代码隐藏 在每个按钮单击事件中使用下面的方法 void ButtonStyle(Button btn) { foreach (var button in buttons) { if (button.Id == btn.Id) { button.BackgroundColor = Color.Blue; button.BorderC

我有一种方法可以在每个按钮单击时更改按钮
背景色
。这一点我可以很容易地从代码隐藏

在每个按钮单击事件中使用下面的方法

void ButtonStyle(Button btn)
{
    foreach (var button in buttons)
    {
        if (button.Id == btn.Id)
        {
            button.BackgroundColor = Color.Blue;
            button.BorderColor = Color.Red;
            button.TextColor = Color.White;
        }
        else
        {
            button.BackgroundColor = Color.White;
            button.BorderColor = Color.Gray;
            button.TextColor = Color.Blue;
        }
    }
}
但是当我使用MVVM来完成同样的任务时,我有大量的代码来完成

ViewModel类是代码示例代码的一部分

public class AssignmentDetailViewModel : INotifyPropertyChanged
{

    private Color _bgColorBtn1;
    private Color _bgColorBtn2;
    private Color _bgColorBtn3;

    private Color _txtColorBtn1;
    private Color _txtColorBtn2;
    private Color _txtColorBtn3;

    public Color BgColorBtn1
    {
        get { return _bgColorBtn1; }
        set
        {
            if (value == _bgColorBtn1)
                return;
            _bgColorBtn1 = value;
        }
    }
    public Color BgColorBtn2
    {
        get { return _bgColorBtn2; }
        set
        {
            if (value == _bgColorBtn2)
                return;
            _bgColorBtn2 = value;
        }
    }
    public Color BgColorBtn3
    {
        get { return _bgColorBtn3; }
        set
        {
            if (value == _bgColorBtn3)
                return;
            _bgColorBtn3 = value;
        }
    }


    public Color TextColorBtn1
    {
        get { return _txtColorBtn1; }
        set
        {
            if (value == _txtColorBtn1)
                return;
            _txtColorBtn1 = value;
        }
    }

    public Color TextColorBtn2
    {
        get { return _txtColorBtn2; }
        set
        {
            if (value == _txtColorBtn2)
                return;
            _txtColorBtn1 = value;
        }
    }

    public Color TextColorBtn3
    {
        get { return _txtColorBtn3; }
        set
        {
            if (value == _txtColorBtn3)
                return;
            _txtColorBtn1 = value;
        }
    }


    public Command BtnColorsCmd1
    {
        get
        {
            return new Command(() => {

                _bgColorBtn1 = Color.White;
                _txtColorBtn1 = Color.Blue;

            });
        }
    }
    public Command BtnColorsCmd2
    {
        get
        {
            return new Command(() => {

                _bgColorBtn1 = Color.White;
                _txtColorBtn1 = Color.Blue;

            });
        }
    }
    public Command BtnColorsCmd3
    {
        get
        {
            return new Command(() => {

                _bgColorBtn1 = Color.White;
                _txtColorBtn1 = Color.Blue;

            });
        }
    }
}
xaml文件

<StackLayout BackgroundColor="White" Spacing="2.5" VerticalOptions="Start" Padding="7,6,7,3" Orientation="Horizontal" x:Name="stackLayou1">
    <Button HorizontalOptions="FillAndExpand" TextColor="{Binding _txtColorBtn1}" BackgroundColor="{Binding _bgColorBtn1}"  x:Name="button1" Text="Assignment"></Button>
    <Button HorizontalOptions="FillAndExpand" TextColor="{Binding _txtColorBtn2}" BackgroundColor="{Binding _bgColorBtn2}"  x:Name="button2" Text="Content"></Button>
    <Button HorizontalOptions="FillAndExpand" TextColor="{Binding _txtColorBtn3}" BackgroundColor="{Binding _bgColorBtn3}"  x:Name="button3" Text="Review"></Button>
</StackLayout>


有什么方法可以减少此代码或其他方法吗?

MVVM中VM的目标是在您的模型(来自Web API、本地数据库或任何其他)和视图之间执行操作

=>数据绑定以显示模型信息

=>实现进程的命令

在这里,您只执行我认为不应该在ViewModel中执行的UI操作

根据您的需要,您应该:

-在处理选择行为的特定文件中创建控件(ContentView或按钮直接)


-在视图中定义3个实例

除了Rudy Spano给出的答案之外,您还可以使用与此存储库中定义的函数类似的
SetProperty(ref backingField,value)
函数()

当然,您可以省略验证函数和OnChanged操作。因此,您可以减少重复比较,并且每次比较只有一行

不使用按钮,您可以使用
ToggleButton
s并定义一个带有触发器的样式,在该样式中,仅当选择了ToggleButton时,您才可以设置颜色。问题应该在这里,为什么您的命令不在列表、数组或类似内容中?您意识到
BtnColorsCmd2
从1而不是2返回数据?同样适用于
BtnColorsCmd3
。在代码隐藏中,我有条件
btn.Id==button.Id
,但我无法在viewmodel中创建该条件。我没有得到你的最后两点。是否告诉您要将不同的xaml文件和appedn添加到主xaml文件中?其想法是创建一个可重用控件(在单独的文件中),该控件的属性为IsChecked或IsSelected,并使用它避免对所有控件(如ButtonStyle方法)进行迭代。正如所建议的,你们想要实现的是一个切换按钮,你们也可以从互联网上得到一个。这里的例子:如果我没有错,切换按钮总是有两个打开或关闭的特定UI。Thakns为您的答案。我会调查的。我还将添加到大脑转储的集合中。。。MVVM是一种SOC(关注点分离)和可测试性模式。虽然有些人是非纯粹主义者,但也有人会说,如果它直接涉及UI(通过xaml或C#),它可能会进入代码隐藏。在我使用的两个框架中,Prism和Caliburn.Micro在减少总体代码方面都做得非常出色,它们都有INPC实现的变体(两者做的事情完全相同)。。PRISM是大约在21世纪初第一个MVVM/MVP框架之一。每一种都有自己独特的用法。