简体   繁体   中英

Wp7: Wait for user interaction

I'm workin' on a simple game for Windows Phone. This is my problem:

I have to do "polling" on a variable setted by the user, but when I use: while(var == null); user can't interact with the phone anymore. I've tried to use Thread.Sleep but it seems that the program stops to intercept user events when it goes in that while.

What can I do to fix it?

Thanks

EDIT: I simply need to stop my program until that variable is updated by the user

What kind of functionality do you want? Do you want something like a button that user can press? Use events then. Here is an example.

Don't poll. Add an event to the class that contains the property and which will be raised when the property changes.

class Car
{
    private int _fuel;
    public int Fuel
    {
        get { return _fuel;}
        set
        {
            if(_fuel != value)
            {
                _fuel = value;
                var t = TankEmpty;
                if(t != null)
                {
                    t(this, EventArgs.Empty);
                }
            }
        }
     }

     public event EventHandler TankEmpty; 
}


class Driver
{
    private Car _car;

    Driver(Car car)
    {
        _car = car;
        _car.TankEmpty += Car_TankEmpty;
    }

    private void CarTank_Empty(object sender, EventArgs e)
    {
        //Push Car
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM