ToggleSquare()
当徽标被点击时,这个帮助方法将会设置所有相关的徽标(亮->暗,暗->亮),然后检查是否棋盘处于赢的状态。
private void ToggleSquare(Image image)
{
// check if square is on
if (image.Source == vistaLogoOn.Source)
{
image.Source = vistaLogoOff.Source;
image.Opacity = 0.50;
}
else
{
image.Source = vistaLogoOn.Source;
image.Opacity = 0.75;
}
if (CheckForWin())
gameStatus.Text = "You Win";
}
CheckForWin()
这个帮助函数用来检查棋盘是否处于赢状态,如果所有的徽标都是亮或者都是暗,则玩家胜出。
private bool CheckForWin()
{
int onCount = 0; // assume no square on by default
bool checkForWin = false; // assume player did not win
// loop through all squares
for (int i = 0; i < squares.Count; i++)
{
if (squares[i].Source == vistaLogoOn.Source)
onCount++;
}
// if all lights are on or off then player wins
if (onCount == 0 || onCount == 25)
checkForWin = true;
return checkForWin;
}
Browser.HtmlTimer
我发现在Silverlight1.1中有一个HtmlTimer类。这个类没有文档记载而且是被遗弃的类。在编译后VS将会有一个提示:'System.Windows.Browser.HtmlTimer' is obsolete: '这不是一个高精度的计时器,并不适合于短间隔的动画。在未来版本里将会有个高级的计时器版本出现。
...
// use the browser's HtmlTimer to refresh background regularly
System.Windows.Browser.HtmlTimer timer = new System.Windows.Browser.HtmlTimer();
timer.Interval = 1;
timer.Enabled = true;
timer.Tick += new EventHandler(timer_Tick);
...
void timer_Tick(object sender, EventArgs e)
{
double currentLeft = (double)background.GetValue(Canvas.LeftProperty);
if (currentLeft <= 0)
{
// move background pixels over>
background.SetValue(Canvas.LeftProperty, currentLeft + 2);
}
else
{
// reset backgrounds position
background.SetValue(Canvas.LeftProperty, -340);
}
}
但是目前System.Windows.Browser.HtmlTimer是最好的创建基本动画的方法。