Written by Altınok Darıcı on 26 - Nov - 2014

Windows Phone 8.1 Share Contract – Share Target

  1. Register
    You should register supported formats and file types in manifest. (Plain text, Formatted text, URI, HTML, Images, Files, Custom data formats)Manifest Settings
  2. Activation
    You should create a page. i.e. ShareTargetPage.xaml. And write below code block to App.xaml.cs
    protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
    {
       Frame frame = Window.Current.Content as Frame;
    
       if (frame == null)
       {
          frame = new Frame();
          Window.Current.Content = frame;
       }
    
       if (frame.Content == null)
           frame.Navigate(typeof(ShareTargetPage), args.ShareOperation);  
    
       Window.Current.Activate();
    }
  3. ShareTargetPage OnNavigatedTo
    When an application share file and text to our application, our ShareTargetPage will handle it.
    ShareOperation operation=null;
    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
     operation = (ShareOperation)e.Parameter;
     TextBlockTitle.Text = operation.Data.Properties.Title;
     TextBlockDescription.Text = operation.Data.Properties.Description;
     TextBlockText.Text = await operation.Data.GetTextAsync();// get shared text
     var file = (StorageFile)(await operation.Data.GetStorageItemsAsync()).FirstOrDefault();// get first shared file
     BitmapImage img = new BitmapImage();
     img.SetSource(await file.OpenReadAsync());
     ImagePhoto.Source = img;
    }

     

  4. Complete
    Last step is ReportCompleted, when our target application finalize their job. We should call operation.ReportCompleted(); method to return user to source application. For example we have a button and when job is completed user touch this button.
    private void Button_Click(object sender, RoutedEventArgs e)
    {
       operation.ReportCompleted();
    }
    

     Happy Coding :)

comments powered by Disqus