Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#&;具有Get/Set属性的Silverlight基本类语法不起作用_C#_Class_Properties_Get_Set - Fatal编程技术网

C#&;具有Get/Set属性的Silverlight基本类语法不起作用

C#&;具有Get/Set属性的Silverlight基本类语法不起作用,c#,class,properties,get,set,C#,Class,Properties,Get,Set,我有一个非常非常简单的类,我试图使用get/set属性,但它们对我来说不起作用。。。我相信这是最明显的事情,我看得太多了,但我就是不明白为什么他们不工作。我已经检查了使用这个类的代码,我可以看到它很好 在主代码中,如果我键入 Report r = new Report(); string str = "Taco"; r.displayName = str; 报告被声明为正常,所有内容都被设置为空字符串或新列表,或者参数的默认值。但是每次我运行它时,在代码完成执行后,displayName总

我有一个非常非常简单的类,我试图使用get/set属性,但它们对我来说不起作用。。。我相信这是最明显的事情,我看得太多了,但我就是不明白为什么他们不工作。我已经检查了使用这个类的代码,我可以看到它很好

在主代码中,如果我键入

Report r = new Report(); 
string str = "Taco";
r.displayName = str; 
报告被声明为正常,所有内容都被设置为空字符串或新列表,或者参数的默认值。但是每次我运行它时,在代码完成执行后,displayName总是保持空白

因此,我尝试在类displayName set属性的集合{{u displayName=displayName;}中放置一个停止点,并且始终传入的值(displayName)是一个空字符串。。。。即使字符串在主代码中清楚地显示“Taco”。。。。我不知道,但我确信它就在我面前。如果你需要更多的代码,我可以提供它

Report r = new Report(); 
string str = "Taco"; 
r.setReportDisplayName(str); 
但出于某种原因,上述方法是有效的

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; 
    }
}
公共类报告
{
私有字符串_reportPath=string.Empty;
公共字符串报告路径
{
获取{return\u reportPath;}
设置{u reportPath=reportPath;}
}
私有字符串_displayName=string.Empty;
公共字符串显示名
{
获取{return\u displayName;}
设置{u displayName=displayName;}
}
私有列表_参数=新列表();
公共列表参数
{
获取{返回_参数;}
设置{u parameters=parameters;}
}
公共报告(){}
公共报告(字符串路径、字符串显示、列表参数)
{
_reportPath=path;
_displayName=display
_参数=参数;
}
public void setReportDisplayName(字符串str)
{
_displayName=str;
}
}

您需要为集合中的特殊变量赋值是在将属性传递到集合中时,将保存分配给属性的(heh)值的值

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

您需要为集合中的特殊变量value赋值是在将属性传递到集合中时,将保存分配给属性的(heh)值的值

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

您定义的属性不正确。这应通过以下方式进行:

private string _displayName = string.Empty;
public string displayName
{
    get { return _displayName; }
    set { _displayName = value; }
}
也就是说,如果您将此应用于Silverlight,您很可能希望实现。否则,数据绑定将不会反映代码中所做的更改

要实现此接口,您需要添加此实现。实现这一点的“标准”方法是:

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));
        }
    }  
此时,您需要定义属性,如:

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

这样做将允许您将数据绑定到“Report”类。对于数据绑定要使用的任何集合,您也可以考虑使用“<代码>列表> /代码>。

您定义的属性不正确。这应通过以下方式进行:

private string _displayName = string.Empty;
public string displayName
{
    get { return _displayName; }
    set { _displayName = value; }
}
也就是说,如果您将此应用于Silverlight,您很可能希望实现。否则,数据绑定将不会反映代码中所做的更改

要实现此接口,您需要添加此实现。实现这一点的“标准”方法是:

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));
        }
    }  
此时,您需要定义属性,如:

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

这样做将允许您将数据绑定到“Report”类。您还可以考虑使用数据绑定来使用的任何集合而不是<代码>列表< />。我一直在把这段代码与我已经写过的东西和互联网上的东西进行比较,现在你的代码——我终于明白了什么不合适了。谢谢哦,真傻。我一直在把这段代码与我已经写过的东西和互联网上的东西进行比较,现在你的代码——我终于明白了什么不合适了。谢谢