简体   繁体   English

方法传递ViewModel与传递绑定变量

[英]Method Passing ViewModel vs Passing Bound Variables

I have a ComboBox bound to a ViewModel string Quality_SelectedItem . 我有一个ComboBox绑定到ViewModel string Quality_SelectedItem

And I have a Method named Quality, which inside accesses the value of the SelectedItem in an if statement. 我有一个名为Quality的方法,该方法在if语句中访问SelectedItem的值。

I have two ways of accessing the value, by passing the ViewModel through the Method, or by passing the string Quality_SelectedItem . 我有两种访问值的方法,一种是通过ViewModel方法,或者通过string Quality_SelectedItem

Which way should I be using it and which performs faster? 我应该以哪种方式使用它,并且执行速度更快?


XAML XAML

<ComboBox x:Name="cboQuality" 
          ItemsSource="{Binding Quality_Items}"
          SelectedItem="{Binding Quality_SelectedItem, Mode=TwoWay}"
          HorizontalAlignment="Left" 
          Margin="0,2,0,0" 
          VerticalAlignment="Top" 
          Width="105" 
          Height="22"/>

ViewModel Class ViewModel类

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    private void OnPropertyChanged(string prop)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(prop));
        }
    }

    // Quality Selected Item
    private string _Quality_SelectedItem { get; set; }
    public string Quality_SelectedItem
    {
        get { return _Quality_SelectedItem; }
        set
        {
            if (_Quality_SelectedItem == value)
            {
                return;
            }

            _Quality_SelectedItem = value;
            OnPropertyChanged("Quality_SelectedItem");
        }
    }

    ...

Example 1 - Passing ViewModel 示例1-传递ViewModel

In the Quality Method, I access vm.Quality_SelectedItem directly from the if statement. 在质量方法中,我直接从if语句访问vm.Quality_SelectedItem

public ViewModel vm = new ViewModel();

// MainWindow
public MainWindow() 
{
    InitializeComponent();
    DataContext = vm;

    // Quality Method
    Quality(vm); // <---
}


// Quality Method
public static void Quality(ViewModel vm) 
{
    if (vm.Quality_SelectedItem == "High") 
    {
        // do something
    }
    else if (vm.Quality_SelectedItem == "Low") 
    {
        // do something
    }
}

Example 2 - Passing String SelectedItem 示例2-传递字符串SelectedItem

I pass vm.Quality_SelectedItem through the Quality Method and give it the string name quality . 我通过“质量方法”传递vm.Quality_SelectedItem ,并为其指定string名称quality

public ViewModel vm = new ViewModel();

// MainWindow
public MainWindow() 
{
    InitializeComponent();
    DataContext = vm;

    // Quality Method
    Quality(vm.Quality_SelectedItem); // <---
}


// Quality Method
public static void Quality(string quality) 
{
    if (quality == "High") 
    {
        // do something
    }
    else if (quality == "Low") 
    {
        // do something
    }
}

As a general rule, you should make your code as simple as possible. 通常,您应该使代码尽可能简单。 Remember the KISS principle. 记住KISS原则。 This also plays well with SOLID ("simple" is a good way to achieve Single responsibility and Interface segregation). 这也可以与SOLID配合使用(“简单”是实现单一职责和接口隔离的好方法)。

Avoid reaching into one object to get another. 避免伸手去拿另一个。

If you need only a string value in the method, only pass that string value. 如果在方法中仅需要一个string值,则仅传递该string值。 Don't force your method to dig into object hierarchies and dependencies to get that value. 不要强迫您的方法深入研究对象层次结构和依赖关系以获得该值。

If the method needs to modify a string property value, then pass the object where to modify the property. 如果该方法需要修改 string属性值,则将对象传递到修改属性的位置。

From the performance point of view, you will not notice any change. 从性能的角度来看,您不会注意到任何变化。 Accessing an object by-reference is a very cheap operation. 通过引用访问对象是非常便宜的操作。 (Unless you're implementing loops with billions of iterations.) (除非您要实现数十亿次迭代的循环。)

From the design point of view, keeping the things simple makes your code SOLID and easily allows re-usage. 从设计的角度来看,使事情变得简单可以使您的代码成为SOLID,并轻松地允许重复使用。

It depends on what is //do something . 这取决于//do something

  • If you have to handle/interact your viewmodel -object, then pass viewmodel as parameter 如果必须处理/交互viewmodel -object,则将viewmodel作为参数传递
  • otherwise use a string for less dependencies and possibility of common use. 否则,请使用字符串以减少依赖关系和常见使用可能性。
  • If you have your viewmodel as singleton, then it doesn't matter which way you go. 如果您将视图模型设置为单例,那么走什么路都没关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM