Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

c# - Sharing from Windows Phone 8

I am working on a Windows Phone 8 app and am trying to share content through the DataTransferManager. The Windows API documentation says it is supported in Windows Phone but when the DataTransferManager.GetForCurrentView() function is called I get an exception

System.NotSupportedException was unhandled by user code
  HResult=-2146233067
  Message=Specified method is not supported.
  Source=Windows
  InnerException: 

I have been searching for an answer and can't find anyone else with the same issue, any help would be appreciated. All samples on this topic seem to be Windows 8 specific, but Phone 8 does include these functions. Here's sample code from my app.

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
        dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(dataTransferManager_DataRequested);
    }

    private void dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
    {
        DataPackage requestData = e.Request.Data;
        requestData.Properties.Title = "Share Text Example";
        requestData.Properties.Description = "An example of how to share text.";
        requestData.SetText("Hello World!");
    }

    private void Button_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
    {
        DataTransferManager.ShowShareUI();
    }

Again, the exception is shown when the page loads on the DataTransferManager.GetForCurrentView(); function so it doesn't get to the other lines, but included them anyway. I've tried adding/removing permissions and assemblies but must be missing something. I've also tried putting the function in different events (such as the onTap function) with the same results.

If anyone is interested in trying this on their own here is some documentation:

DataTransferManager

DataRequested

DataPackage

GetForCurrentView()

UPDATE

Although it may not be the best solution given the context of this question, I am implementing the Email/Sms/Link Tasks as described below rather than using the DataTransferManager. It seems that DataTransferManager may not be accessible in WP8 and although the tasks will take a number of different functions they seem to be the best way to perform the intended functionality.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think I have found most of what I was looking for with Launchers... Rather than just using the Windows 8 general sharing functionality I can be specific with Tasks/Launchers.

Unfortunately it doesn't have as many sharing options as the charm does, I will be implementing several functions for email/sms/social but so far this is the best solution.

Here are the functions that I will be implementing

    private void ShareLink(object sender, System.Windows.Input.GestureEventArgs e)
    {
        ShareLinkTask shareLinkTask = new ShareLinkTask()
            {
                Title = "Code Samples",
                LinkUri = new Uri("http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff431744(v=vs.92).aspx", UriKind.Absolute),
                Message = "Here are some great code samples for Windows Phone."
            };

        shareLinkTask.Show();
    }


    private void ShareEmail(object sender, System.Windows.Input.GestureEventArgs e)
    {
        EmailComposeTask emailComposeTask = new EmailComposeTask()
            {
                Subject = "message subject",
                Body = "message body",
                To = "[email protected]",
                Cc = "[email protected]",
                Bcc = "[email protected]"
            };

        emailComposeTask.Show();
    }

    private void ShareSMS(object sender, System.Windows.Input.GestureEventArgs e)
    {
        SmsComposeTask smsComposeTask = new SmsComposeTask()
            {
                Body = "Try this new application. It's great!"
            };

        smsComposeTask.Show();
    }

Ref:

Launchers for Windows Phone

Share Link Task


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...