views:

107

answers:

2

I have a button with 3 States Start/Resume/Pause.
1 Is this a good pattern to solve this or maybe there is some toggle button mode that I am not aware of?

private void cmdStart_Click(object sender, RoutedEventArgs e)
    {
        if (m_end)
        {
            // Reset the game.
            m_end = false;
            cmdStart.Content = "Pause Game";
            // Update the display.
        }
        else
        {
            if (m_pause)
            {
                m_bombTimer.Start();
                foreach (var storyboard in m_storyboards)
                {
                    // resume all animations
                }
                status.Visibility = System.Windows.Visibility.Collapsed;
                m_pause = false;
                cmdStart.Content = "Pause Game";
            }
            else
            {
                m_bombTimer.Stop();
                foreach (var storyboard in m_storyboards)
                {
                    // pause all animations
                }
                status.Visibility = System.Windows.Visibility.Visible;
                cmdStart.Content = "Resume Game";
                m_pause = true;
            }
        }

2 How to get rid off focus of button in Silverlight? I do not want to user to click button pressing enter.

+1  A: 

Have you thought about modelling this using a state machine?

At the moment all of your work is being handled within the Click event but the click event should be simple working with the state machine:

public class RunStateController
{
   public RunState CurrentState { get; private set; }

   public void Start()
   {
       // reset everything
       Run();
   }

   public void Run() 
   {
       State = new RunningState();
       // do the running code here
   }

   public void Pause()
   {
      State = new PausedState();
      // do the pause logic here
   }
}
public abstract class RunState
{
   public abstract void Change(RunStateContext context);
}

public class StartState : RunState
{
   public override void Change(RunStateContext context)
   {
      context.Run();
   }
}

public class RunningState : RunState
{
   public override void Change(RunStateContext context)
   {
      context.Run();
   }
}

public class Form....
{
   private void cmdStart_Click(object sender, RoutedEventArgs e)
   {
      m_controller.CurrentState.Change();
   }

}

In answer to (2) - have you just tried handling the GotFocus event and passing focus straight on to the control you want to have focus?

Ian Johnson
nice snippet. thx
lukas
+3  A: 

Silverlight provides a built-in state machine to assist you, called the Visual State Manager. It would be a bit involved for me to try to provide a full sample here, but in essence, you can create your states explicitly and each state has a state value, which is a storyboard of duration 0, and a transition, which can be any number of animations. In the states, you simply define the desired control states - for example, you can ask that the control is collapsed, etc. In the transitions, you can kick off your animations. Furthermore, you can bind to the states using a custom visual state manager such that when the states change, you trigger changes in content.

What's nice here is there is a clean separation of code from the UI and you simply transition to states and encapsulate the logic elsewhere.

More reading on the VSM:

http://blogs.infosupport.com/blogs/alexb/archive/2010/04/02/silverlight-4-using-the-visualstatemanager-for-state-animations-with-mvvm.aspx

http://blogs.silverlight.net/blogs/justinangel/archive/2008/12/25/custom-vsm-visualstatemanagers-in-silverlight-2-0.aspx

Jeremy Likness
thx a lot I'm new to Silverlight :)
lukas