Xamarin.Forms应用程序将数据返回到调用应用程序 [英] Xamarin.Forms App return data to calling App

查看:100
本文介绍了Xamarin.Forms应用程序将数据返回到调用应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,或者我问错了,或者不可能,让我们看看哪个...

So, either I am asking incorrectly, or it isn't possible, let's see which...

如果我的应用程序(Xamarin.Forms)是从另一个应用程序启动的,为了从我的应用程序获取url,如何将这些数据返回给调用应用程序?我错误地假设了SetResult和Finish,也错误地假设了StartActivityForResult,但是必须有一种方法可以做到这一点.我知道如何将数据从另一个应用程序获取到我的应用程序中,但是回报却不尽相同.

If my app (Xamarin.Forms) is launched from another app, in order to get a url from my app, how do I return that data to the calling app? I wrongly assumed SetResult and Finish, I also wrongly assumed StartActivityForResult, but there has to be a way to do this. I know how to get data INTO my app from another app, but not the same in return.

可能的部分解决方案-更新,失败

因此,我必须在PCL中设置一个接口,然后从列表视图项选定的处理程序中调用该方法,然后在Android应用程序中可以做到这一点:

So I have to setup an interface in my PCL, and call the method from the listview item selected handler, in the Android app I can then do this:

Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_url"));
setResult(Activity.RESULT_OK, result);
finish();

(来源: https://developer.android.com/training/basics/intents/filters.html )

这看起来是否正确,我将如何在iOS上实现相同的功能?

Is this looking right, and how would I implement the same thing on iOS?

END

我删除了之前的问题,因为我无法清楚地说明问题,因此请继续.

I deleted my previous question because I couldn't explain the problem clearly, so here goes.

我有一个Xamarin Forms应用程序,我想将此应用程序的一部分用作图库.目前,我在列表中显示了图像,并且有一个Intent过滤器集,当您选择应用程序作为图像源(例如在Facebook上上传图像)时,将启动此页面. 我的问题是我不知道如何将数据(选定的图像)返回到发出请求的应用程序/网页.在android中,我知道您可以使用StartActivityForResult和OnActivityResult来处理此问题,但是我使用的是Xamarin Forms(Android,iOS,UWP),并且无法真正找到可以跨平台使用的解决方案.

I have a Xamarin Forms app, I want to use a section of this app as a gallery. Currently I have images displayed in a list, and I have an Intent filter set that launches this page when you select the app as the source for an image (such as upload image on Facebook).
My issue is that I don't know how to return the data (the selected image) back to the app / webpage that made the request. In android I understand that you would use StartActivityForResult and OnActivityResult to handle this, but I am using Xamarin Forms (Android, iOS, UWP) and can't really find a solution that could be used cross-platform.

只需一个指向涵盖此内容的文档的链接就可以了,但是,如果您有一个示例,那就更好了.

Just a link to documentation that covers this would be great, but if you have an example then even better.

谢谢

编辑

这是用于启动该应用程序的代码,我感兴趣的是在用户从 ListView 中选择图像后,从 Intent.ActionPick取回数据,在PCL的 ContentPage 中.

Here is the code used to launch the app, I am interested in getting data back from the Intent.ActionPick after the user has selected an image from a ListView, which is in a ContentPage in the PCL.

[Activity(Label = "", Icon = "@drawable/icon", Theme = "@style/DefaultTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop,
          ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = @"*/*")]
[IntentFilter(new[] { Intent.ActionView, Intent.ActionPick, Intent.ActionGetContent }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryOpenable }, DataMimeType = @"*/*")]
public class MainActivity : FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        try
        {
            base.OnCreate(bundle);

            CurrentPlatform.Init();
            Xamarin.Forms.Forms.Init(this, bundle);

            App _app = new App();
            LoadApplication(_app);

            if (Intent.Action == Intent.ActionSend)
            {
                var image = Intent.ClipData.GetItemAt(0);
                var imageStream = ContentResolver.OpenInputStream(image.Uri);
                var memOfImage = new System.IO.MemoryStream();
                imageStream.CopyTo(memOfImage);
                _app.UploadManager(memOfImage.ToArray());  //This allows me to upload images to my app
            }
            else if (Intent.Action == Intent.ActionPick)
            {
                _app.SelectManager(); //here is where I need help
            }
            else
            {
                _app.AuthManager(); //this is the default route
            }
        }
        catch (Exception e)
        {
        }
    }

推荐答案

似乎您无法使用远程URI提供给调用应用程序.我检查过的一些帖子建议将文件存储在本地,并提供调用应用程序的路径.为避免许多文件存储时发生内存泄漏,我建议使用相同的文件名,这样您随时都将只有一个文件.

It seems you cannot use remote URI to provide to calling app. Some posts I checked suggest to store the file locally and provide it's path to calling app. To avoid memory leak with many files stored I suggest to use the same file name then you will have only one file at any moment.

还有一个便条.我在Facebook中测试了此解决方案. Skype似乎不接受这一点,而且,我再次检查过的帖子中说Skype无法正确处理Intent(不确定这是什么意思).

One more note. I tested this solution in facebook. Skype doesn't seem to accept that and, again, the posts I checked saying that Skype doesn't handle Intent properly (not sure what that means).

现在解决.在主要活动中,例如在OnCreate方法中添加以下内容. ReturnImagePage是我在其中选择图像的页面类的名称

Now to solution. In main activity for example in OnCreate method add the follow. ReturnImagePage is the name of my page class where I select an image

        Xamarin.Forms.MessagingCenter.Subscribe<ReturnImagePage, string>(this, "imageUri", (sender, requestedUri) => {

            Intent share = new Intent();
            string uri = "file://" + requestedUri;
            share.SetData(Android.Net.Uri.Parse(uri));

            // OR
            //Android.Net.Uri uri = Android.Net.Uri.Parse(requestedUri);
            //Intent share = new Intent(Intent.ActionSend);
            //share.PutExtra(Intent.ExtraStream, uri);
            //share.SetType("image/*");
            //share.AddFlags(ActivityFlags.GrantReadUriPermission);

            SetResult(Result.Ok, share);
            Finish();
        });

选择图像后,以上将收听消息.

Above will listen for the message when the image is selected.

然后在XFroms代码中选择图像后,将其下载,存储,获取路径并使用其路径发送给Activity.下面是我的测试路径

Then in XFroms code when image is selected dowload it, store it, get path and send to Activity using it's path. Below is my test path

MessagingCenter.Send<ReturnImagePage, string>(this, "imageUri", "/storage/emulated/0/Android/data/ButtonRendererDemo.Droid/files/Pictures/temp/IMG_20170207_174559_21.jpg");

这篇关于Xamarin.Forms应用程序将数据返回到调用应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆