C# 比较文本前景与矩形填充错误
我正在Windows Phone 8中开发一个复杂颜色的游戏 我有一个文本块和8个矩形(颜色) 任务是按下文本的颜色和而不是单词的颜色 示例:如果文本为:红色且文本的前景为绿色 玩家必须点击绿色矩形,点击非红色矩形 所以在checking-answer函数中 我写道:C# 比较文本前景与矩形填充错误,c#,windows-phone-8,C#,Windows Phone 8,我正在Windows Phone 8中开发一个复杂颜色的游戏 我有一个文本块和8个矩形(颜色) 任务是按下文本的颜色和而不是单词的颜色 示例:如果文本为:红色且文本的前景为绿色 玩家必须点击绿色矩形,点击非红色矩形 所以在checking-answer函数中 我写道: private void CheckAnswer(Rectangle c) { if (SaveMission == 1) { if
private void CheckAnswer(Rectangle c)
{
if (SaveMission == 1)
{
if (ColorText.Foreground.Equals(c.Fill))
MessageBox.Show("Right Answer");
else
MessageBox.Show("Wrong Answer");
}
}
前台属性是getWindows.Media.Bursh
而且Fill属性获取Windows.Media.Brush
当我调试应用程序时:答案总是错误的!
问题是什么?首先是一个演示问题的示例程序
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是否与当前DependencyObject等效”的方法,或者换句话说,不比较颜色
解决方案是投射和检索颜色
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);
@你说的颜色属性是什么意思?只需调试它,看看运行时的值是什么,看看它们为什么不相等。。。或者为什么它们不是你想象中的那样。抱歉,忽略这一点,从我的手机上写信,无法检查msdn,当你添加断点时返回什么值?@Sayse相同的ARGB值!但是我如何将c和ColorText的字节(ARGB)替换为零呢。。你只需投射你的
前景
我不懂,如何投射前景和填充属性?((SolidColorBrush)ColorText.Foreground)
你的意思是这样的System.Windows.Media.Brush b1=新的SolidColorBrush((SolidColorBrush)ColorText.Foreground)代码>?