如何将Xamarin Mobile App Client连接到Azure后端数据库服务器?

时间:2017-02-25 08:48:35

标签: asp.net azure mobile xamarin.android

我正在尝试将使用Xamarin MvvmCross开发的移动应用程序连接到Azure移动应用后端数据库。

之前我已成功完成此操作,直到我创建了一个新项目,并且新的Azure后端服务器需要ZUMO-API-VERSION 2.0.0来获取数据。我从检索数据开始遵循完全相同的过程,从创建数据模型,azure和数据接口以及Azure数据库类开始。我还在 Setup.cs 中注册了它们。

我在数据库中放了一些数据。但是,当我尝试使用var members = await memberDatabase.GetMembers();获取数据时,结果为null。对我做错了什么的想法?

我在这里附上一些相关的代码:

Project.Core /型号/ Members.cs

public class Members
{
    public Members() { }
    public string ID { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DOB { get; set; }
    public string MemberSince { get; set; }
    public double CurrentBalance { get; set; }
}

Project.Core /接口/ IDatabaseMembers.cs

public interface IDatabaseMembers
{
    Task<IEnumerable<Members>> GetMembers();
}

Project.Core /接口/ IAzureDatabase.cs

public interface IAzureDatabase
{
    MobileServiceClient GetMobileServiceClient();
}

Project.Core /数据库/ Members.cs

public class AzureDatabaseMembers : IDatabaseMembers
{
    private MobileServiceClient azureDatabase;
    private IMobileServiceSyncTable<Members> azureSyncTable;
    public AzureDatabaseMembers()
    {
        azureDatabase = Mvx.Resolve<IAzureDatabase>().GetMobileServiceClient();
        azureSyncTable = azureDatabase.GetSyncTable<Members>();
    }

    public async Task<IEnumerable<Members>> GetMembers()
    {
        await SyncAsync(true);
        var members = await azureSyncTable.ToListAsync();
        return members;
    }

    private async Task SyncAsync(bool pullData = false)
    {
        try
        {
            await azureDatabase.SyncContext.PushAsync();

            if (pullData)
            {
                await azureSyncTable.PullAsync("Members", azureSyncTable.CreateQuery()); // query ID is used for incremental sync
            }
        }

        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
    }
}

Project.Droid /数据库/ AzureDatabase.cs

public class AzureDatabase : IAzureDatabase
{
    MobileServiceClient azureDatabase;

    public MobileServiceClient GetMobileServiceClient()
    {
        var platform = new Microsoft.WindowsAzure.MobileServices.CurrentPlatform();
        System.Diagnostics.Debug.WriteLine(platform);
        CurrentPlatform.Init();

        azureDatabase = new MobileServiceClient("http://myweb.azurewebsites.net/");
        InitializeLocal();
        return azureDatabase;
    }

    private void InitializeLocal()
    {
        var sqliteFilename = "Local.db3";
        string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder
        var path = Path.Combine(documentsPath, sqliteFilename);
        if (!File.Exists(path))
        {
            File.Create(path).Dispose();
        }
        var store = new MobileServiceSQLiteStore(path);
        store.DefineTable<Project.Core.Models.Members>();
        azureDatabase.SyncContext.InitializeAsync(store);
    }
}

Project.Droid / Setup.cs

public class Setup : MvxAndroidSetup
{
    .
    .
    .
    protected override void InitializeFirstChance()
    {
        Mvx.LazyConstructAndRegisterSingleton<IAzureDatabase, AzureDatabase>();
        Mvx.LazyConstructAndRegisterSingleton<IDatabaseMembers, AzureDatabaseMembers>();
        base.InitializeFirstChance();
    }
}

0 个答案:

没有答案