开发者

How to take a photo and email it?

I'd like the user to take a photo and then open an email dialog with the photo attached. So far I got this:

private void btnSubmitPhoto_Click(object sender, EventArgs e) 
{ 
    CameraCaptureTask cameraCaptureTask = new CameraCaptureTask(); 
    cameraCaptureTask.Completed += cameraCaptureTask_Completed; 
    cameraCaptureTask.Show(); 
} 

private void cameraCaptureTask_Completed(object sender, PhotoResult e) 
{ 
    if (e.TaskResult == TaskResult.OK) 
    { 
        currentImage = new BitmapImage(); 
        currentImage.SetSource(e.ChosenPhoto); 

        EmailComposeTask ect = new EmailComposeTask();     
    }             
}    

I don't see how to add an attachment to the EmailComposeTask. Am I开发者_StackOverflow社区 missing something?


It is not possible with the current framework tools to add an attachment to an email by using the EmailComposeTask. If you need this functionality, you will have to manually handle email sending yourself by using a web service.


In addition to not being able to send attachments there is an issue with the way that you are using the CameraCaptureTask.

Because this task is a chooser you must code it's use to account for tombsoning.

In practical terms, this means that yoour instance of CameraCaptureTask must be at a class level and the completed event handler should be subscribed too in the page constructor.

If you don't do this then the page won't know how to handle the information returned when the task returns.

Your code will need to look something like this:

public partial class MainPage : PhoneApplicationPage
{
    CameraCaptureTask cct = new CameraCaptureTask();

    public MainPage()
    {
        InitializeComponent();

        // Any other initialization tasks

        cct.Completed += new EventHandler<PhotoResult>(cct_Completed);
    }

    void cct_Completed(object sender, PhotoResult e)
    {
        // Do something with `e`
    }

    // Or some other appropriate event
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        cct.Show();
    }

Please Note. this is the case for all choosers, but not launchers.

Update:
The reason for this is due to the way the page is "rehydrated" after the application is tombstoned to open the chooser.

When returning from displaying the chooser a new instance of the page will be created. This will, therefore, not include any record of event handlers subscribed in previous instances. Without anything attached to the completed handler your code to process the details returned by the chooser will not be called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜