2012-12-08 20 views
1

我试图了解Windows应用商店示例中的示例游戏之一是如何工作的。具体来说这一个Windows应用商店应用中的PropertyPaths和绑定

http://code.msdn.microsoft.com/windowsapps/Reversi-XAMLC-sample-board-816140fa

我了解大部分的怎么回事(我认为),但我真的不知道怎么回事就在这里:

boardSpace.SetBinding(BoardSpace.SpaceStateProperty, 
new Binding { Path = new PropertyPath(String.Format("[{0},{1}]", row, column)) }); 

我不明白的PropertyPath是什么完全绑定。它似乎在形成一些2D数组,因此它将SpaceStateProperty从游戏模型视图绑定到此PropertyPath,但[0,1]或[2,2]如何转换为某个特定的实例或路径?

下一行更有意义: boardSpace.SetBinding(BoardSpace.CommandProperty, new Binding {Path = new PropertyPath(“MoveCommand”)});

这些绑定BoardSpacebutton CommandProperty到MoveCommand代表这是在GameViewModel

暴露现在我发现一个函数多数民众赞成暴露这样

public BoardSpaceState this[String index] 

请问属性路径被绑定到这个功能因为它需要一个字符串,而PropertyPath只是一个字符串[x,y]?它如何知道?

我觉得我错过了PropertyPath工作方式的一个细微部分,但阅读文档并没有多大意义。

我感谢所有帮助

回答

0

是的,这是结合了“此”属性,也被称为索引,它提供了一种索引直接进入定义类,仿佛它是一个集合。因此,索引器是指定索引时使用的一种默认属性,但没有特定的属性名称。

文档中的相关主题是Indexers (C# Programming Guide),它包括下面的代码段(这里缩写):

class SampleCollection<T> 
{ 
    // Define the indexer, which will allow client code 
    // to use [] notation on the class instance itself. 
    public T this[int i] { /* ... */ } 
} 

// Usage: 
SampleCollection<string> stringCollection = new SampleCollection<string>(); 
stringCollection[0] = "Hello, World"; 

这不是有约束力的例子,但概念是在结合相同的。属性路径“[索引]”是对当前数据上下文的索引器属性(示例中的GameViewModel对象)的特定索引值的绑定。