2

I want to share two seperate images on two seperat button clicks. Until now I have used this code

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //showPuzzle();
            DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.DataRequested);
        }

private async void DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            DataRequest request = args.Request;
            request.Data.Properties.Title = "c this";
            request.Data.Properties.ApplicationName = "";
            DataRequestDeferral deferral = request.GetDeferral();

            // Make sure we always call Complete on the deferral.
            try
            {
                StorageFile imagefile = await KnownFolders.PicturesLibrary.GetFileAsync("pic.jpg");
                List<IStorageItem> storageItems = new List<IStorageItem>();
                storageItems.Add(imagefile);
                request.Data.SetStorageItems(storageItems);
            }
            finally
            {
                deferral.Complete();
            } 
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
  Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();

        }

This works well for one button tap but how to do for sharing another image (say pic1.jpg)

Isham Mohamed
  • 2,629
  • 1
  • 14
  • 27
  • 1
    It is unclear (to me at least) what you are trying to ask. – bobbyalex Aug 23 '14 at 15:18
  • @BobbyAlexander Say for instance, I wanna share one image (named pic.jpg) on a button click (named Button) for that I can use the code above, at the same time I need to share another image (named pic1.jpg) on another button click (named Button1). But ` Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();` shows only one (the first one) how to do it for the second one as well. (that means, 2 different images should be shared on two different button clicks) – Isham Mohamed Aug 23 '14 at 17:06

1 Answers1

2

You can define a private member of the page which contains the name of the image to be shared and set this name on each button click. Example:

  private string imageName = "pic.jpg";

  private void Button_Click(object sender, RoutedEventArgs e)
  {
    imageName = "pic.jpg";
    Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();
  }

  private void Button1_Click(object sender, RoutedEventArgs e)
  {
    imageName = "pic1.jpg";
    Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();
  }

and in DataRequested() instead of hardcoding the name of the file, use the name from the private member

Jogy
  • 2,465
  • 1
  • 14
  • 9