Xamarin + MvvmCross + Azure移动应用程序表请求不起作用

时间:2018-08-26 14:44:13

标签: xamarin mvvmcross azure-mobile-services

我一直在使用MvvmCross框架和Azure移动应用程序服务来构建一个简单的Xamarin应用程序,以从云存储和检索数据。 UI项目是特定于平台的。

我的应用程序的主视图显示了一个帖子列表,并且应该从Azure云中检索该列表。 发生的是,在启动屏幕之后,该应用程序仅显示黑屏。 检索数据的调用似乎已正确触发,但从未收到响应。

这是我的帖子查看模型:

public class PostsViewModel : MvxViewModel
    {
        readonly IPostService _postService;
        readonly IMvxNavigationService _navigationService;
        readonly MvxSubscriptionToken token;

        public PostsViewModel(IPostService postService, IMvxMessenger messenger, IMvxNavigationService navigationService)
        {
            _postService = postService;
            _navigationService = navigationService;

            token = messenger.SubscribeOnMainThread<PostsChangedMessage>
                (async mex => await LoadPosts());

            Posts = new ObservableCollection<PostViewModel>();
            AddNewPostCommand = new MvxAsyncCommand(AddNewPost);
            ShowPostDetailsCommand = new MvxAsyncCommand<PostViewModel>(ShowPostDetails);
        }

        #region Properties
        public ObservableCollection<PostViewModel> Posts { get; set; }

        private bool isLoading = true;
        public bool IsLoading
        {
            get => isLoading;
            set => SetProperty(ref isLoading, value);
        }
        #endregion

        #region Commands
        public IMvxAsyncCommand AddNewPostCommand { get; }
        private async Task AddNewPost()
        {
            await _navigationService.Navigate(typeof(PostViewModel), new Post());
        }

        public IMvxAsyncCommand<PostViewModel> ShowPostDetailsCommand { get; }
        private async Task ShowPostDetails(PostViewModel postViewModel)
        {
            await _navigationService.Navigate(typeof(PostDetailsViewModel), postViewModel);
        }
        #endregion

        #region Functions
        public override async Task Initialize()
        {
            await LoadPosts();
        }

        private async Task LoadPosts()
        {
            // Remove all counters before reloading
            Posts.Clear();

            var posts = await _postService.GetAllPosts();
            foreach(var post in posts)
            {
                var viewModel = new PostViewModel(_postService, _navigationService);
                viewModel.Prepare(post);
                Posts.Add(viewModel);
            }

            IsLoading = false;
        }
        #endregion
    }

这是我的邮政服务:

    public class PostService : IPostService
    {
        readonly IPostRepository _repository;
        readonly IMvxMessenger _messenger;

    public PostService(IPostRepository repository, IMvxMessenger messenger)
    {
        _repository = repository;
        _messenger = messenger;
    }

    public async Task<Post> AddNewPost(Post post)
    {
        // Just for testing purposes
        post.Date = DateTimeOffset.Now;
        // Temporary hard-coded author; this will be get from the user object 
        post.Author = "John Smith";

        await _repository.Save(post).ConfigureAwait(false);
        _messenger.Publish(new PostsChangedMessage(this));

        return post;
    }

    public async Task DeletePost(Post post)
    {
        await _repository.Delete(post).ConfigureAwait(false);
        _messenger.Publish(new PostsChangedMessage(this));
    }

    public Task<List<Post>> GetAllPosts()
    {
        return _repository.GetAll();
    }

这是我的存储库:

public class PostRepository : IPostRepository
{
    MobileServiceClient MobileService
        = new MobileServiceClient(@"https://vntestapp.azurewebsites.net", new LoggingHandler(true));

    IMobileServiceTable<Post> postTable;

public PostRepository()
{
    postTable = MobileService.GetTable<Post>();
}

public Task Delete(Post post)
{
    return postTable.DeleteAsync(post);
}

public async Task<List<Post>> GetAll()
{
    return await postTable.ToListAsync();
}

public async Task<Post> Save(Post post)
{
    await postTable.InsertAsync(post);
    return post;
}

}

1 个答案:

答案 0 :(得分:0)

如果运行的平台是android,则将Task.Run()构造用于API调用。

根据我在使用Mvvmcross的实践经验,有时在android中,等待异步方法的调用会阻塞该线程,并且永远不会返回

在这种情况下,如果您在Task.Run()中调用它,则效果很好。 虽然iOS没有这个问题。

我会寻找更深入的解释以提供原因。