简体   繁体   English

C#中Class属性的最佳语法

[英]Best syntax for Properties in Class in c#

I am new to C# programming. 我是C#编程的新手。 I am building some classes and finding different samples in tutorials. 我正在构建一些类,并在教程中找到不同的示例。 I also believe that in order to keep a WPF form displaying the correct values you need to impliment INotify. 我还相信,为了保持WPF表单显示正确的值,您需要隐含INotify。

So my question is two parts I guess. 所以我的问题是两部分。 Do I really need I notify and a custom method that fires when a property changes? 我是否真的需要通知我,并在属性更改时触发一个自定义方法?

Is this the best systax to define my class? 这是定义我的课程的最佳系统税吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;

namespace LakesideQTS
{
    public class Machine : BaseModel, IDataErrorInfo
    {
    private int _MachineID; 
    private string _Description;

    public int MachineID 
    {
        get { return _MachineID; }
        set { _MachineID = value; OnPropertyChanged1("MachineID"); }
    }
    public string Description 
    {
        get { return _Description; }
        set { _Description = value; OnPropertyChanged1("Description"); }
    }
    }
}

It seems like alot of typing and really long and messy. 看起来好像打字很多,而且又长又乱。 Is there a way to call the OnPropertyChanged1() with the newer syntax style? 有没有一种方法可以使用较新的语法样式来调用OnPropertyChanged1()? Something like this: 像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;

namespace LakesideQTS
{
    public class Machine : BaseModel, IDataErrorInfo
    {

    public int MachineID {get;set; OnPropertyChanged1("MachineID");}
    public string Description {get;set; OnPropertyChanged1("MachineID");)}
    }
}

Thanks for any help you guys and gals can provide! 感谢您提供的任何帮助!

No, there is no way. 不,没有办法。

You either have a barebones property like this: 您要么拥有这样的准系统属性:

public int MachineID { get; set; }

or you have the full fledged property with an explicit backing field: 或者您拥有具有明确后备字段的完整属性:

private int _MachineID;
public int MachineID
{
    get { return _MachineID; }
    set
    {
        _MachineID = value;
        OnNotifyPropertyChanged("MachineID");
    }
}

Only the latter syntax will allow you to do anything more than just storing the value. 只有后一种语法允许您做的事情不仅仅是存储值。

There is no way to write less code to implement it. 无法编写更少的代码来实现它。

To save some time and avoid hand-written code you can use the propfull snippet in Visual Studio. 为了节省时间并避免使用手写代码,可以在Visual Studio中使用propfull代码段。

The usage is: type propfull and press TAB 2 times. 用法是:键入propfull并按TAB 2次。

The main reason to implement INotifyPropertyChanged (probably through your BaseModel class) is for objects where you will use GUI bounded on your model object properties. 实现INotifyPropertyChanged(可能通过BaseModel类)的主要原因是针对将使用在模型对象属性上绑定GUI的对象。 This way GUI controls will update itself when your model object property change. 这样,当您的模型对象属性更改时,GUI控件将自行更新。

The best way I have found the do a property with backing field and Notification with INotifyPropertyChanged was to use Resharper (where you will also have a lots of other useful advantages) and follow Nico receipt . 我发现做一个带有后备字段和带INotifyPropertyChanged的Notification的属性的最好方法是使用Resharper(在这里您还将有很多其他有用的优点)并遵循Nico receive You will save a lots of time and effort of repetitive job if you follow that. 如果您这样做,将节省大量的时间和重复性工作。 It require some time to setup, but you will save a lots more after. 它需要一些时间来设置,但是之后您将节省更多。 Good luck! 祝好运!

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

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