Xamarin.Forms App 将数据返回给调用 App [英] Xamarin.Forms App return data to calling App

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

问题描述

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

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?

结束

我删除了我之前的问题,因为我无法清楚地解释问题,所以在这里.

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 会在选择图片时监听消息.

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

然后在选择图像时在 XFroms 代码中下载它,存储它,获取路径并使用它的路径发送到活动.下面是我的测试路径

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 App 将数据返回给调用 App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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