I study of Windows phone APP development with C# and XAML nearly, but I experience some troubles, one of them is we have no way to trace to the value of Rectangle.Fill immediately.
我能理解設計者必須站在高處為開發者考慮通用的功能;矩形不只能填充單一色彩, 也能填充漸層甚至更複雜的圖案, 所以要設計一個參數或者方法去得顏色的值是很困難的
I can understand that designer need concern the general function for developers; the rectangle not only could be filled with one color, but also could be filled with linear gradient or even more complicated picture, so it's hard to design a property or a method for show the value of color.
不過我的要求其實很單純, 我只是要取得Rectangle.Fill的值, 我們只能給Rectangle.Fill設一種顏色對吧?不需要複雜的方法去比較每一個像素, 只需要一個參數去儲存色碼, 為什麼我們沒有方法去追蹤Fill的值呢?
But...in fact, my request just is get the value form Rectangle.Fill, we just can set one color with Rectangle.Fill right? No need complicated method to compare each pixel, just need a parameter to store the color code, why we have no way to trace the value for Fill?
幸好我們可以利用"Tag", "Tag"事一個可以儲存抽象物件的屬性, 當然我們也可以利用它來取得物件
Fortunately, we can use "Tag" to save it, "Tag" is a property that could be set an arbitrary object to store custom information, of course we can get the object by it also.
所以我們可以在給矩形設顏色時, 順便儲存在Tag, 如下碼:
So we can store the color in the rectangle object by Tag when we set it, as below:
Rectangle rectangle = new Rectangle(); rectangle.Fill = new SolidColorBrush(Colors.Blue); rectangle.Tag = Colors.Blue;
然後我們可以利用Tag來追蹤矩形的顏色:
Then we can use Tag to trace the color of the rectangle:
if(rectangle.Tag.Equals(Colors.Blue)){ action...}
沒有留言:
張貼留言