comparing text foreground with rectangle fill error
我正在Windows Phone 8中开发棘手的颜色游戏
我有一个文本块和8个矩形(颜色)
任务是按文本的颜色而不是单词的颜色!
示例:如果文本为:红色,并且前景为绿色
播放器必须点击绿色矩形而不是红色矩形
因此在检查答案功能
中
我写道:
1 2 3 4 5 6 7 8 9 10
| private void CheckAnswer(Rectangle c)
{
if (SaveMission == 1)
{
if (ColorText.Foreground.Equals(c.Fill))
MessageBox.Show("Right Answer");
else
MessageBox.Show("Wrong Answer");
}
} |
前景属性为Windows.Media.Bursh
,Fill属性也获得Windows.Media.Brush
当我调试应用程序时:答案始终是错误的!
有什么问题吗?
- @Sayse您所说的color property是什么意思?
-
只需对其进行调试,以查看运行时的值,并查看为什么它们不相等...或为什么它们与您认为的不一样。
-
对不起,忽略了这一点,从我的手机中写入并且无法检查msdn,添加断点时会返回什么值?
-
@Sayse相同的ARGB值!
首先是一个示例程序,它将演示该问题
1 2 3 4 5
| System.Windows.Media.Brush b1 = new SolidColorBrush (Color .FromArgb(0, 0, 0, 0));
System.Windows.Media.Brush b2 = new SolidColorBrush (Color .FromArgb(0, 0, 0, 0));
bool b = b1 .Equals(b2 );
//b will equal false |
现在,如果在程序未运行时用鼠标悬停在.Equals上,则会看到它正在使用DependencyObject.Equals方法,该方法"确定所提供的DependencyObject是否等效于当前DependencyObject"。换句话说,就是不比较颜色
解决方案是投射和检索颜色
1 2 3 4
| System.Windows.Media.Brush b1 = new SolidColorBrush (Color .FromArgb(0, 0, 0, 0));
System.Windows.Media.Brush b2 = new SolidColorBrush (Color .FromArgb(0, 0, 0, 0));
bool b = ((SolidColorBrush )b1 ).Color.Equals(((SolidColorBrush )b2 ).Color); |
- 但是我如何将c和ColorText的字节(ARGB)代替零?
-
你不..你只是投你的Foregrounds
-
我不理解,如何铸造前景和填充属性?
-
((SolidColorBrush)ColorText.Foreground)
-
您的意思是这样的System.Windows.Media.Brush b1 = new SolidColorBrush((SolidColorBrush)ColorText.Foreground);吗?
-
您只需要修改最后一行中的单词b1和b2即可包含画笔。