Friday, 22 January 2021

Xamarin Android IntentFilter Image share view update

I am trying to pass information from the MainActivity.cs to a View and then update that view with the information to that view in some way. In this case an image. Problem is I cannot get the View to update from within the subscribed message function. The function does hit a break point but doesnt update the UI.

Ive added an intentfilter to MainActivity.cs, and sent the stream of the image via a MessagingCenter using the "Xamarin.Forms.Application.Current" (Is there a better way/object to use?).

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
        if (Intent.Action == "android.intent.action.SEND")
        {

            var data = Intent.Data;
            Android.Net.Uri imageUri = (Android.Net.Uri)Intent.GetParcelableExtra(Android.Content.Intent.ExtraStream);
            var stream = ContentResolver.OpenInputStream(imageUri);
            MessagingCenter.Send(Xamarin.Forms.Application.Current, "ImageSent", stream);

        }
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

On my view I accept the information from the message subscription and have a function that runs. It does hit a break point.

public partial class AboutPage : ContentPage
{

    AboutViewModel _viewModel;
    public AboutPage()
    {
        InitializeComponent();

        MessagingCenter.Instance.Subscribe<Application, Stream>(Application.Current, "ImageSent", OnImageSent);

        BindingContext = _viewModel = new AboutViewModel();
    }

    private void OnImageSent(Application source, Stream imageStream)
    {

        stack.Children.Add(new Image
        {

            Source = "xamarin_logo.png",
            VerticalOptions = LayoutOptions.Center
        });

    }
}

If I add the dynamic image (ive hard coded the image to an image I know exists in this instance) on the constructor it adds it fine but not if it runs in the function. I understand that I should be using MVVM etc, but I want to get this working simply with a minimum of moving parts and then take it from there. Problem exists when I update the BindingContext object too. Something to do with the context of the function that gets run?

I also tried the suggestion to try add the image inside Device.BeginInvokeOmMainThread. But this didnt work either.

    private void OnImageSent(App source, Stream imageStream)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            stack.Children.Add(new Image
            {
                //HeightRequest = 64,
                Source = "xamarin_logo.png",
                VerticalOptions = LayoutOptions.Center
            });
        });
    }

Or am I going about this the wrong way entirely? How should I get my shared image to the App to my view?



from Xamarin Android IntentFilter Image share view update

No comments:

Post a Comment