locked
how to implement share contract correctly in windows 8 apps?

    Question

  • i had VS pro 2012 installed on windows 8.1 pro, i am running windows 8 app(c# & xaml) in it, i added search contract to my app & ran it, when i clicked share from charms bar i got share options as mail & box, i clicked mail & automatically my apps screenshot is getting attached to the mail, how to avoid this & include a text i want in mail ?  
    • Edited by kranthi88 Saturday, October 11, 2014 11:25 AM
    Saturday, October 11, 2014 11:25 AM

Answers

  • Hi Kranthi88 and James,

    On Windows 8.1, the default share behavior is a screenshot of the current screen as described here:
    http://www.7tutorials.com/how-share-screenshots-windows-81-apps

    For adding a custom text share contract you have to follow the steps provided by James. When you need other data types please follow the next link

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh871374(v=win.10).aspx

    Regards,
    Annihil8

    Monday, October 13, 2014 3:56 PM
  • I think you are working for a win8.0 app instead of win8.1 since you are using Visual Studio 2012, I suggest you to find a share sample from https://code.msdn.microsoft.com/windowsapps/Windows-8-app-samples-4d76cbbf

    Let's say if you want to share a image from your main page, then you need to override getShareContent() as you can see from Sharing content source app sample.

    Below is the code that can only share image from a single page.

            protected override bool GetShareContent(DataRequest request)
            {
                bool succeeded = false;
    
                if (this.imageFile != null)
                {
                    DataPackage requestData = request.Data;
                    requestData.Properties.Title = TitleInputBox.Text;
                    requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
    
                    // It's recommended to use both SetBitmap and SetStorageItems for sharing a single image
                    // since the target app may only support one or the other.
    
                    List<IStorageItem> imageItems = new List<IStorageItem>();
                    imageItems.Add(this.imageFile);
                    requestData.SetStorageItems(imageItems);
    
                    RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile);
    
                    
                    requestData.Properties.Thumbnail = imageStreamRef;
                    requestData.SetBitmap(imageStreamRef);
                    succeeded = true;
                }
                else
                {
                    request.FailWithDisplayText("Select an image you would like to share and try again.");
                }
                return succeeded;
            }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, October 15, 2014 7:41 AM
    Moderator

All replies

  • Hi kranthi88,

    Take a look at the documentation: Sharing content

    If you want to share the text, I would recommend you to add following code into your app, but a quite strange thing is in win8 app, there is no screenshot API, how could your mail app get your app screenshot?

    Anyway, try with code gallery sample to see if the mail can get correct text you want to share.

    private void RegisterForShare()
    {
        DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
        dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, 
            DataRequestedEventArgs>(this.ShareTextHandler);
    }
    
    private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e)
    {
        DataRequest request = e.Request;
        request.Data.Properties.Title = "Share Text Example";
        request.Data.Properties.Description = "A demonstration that shows how to share text.";
        request.Data.SetText("Hello World!");
    }
    

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, October 13, 2014 3:03 AM
    Moderator
  • Hi Kranthi88 and James,

    On Windows 8.1, the default share behavior is a screenshot of the current screen as described here:
    http://www.7tutorials.com/how-share-screenshots-windows-81-apps

    For adding a custom text share contract you have to follow the steps provided by James. When you need other data types please follow the next link

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh871374(v=win.10).aspx

    Regards,
    Annihil8

    Monday, October 13, 2014 3:56 PM
  • i have 2 pages in my app, main page & sub page, in which page i have to wire the events & how to wire them, there is no need to add new share contract ??
    Tuesday, October 14, 2014 3:41 AM
  • I think you are working for a win8.0 app instead of win8.1 since you are using Visual Studio 2012, I suggest you to find a share sample from https://code.msdn.microsoft.com/windowsapps/Windows-8-app-samples-4d76cbbf

    Let's say if you want to share a image from your main page, then you need to override getShareContent() as you can see from Sharing content source app sample.

    Below is the code that can only share image from a single page.

            protected override bool GetShareContent(DataRequest request)
            {
                bool succeeded = false;
    
                if (this.imageFile != null)
                {
                    DataPackage requestData = request.Data;
                    requestData.Properties.Title = TitleInputBox.Text;
                    requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
    
                    // It's recommended to use both SetBitmap and SetStorageItems for sharing a single image
                    // since the target app may only support one or the other.
    
                    List<IStorageItem> imageItems = new List<IStorageItem>();
                    imageItems.Add(this.imageFile);
                    requestData.SetStorageItems(imageItems);
    
                    RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile);
    
                    
                    requestData.Properties.Thumbnail = imageStreamRef;
                    requestData.SetBitmap(imageStreamRef);
                    succeeded = true;
                }
                else
                {
                    request.FailWithDisplayText("Select an image you would like to share and try again.");
                }
                return succeeded;
            }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, October 15, 2014 7:41 AM
    Moderator
  • don't i need to right click & add a new share contract page??
    Wednesday, October 15, 2014 2:38 PM
  • Yes, you don't need, you could look into the share contract page, it basically the same code as we suggested.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, October 22, 2014 9:06 AM
    Moderator