如何将Android Java代码转换为同等的C#Xamarin Android代码

发布于 2025-01-23 08:10:15 字数 3980 浏览 0 评论 0原文

下面给出的是我的Android Java代码。我有一个接口 cormissionResultCallback

public interface PermissionResultCallback {

    /**
     * Called with the permission result.
     *
     * @param enabled {@link true} if the permissions have been granted, otherwise {@code false}.
     */
    void onResult(boolean enabled);
}

我在 startwithpermission prompt 方法中称此接口方法

public void startWithPermissionPrompt(@Nullable final PermissionResultCallback callback) {
    requestPermissionsTask = new RequestPermissionsTask(context.getApplicationContext(), new PermissionResultCallback() {
        @Override
        public void onResult(boolean enabled) {
            if (enabled) {
                //noinspection MissingPermission
                startAdapter();
            }

            if (callback != null) {
                callback.onResult(enabled);
            }
        }
    });

    requestPermissionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ACCESS_FINE_LOCATION);
}

requestPermissionTask 是一个内部类,它扩展了 asynctask

private static class RequestPermissionsTask extends AsyncTask<String, Void, Boolean> {

    @SuppressLint("StaticFieldLeak")
    private final Context context;
    private PermissionResultCallback callback;


    RequestPermissionsTask(Context context, PermissionResultCallback callback) {
        this.context = context;
        this.callback = callback;
    }

    @Override
    protected Boolean doInBackground(String... permissions) {
        int[] result = HelperActivity.requestPermissions(context, permissions);
        for (int element : result) {
            if (element == PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }

        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (callback != null) {
            callback.onResult(result);
        }
    }
}

幸运的是,我将同等代码的一半更改为C#。我知道我们不能直接更改为C#,但是可能会有一些工作可用。我已经以所有表格进行了搜索,但是我找不到确切的解决方案。

下面,我为上述给定的Android Java代码添加了等效的Xamarin Android代码。

接口许可ResultCallback

public interface IPermissionResultCallback
    {
        

        /**
         * Called with the permission result.
         *
         * @param enabled {@link true} if the permissions have been granted, otherwise {@code false}.
         */
        void onResult(Boolean enabled);
    }

startWithPermistromptrompt 方法

  public void StartWithPermissionPrompt(IPermissionResultCallback callback)
    {

        requestPermissionsTask = new RequestPermissionsTask(context, callback);


    }

这里的问题是我无法实现 inReceive 接口方法

requestPermissionStask 在此内类问题中的内部类

public class RequestPermissionsTask 
    {


        public Context context;

        public IPermissionResultCallback permissionResultCallback;

        public RequestPermissionsTask(Context context, IPermissionResultCallback permissionResultCallback) 
        {
            this.context = context;
            this.permissionResultCallback = permissionResultCallback;

        }

        public async Task DoStuffInBackground()
        {
            
            await Task.Run(() =>
            {
                int[] result = HelperActivity.RequestPermissions(context, permissions);

                
            foreach (var element in result)
                {
                    if (element == (int)Permission.Granted)
                    {
                        return true;
                    }
                }

                return false;
            });
        }

    }

是我不知道如何通过 helperactivity.requeStperpermissions(上下文,权限); 此处给出的权限自动生成doinbackground方法中的Android本机代码。

我几乎用所有表格和堆叠式问题搜索,但我无法将其转换。

提前致谢。

Below given is my Android java code. I have an Interface PermissionResultCallback

public interface PermissionResultCallback {

    /**
     * Called with the permission result.
     *
     * @param enabled {@link true} if the permissions have been granted, otherwise {@code false}.
     */
    void onResult(boolean enabled);
}

I am calling this interface in startWithPermissionPrompt method

public void startWithPermissionPrompt(@Nullable final PermissionResultCallback callback) {
    requestPermissionsTask = new RequestPermissionsTask(context.getApplicationContext(), new PermissionResultCallback() {
        @Override
        public void onResult(boolean enabled) {
            if (enabled) {
                //noinspection MissingPermission
                startAdapter();
            }

            if (callback != null) {
                callback.onResult(enabled);
            }
        }
    });

    requestPermissionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ACCESS_FINE_LOCATION);
}

The RequestPermissionTask is an Inner class which extends AsyncTask

private static class RequestPermissionsTask extends AsyncTask<String, Void, Boolean> {

    @SuppressLint("StaticFieldLeak")
    private final Context context;
    private PermissionResultCallback callback;


    RequestPermissionsTask(Context context, PermissionResultCallback callback) {
        this.context = context;
        this.callback = callback;
    }

    @Override
    protected Boolean doInBackground(String... permissions) {
        int[] result = HelperActivity.requestPermissions(context, permissions);
        for (int element : result) {
            if (element == PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }

        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (callback != null) {
            callback.onResult(result);
        }
    }
}

Fortunately I have changed half of the equivalent code to C#. I know we can't change as exactly and directly to C#, but there may be some work arounds will be available. I have searched in all the forms but I can't able to find the exact solution.

Below I have added my Equivalent Xamarin android code for the above given Android java code.

Interface PermissionResultCallback

public interface IPermissionResultCallback
    {
        

        /**
         * Called with the permission result.
         *
         * @param enabled {@link true} if the permissions have been granted, otherwise {@code false}.
         */
        void onResult(Boolean enabled);
    }

startWithPermissionPrompt method

  public void StartWithPermissionPrompt(IPermissionResultCallback callback)
    {

        requestPermissionsTask = new RequestPermissionsTask(context, callback);


    }

here the problem is I can't able to implement the onReceive interface method from IpermissionResultCallback interface

RequestPermissionsTask inner class

public class RequestPermissionsTask 
    {


        public Context context;

        public IPermissionResultCallback permissionResultCallback;

        public RequestPermissionsTask(Context context, IPermissionResultCallback permissionResultCallback) 
        {
            this.context = context;
            this.permissionResultCallback = permissionResultCallback;

        }

        public async Task DoStuffInBackground()
        {
            
            await Task.Run(() =>
            {
                int[] result = HelperActivity.RequestPermissions(context, permissions);

                
            foreach (var element in result)
                {
                    if (element == (int)Permission.Granted)
                    {
                        return true;
                    }
                }

                return false;
            });
        }

    }

In this inner class problem is I don't know how to pass params for HelperActivity.RequestPermissions(context, permissions); the permissions given here is automatically generated in android native code in doInBackground method.

I almost searched in all the forms and stackoverflow questions but I can't able to convert it.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文