0

我正在尝试在我的应用程序中使用共享合同共享文件:-

public async void SendingEmail(UtilityClass objselecteditem)
{
    try
    {
        StorageFolder tempFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        StorageFile sampleFile = await 
        tempFolder.CreateFileAsync("StundentDetails.html",   
            CreationCollisionOption.ReplaceExisting);
        await Windows.Storage.FileIO.WriteTextAsync(sampleFile, htmlBuilder.ToString());
        await RegisterForShare();                        
    }
    catch (Exception ex)
    {    
    }
}

我在上面的方法中调用了这个 RegisterForShare() :-

private async Task RegisterForShare()
{
    DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();       
    dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareStorageItemsHandler);            
    DataTransferManager.ShowShareUI();

}

private async void ShareStorageItemsHandler(DataTransferManager sender, DataRequestedEventArgs e)          
{
    DataRequest request = e.Request;
    request.Data.Properties.Title = "Student Confirmation Email";
    request.Data.Properties.Description = "Student Details";



    // Because we are making async calls in the DataRequested event handler,
    // we need to get the deferral first.
    DataRequestDeferral deferral =  request.GetDeferral();

    // Make sure we always call Complete on the deferral.
    try
    {
        StorageFolder tempFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        StorageFile EmailFile = await tempFolder.GetFileAsync("StundentDetails.html");      
        List<IStorageItem> storageItems = new List<IStorageItem>();
        storageItems.Add(EmailFile);
        request.Data.SetStorageItems(storageItems);


    }
    finally
    {
        deferral.Complete();
    }
}

第一次正确执行,共享合同正在为我打开共享文件,但如果我试图再次共享它(第二次共享文件)它会抛出一个异常,说“在一个意外的时间调用了一个方法“,如果我做错了,请告诉我,请纠正我。

提前致谢。

4

1 回答 1

0

尝试替换此行:

dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareStorageItemsHandler); 

dataTransferManager.DataRequested -= ShareStorageItemsHandler;
dataTransferManager.DataRequested += ShareStorageItemsHandler;
于 2012-11-07T16:07:28.577 回答