Esempio n. 1
0
    private async void ConnectToiRacer()
    {
      try
      {
        if (racerDeviceInfo != null)
        {
          //Get rid of the command provider
          if (accelerometerCommandProvider != null)
            accelerometerCommandProvider = null;

          //Dispose of the Racer and destory it if it exists...
          DestroyRacer();

          //Create a new Racer...
          //If running on the device, create real racer
          //Otherwise, (running on the emulator) create a mock racer
          if (Microsoft.Devices.Environment.DeviceType == DeviceType.Device)
            this.Racer = new Racer();
            //this.Racer = new Mock.MockRacer();
          else
            this.Racer = new Mock.MockRacer();


          //Connect it to the selected device
          await this.Racer.ConnectAsync(racerDeviceInfo);

          //Assign the AccelerometerCommandProvider to control it
          //this.Racer.CommandProvider = new AccelerometerCommandProvider();
          accelerometerCommandProvider = new AccelerometerCommandProvider();
          accelerometerCommandProvider.Racer = this.Racer;
        }
      }
      catch (Exception ex)
      {
        string message = string.Format("An {0} occurred when trying to connect: {1}", ex.GetType().Name, ex.Message);
        ShowMessage(message);
      }

    }
Esempio n. 2
0
    private void DestroyRacer()
    {
      //To prevent an infinite loop, 
      //make sure to work with the racer field, not the Racer property
      
      if (this.racer != null)
      {
        //Unsubscribe from the RacerConnectionStateChanged event...
        racer.RacerConnectionStateChanged -= OnRacerConnectionStateChanged;

        Racer.Dispose();
        this.racer = null;

        //Get rid of the accelerometerCommandProvider;
        accelerometerCommandProvider.Dispose();
        accelerometerCommandProvider = null;

        GoCommand.RaiseCanExecuteChanged();
        DisconnectCommand.RaiseCanExecuteChanged();
      }
    }