我是初学者,能向我解释这行代码吗?我不明白的代码行
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi)
};
我是初学者,能向我解释这行代码吗?我不明白的代码行
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi)
};
我认为它定义了名称的按钮,文件没有它的extenstion的名称。例如测试,而不是test.txt,然后设置一个背景图像到按钮。
它定义了一个带有文件名标签路径的按钮,没有任何按钮的扩展和背景。
意思是如果文件名是abc.txt那么按钮标签将只是abc。
该代码是用于创建对象的简短代码,与c#4一起出现。 这是此代码的语法糖:
KinectTileButton button = new KinectTileButton()
button.Label = System.IO.Path.GetFileNameWithoutExtension(file),
button.Background = new ImageBrush(bi)
它定义了一个KinectTileButton有:
- 其标签初始化为“文件”的文件名没有扩展名,
- 它的背景,“BI”初始化这可能是一个图像(请参阅http://msdn.microsoft.com/en-us/library/system.windows.media.imagebrush%28v=vs.110%29.aspx了解更多信息)
“=”后面的代码是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);
希望这有助于!
究竟是什么,你不明白?是C#你不明白吗?这是基本的对象实例化。 –
[Object Initializers ??](http://msdn.microsoft.com/zh-cn/library/bb384062.aspx) –