Xamarin.Android Cannot pull table from Azure database in Android 10 Q

北慕城南 提交于 2020-01-05 04:10:12

问题


I did read that because lack of support for Netcore 2.1 the

myItemsList = await App.MobileServiceAndroid.GetTable<MyTable>().ToListAsync();

does not currently work on Android, and there is a workaround to pass an HttpClientHandler() in the constructor of the MobileServiceClient, and so I did like this:

public static MobileServiceClient MobileServiceAndroid =
            new MobileServiceClient(AppConstants.AZURE_PRODUCTION_WEB_API_URL, new HttpClientHandler());

But this is incomplete,its still not working, what exactly do I have to do to make this work, any guidance is much appreciated.


回答1:


From my understanding, you are using a Forms/PCL project whereas the other solution was implementing this code inside their Android project. For you, once you add using Xamarin.Android.Net; to the class, you should be able to just do this:

public static MobileServiceClient MobileServiceAndroid =
            new MobileServiceClient(AppConstants.AZURE_PRODUCTION_WEB_API_URL, new AndroidClientHandler());

Most likely you might have issues getting that using statement, for that you will have to follow steps shown here, or customized for you in the following steps:

  1. Add the Xamarin Forms project to all your projects.
  2. Create an interface ICustomClientHandler in the Core project
using System;
using System.Net.Http;

namespace Test
{
    public interface ICustomClientHandler
    {
        HttpClientHandler GetHandler();
    }
}
  1. Then create a CustomClientHandler in the Droid project, which will be the Android part of the dependency service that will help you retrieve the native AndroidClientHandler
using System.Net.Http;
using Xamarin.Android.Net;
using System.Runtime.CompilerServices;
using Xamarin.Forms;
using Test;
[assembly: Xamarin.Forms.Dependency(typeof(Test.Droid.CustomClientHandler))]
namespace Test.Droid
{
    public class CustomClientHandler : ICustomClientHandler
    {
        public HttpClientHandler GetHandler()
        {
            return new AndroidClientHandler();
        }
    }
}
  1. Implement an iOS version as well in a similar way, but it will instead return new HttpClientHandler();
  2. Finally, use the code as shown, in your Core project:
var clientHandler = DependencyService.Get<ICustomClientHandler>().GetHandler();
public static MobileServiceClient MobileServiceAndroid =
            new MobileServiceClient(AppConstants.AZURE_PRODUCTION_WEB_API_URL, clientHandler);


来源:https://stackoverflow.com/questions/58293151/xamarin-android-cannot-pull-table-from-azure-database-in-android-10-q

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!