locked
InvokeScript in OnActivated for share target RRS feed

  • Question

  • Hello ! 

    I try to use sharetarget, so i have  in my app :

      protected async override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
            {
    
               /* var rootFrame = new Frame();
                //rootFrame.Navigate(typeof(ComposerLight), args.ShareOperation);
                rootFrame.Navigate(typeof(Composer), args.ShareOperation);
                Window.Current.Content = rootFrame;
                Window.Current.Activate();*/
    
                Frame frame = CreateOrGetCurrentFrame();
                frame.Navigate(typeof(Composer), args.ShareOperation);
                Window.Current.Content = frame;
                Window.Current.Activate();

    And when i do this : 

     await Task.Factory.StartNew(async () =>
                 {
                     // Retrieve the data package properties.
                     this.mSharedDataTitle = this.mShareOperation.Data.Properties.Title;
                     this.mSharedDataDescription = this.mShareOperation.Data.Properties.Description;
                     this.mSharedThumbnailStreamRef = this.mShareOperation.Data.Properties.Thumbnail;
                     this.mShareQuickLinkId = this.mShareOperation.QuickLinkId;
                     
                     // Retrieve the data package content.
                     if (this.mShareOperation.Data.Contains(StandardDataFormats.Uri))
                     {
                         // The GetUriAsync(), GetTextAsync(), GetStorageItemsAsync(), etc. APIs will throw if there was an error retrieving the data from the source app.
                         // In this sample, we just display the error. It is recommended that a share target app handles these in a way appropriate for that particular app.
                         try
                         {
                             this.mSharedUri = await this.mShareOperation.Data.GetUriAsync();
                         }
                         catch (Exception ex)
                         {
                             //NotifyUserBackgroundThread("Failed GetUriAsync - " + ex.Message, NotifyType.ErrorMessage);
                         }
                     }
                     if (this.mShareOperation.Data.Contains(StandardDataFormats.Text))
                     {
                         try
                         {
                             this.mSharedText = await this.mShareOperation.Data.GetTextAsync();
                         }
                         catch (Exception ex)
                         {
                             //NotifyUserBackgroundThread("Failed GetTextAsync - " + ex.Message, NotifyType.ErrorMessage);
                         }
                     }
                     if (this.mShareOperation.Data.Contains(StandardDataFormats.StorageItems))
                     {
                         try
                         {
                             this.mSharedStorageItems = await this.mShareOperation.Data.GetStorageItemsAsync();
                         }
                         catch (Exception ex)
                         {
                             //NotifyUserBackgroundThread("Failed GetStorageItemsAsync - " + ex.Message, NotifyType.ErrorMessage);
                         }
                     }
                     
                     if (this.mShareOperation.Data.Contains(StandardDataFormats.Html))
                     {
                         try
                         {
                             this.mSharedHtmlFormat = await this.mShareOperation.Data.GetHtmlFormatAsync();
                         }
                         catch (Exception ex)
                         {
                             // NotifyUserBackgroundThread("Failed GetHtmlFormatAsync - " + ex.Message, NotifyType.ErrorMessage);
                         }
    
                         try
                         {
                             this.mSharedResourceMap = await this.mShareOperation.Data.GetResourceMapAsync();
                         }
                         catch (Exception ex)
                         {
                             // NotifyUserBackgroundThread("Failed GetResourceMapAsync - " + ex.Message, NotifyType.ErrorMessage);
                         }
                     }
                     if (this.mShareOperation.Data.Contains(StandardDataFormats.Bitmap))
                     {
                         try
                         {
                             this.mSharedBitmapStreamRef = await this.mShareOperation.Data.GetBitmapAsync();
                         }
                         catch (Exception ex)
                         {
                             // NotifyUserBackgroundThread("Failed GetBitmapAsync - " + ex.Message, NotifyType.ErrorMessage);
                         }
                     }
                     
                 });
    
                 if (this.mSharedHtmlFormat != null)
                 {
                     // set object
                     subjectField.Text = mSharedDataTitle;
    
                     string htmlFragment = HtmlFormatHelper.GetStaticFragment(this.mSharedHtmlFormat);
                     if (!String.IsNullOrEmpty(htmlFragment))
                     {
                         htmlFragment = htmlFragment.Replace("\\", "\\\\");
                         htmlFragment = htmlFragment.Replace("\"", "\\\"");
                         htmlFragment = htmlFragment.Replace(System.Environment.NewLine, "\\r\\n");
                         this.ComposerWebView.Visibility = Visibility.Visible;
                         try
                         {
                             if (mSharedUri != null)
                             {
                                 var dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
    
                                 await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                                 {
                                     this.ComposerWebView.InvokeScript("eval", new string[] { "insertHTML(\"<a href=\\\"" + mSharedUri + "\\\" title=\\\"" + mSharedUri + "\\\" style=\\\"color:orange\\\">" + mSharedUri + "</a>\");" });
    
                                 });
                             }
                             else
                             { 
                                 var dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
                             await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                             {
                                 String[] evalString = new String[] { "insertHTML(\"<br \\><br \\>" + htmlFragment + "\");" };
                                 this.ComposerWebView.InvokeScript("eval", evalString);
                            
                                 //await Task.Delay(70);
                                 this.refreshWebViewBrush();
                             });
                             }
                         }
                         catch (Exception ex)
                         {
                             //TODO
                         }

    when i'm clicking on a button it's running correctly by i want to do my code in OnActivated. It's doesn't work, i have no error but the probleme is on 

    this.ComposerWebView.InvokeScript("eval", evalString);

    I think the task it doesn't finish or something like this. Someone has a solution ? 

    Thanks !

    Wednesday, July 17, 2013 11:34 AM

Answers

  • Does that line of code work properly when it's not in onsharetargetactivated?

    Also, I am not convinced that the webview exists at the time that you are trying to perform an invokescript. 


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, July 17, 2013 6:19 PM
    Moderator