Share Data Xamarin Forms

Marco Salvatori 191 Reputation points
2021-09-06T13:17:29.607+00:00

I have implemented the code to share data with my APP.
On Android, if my APP exists in the background, it works fine.

129634-sharedata-error-1.png

otherwise, if android closed my app, it won't load the controls in the form correctly.

129605-sharedata-error-2.png

What could be the problem?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
{count} votes

Accepted answer
  1. Marco Salvatori 191 Reputation points
    2021-10-14T14:53:31.973+00:00

    after several tests, i found the problem.
    I use a TabbedPage.
    to visualize the page correctly, I have to insert this instruction
    android: TabbedPage.ToolbarPlacement = "Bottom"
    'Bottom' and not 'Top'. with 'Top' the page remains empty.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,381 Reputation points Microsoft Vendor
    2021-09-08T07:56:17.6+00:00

    Hello,
    Welcome to our Microsoft Q&A platform!
    I test the share feature with two apps, it works fine, I am so sorry I can't reproduce your issue, you could check your code according to the following steps.

    First, click the button to share in ShareImageTwo(An app to share)

    private async void Button_Clicked(object sender, EventArgs e)  
            {  
                await Share.RequestAsync(new ShareTextRequest  
                {  
                    Text = "https://aka.ms/campus.jpg",  
                    Title = "ShareImageUrlString"  
                });  
            }  
    

    Second, receive the text in ShareImageOne(means your app)

    1. add [IntentFilter(new[] { "android.intent.action.SEND" }, Categories = new[] { Intent.CategoryDefault }, DataMimeTypes = new[] { "text/plain" })] to MainActivity.cs
    2. receive the text in OnCreate function and use MessagingCenter to pass this text to Forms project.
             protected override void OnCreate(Bundle savedInstanceState)  
      {  
          base.OnCreate(savedInstanceState);  
      
          Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
          global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
          LoadApplication(new App());  
          if (this.Intent.Action == Intent.ActionSend)  
          {  
              var url = this.Intent.Extras.GetString(Intent.ExtraText);  
              MessagingCenter.Send<string,string>("11","imageUrl", url);                 
          }  
      }  
      
    3. subscribe to this message and display
      public MainPage()  
              {  
              InitializeComponent();  
      
              MessagingCenter.Subscribe<string, string>("11", "imageUrl", async (sender, arg) =>  
              {  
                  myLabel.Text = arg;  
                  await DisplayAlert("Message received", "arg=" + arg, "OK");// test   
                  imageurl = arg;  
                  await Navigation.PushModalAsync(new ShowImagePage(imageurl));  
              });  
      
          }  
      
    4. new page to display the image
      public ShowImagePage(string imageurl)  
              {  
              InitializeComponent();  
              myImage.Source = ImageSource.FromUri(new Uri(imageurl));   
              }  
      

    You also could upload your basic, minimal, reproducible project to github and attach the link here, then I will download and test it.

    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.