简体   繁体   English

带有Get / Set属性的C#和Silverlight基本类语法不起作用

[英]C# & Silverlight Basic Class Syntax with Get/Set Properties not working

I have a really, really simple class and I am tried to use the get/set properties but they just aren't working for me... I am sure it is the most obvious thing that I am over looking but I just can't see why they aren't working. 我有一个非常非常简单的类,我尝试使用get / set属性,但它们对我不起作用......我确信这是我看到的最明显的事情,但我只是可以'看看他们为什么不工作。 I have checked out the code that utilizes this class and its fine that I can see. 我已经检查了使用这个类的代码及其可以看到的罚款。

In the main code, if I type 在主代码中,如果我输入

Report r = new Report(); 
string str = "Taco";
r.displayName = str; 

The report is declared alright and everything is set to empty strings or a new list or whatever the parameter's default is. 声明报告正常,一切都设置为空字符串或新列表或参数的默认值。 But every time I ran this the displayName always remained blank after the code finished executing... 但每次运行此代码后,displayName在代码执行完毕后总是保持空白...

so I tried putting a stop point in the Class displayName set property at set {_displayName = displayName;} and the value always passed in (displayName) was an empty string.... even though the string clearly says "Taco" in the main code.... I have no idea but I am sure its right in my face. 所以我尝试在set {_displayName = displayName;}的Class displayName set属性中设置一个停止点,并且总是传入的值(displayName)是一个空字符串....即使字符串清楚地在主要字母中显示“Taco”代码....我不知道,但我确信它在我的脸上。 If you need more code I can provide it... 如果您需要更多代码我可以提供它...

Report r = new Report(); 
string str = "Taco"; 
r.setReportDisplayName(str); 

But for some reason the above works. 但出于某种原因上述工作。

public class Report  
{
    private string _reportPath = string.Empty;
    public string reportPath
    {
        get { return _reportPath; }
        set { _reportPath = reportPath; }
    }

    private string _displayName = string.Empty;
    public string displayName
    {
        get { return _displayName; }
        set { _displayName = displayName; }
    }

    private List<parameter> _parameters = new List<parameter>();
    public List<parameter> parameters
    {
        get { return _parameters; }
        set { _parameters = parameters; }
    }

    public Report() { }
    public Report(string path, string display, List<parameter> param)
    {
        _reportPath = path;
        _displayName = display
        _parameters = param;
    }

    public void setReportDisplayName(string str)
    {
       _displayName = str; 
    }
}

You are defining your properties incorrectly. 您错误地定义了属性。 This should be done as: 这应该做到:

private string _displayName = string.Empty;
public string displayName
{
    get { return _displayName; }
    set { _displayName = value; }
}

That being said, if you are using this for Silverlight, you most likely will want to implement INotifyPropertyChanged . 话虽这么说,如果你将它用于Silverlight,你很可能会想要实现INotifyPropertyChanged Without this, data binding will not reflect changes made in code. 如果没有这个,数据绑定将不会反映代码中所做的更改。

To implement this interface, you'll need to add this implementation. 要实现此接口,您需要添加此实现。 A "standard" way to implement this is via: 实现这一目标的“标准”方式是:

public class Report : INotifyPropertyChanged
{
    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;

    // Raise the PropertyChanged event
    protected void NotifyPropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }  

At this point, you need to define your properties like: 此时,您需要定义您的属性,如:

private string _displayName = string.Empty;
public string DisplayName
{
    get { return _displayName; }
    set 
    { 
        if (_displayName != value)
        {
            _displayName = value; 
            NotifyPropertyChanged("DisplayName");
        }
    }
}

Doing this will allow you to data bind to your "Report" class. 这样做将允许您将数据绑定到“Report”类。 You may also want to consider using ObservableCollection<T> instead of List<T> for any collections you want to use with data binding. 对于要与数据绑定一起使用的任何集合,您可能还需要考虑使用ObservableCollection<T>而不是List<T>

You need to be assigning the value of the special variable value in your sets. 您需要在集合中指定特殊变量值的值 value is what will be holding the (heh) value that was assigned to your property as it is passed into the set. value是将传递给集合时分配给您的属性的(heh)值。

 public string reportPath
    {
        get { return _reportPath; }
        set { _reportPath = value; }
    }

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

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