2013-12-20 52 views
-3

我是初学者,能向我解释这行代码吗?我不明白的代码行

var button = new KinectTileButton 
{ 
    Label = System.IO.Path.GetFileNameWithoutExtension(file), 
    Background = new ImageBrush(bi) 
}; 
+2

究竟是什么,你不明白?是C#你不明白吗?这是基本的对象实例化。 –

+2

[Object Initializers ??](http://msdn.microsoft.com/zh-cn/library/bb384062.aspx) –

回答

3

我认为它定义了名称的按钮,文件没有它的extenstion的名称。例如测试,而不是test.txt,然后设置一个背景图像到按钮。

0

它定义了一个带有文件名标签路径的按钮,没有任何按钮的扩展和背景。

意思是如果文件名是abc.txt那么按钮标签将只是abc。

1

该代码是用于创建对象的简短代码,与c#4一起出现。 这是此代码的语法糖:

KinectTileButton button = new KinectTileButton() 

    button.Label = System.IO.Path.GetFileNameWithoutExtension(file), 
    button.Background = new ImageBrush(bi) 
0

“=”后面的代码是KinectTileButton对象的实例化,该对象的“Label”和Background属性设置为其他对象类型。

具体地说,

Label = System.IO.Path.GetFileNameWithoutExtension(file)通过调用“GetFileNameWithoutExtension方法 Background - New ImageBrush(bi)是实例化”的ImageBrush对象并将其分配给背景属性设置该属性。

KinectTileButton实例化的这种技术称为ObjectInitialiser。你也可以这样写:

var button = KinectTileButton(); //assuming there is a parameterless constructor available 
button.Label = System.IO.Path.GetFileNameWithoutExtension(file); 
button.Background = new ImageBrush(bi); 

希望这有助于!