0

i am trying to create a simple demo application that does the following: i have a button at MainPage.xaml (with Name="btnCamera") and an image control (with Name="photo") and when i press the button i want to start the camera task, capture a photo and display it on the image control. The problem is that my code works on the emulator but not on a real device. The device i have is updated to the latest update(7740). Do you have an explanation for that or any change to my code to make it work? That is my code:

public partial class MainPage : PhoneApplicationPage
{
     CameraCaptureTask _cameraCapture;

     public MainPage()
     {
          InitializeComponent()
         _cameraCapture = new CameraCaptureTask();
         _cameraCapture.Completed += new EventHandler(_cameraCapture_Completed);
     }

     private void btnCamera_Click(object sender, RoutedEventArgs e)
     {
          try
          {
                _cameraCapture.Show();
          }
          catch (Exception)
          {
                MessageBox.Show("Error occured");
          }
     }

     void _cameraCapture_Completed(object sender, PhotoResult e)
     {
           if (e.TaskResult == TaskResult.OK)
           {
                BitmapImage image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);
                photo.Source = image;
           }
     }
}
ctacke
  • 66,480
  • 18
  • 94
  • 155
Giorgos Manoltzas
  • 1,710
  • 5
  • 24
  • 34
  • what specifically about it isn't working? – John Gardner Jan 11 '12 at 18:20
  • (and i was going to edit your code to fix formatting, but its full of html tags!) – John Gardner Jan 11 '12 at 18:21
  • @JohnGardner yes my formatting is a real mess and really sorry for that but i did not know any other way to format it nicely. When i say it is not working i mean when i press the button it makes an animation (trying to open the camera) but then the screen goes black and it returns to MainPage.xaml – Giorgos Manoltzas Jan 11 '12 at 18:32
  • SO has automatic code formatting. all you have to do is "indent" the code like 4 spaces in the editor, and *magic* happens. – John Gardner Jan 11 '12 at 18:48

1 Answers1

2

You need to make sure Zune is not running. The code looks fine and should work if you unplug the phone from the PC. If you want to debug whilst plugged into the PC, use WPConnect instead of Zune.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • Oh you do not know how much i thank you. I did not know that. – Giorgos Manoltzas Jan 11 '12 at 18:49
  • You're welcome! It's a common problem as it's not very obvious :) I wrote a short blog post explaining how to add the `WPConnect` tool as an option in Visual Studio. That way, you don't have to keep opening `Explorer` when you want to run it. http://phone7.wordpress.com/2010/11/23/making-it-easier-to-debug-media-in-windows-phone-7-apps/ – keyboardP Jan 11 '12 at 18:51