【问题标题】:MVVM async call in viewmodel : How can I connect azure DB with MVVMviewmodel 中的 MVVM 异步调用:如何将 azure DB 与 MVVM 连接
【发布时间】:2014-10-03 05:57:52
【问题描述】:

我正在使用 mvvm 概念(数据、组、模型)开发一个 win phone 8 应用程序,我使用这个概念完成了我的应用程序设计。现在我正在尝试将我的应用程序连接到 azure DB,我还通过以下代码使用 MVVM 概念连接了 azure DB,并且它工作成功。

var js = new JObject { { "institutionid", obj.institutionid }, { "userid", obj.userid } };
var result = await App.MobileService.InvokeApiAsync("school_365_create_dynamic_tile", js, System.Net.Http.HttpMethod.Post, null);

在创建 ViewModel 时,我必须调用使用 Azure 移动服务 Sdk 从 Azure 移动服务读取数据的服务。

Sdk api 使用 async /await 来完成这项工作,而我无法在 ViewModel 中进行异步调用。

Model类的代码是这样的:

public class ModelMail : INotifyPropertyChanged
{

    //newly added
    public Group Mail { get; set; }
    public Group OutBox { get; set; }
    public Group Draft { get; set; }
    public Group SendItems { get; set; }
    public bool IsDataLoaded { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public void LoadData()
    {
        //Newly created
        Mail = CreateMail();
        OutBox = CreateOutBox();
        Draft = CreateDraft();
        SendItems = CreateSendItems();
        IsDataLoaded = true;
    }


    private Group CreateDraft()
    {
        Group data = new Group();

        data.Title = "all";
        string[] gataFromDB = new string[] { "sample ", "sample", "sample", "sample" };

        data.Items.Add(new Data { Name_1 = "Nisar Mohamed VM", Subject = "Subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject 456", Message = "Message This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisarThis is a sample mail message from nisar", time = "11:00 AM", IsChecked = false, foreground = "Black", to = "gowthamrajs@hotmail.com", mailFullDateTime = "Fri 9/12 9:25 PM", from = "kkkk@knowledgeq.com" });

        foreach (string dataa in gataFromDB)
        {

            data.Items.Add(new Data { Name_1 = "Nisar Mohamed VM", Subject = "Subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject Inbox sample mail subject 456", Message = "Message This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisar This is a sample mail message from nisarThis is a sample mail message from nisar", time = "11:10 AM", IsChecked = false, foreground = "Black", to = "nisr199@hotmail.com", cc = "nisr19@gmail.com", mailFullDateTime = "Fri 9/12 9:25 PM", from = "kkkk@knowledgeq.com" });
        }


        return data;
    }

    private Group CreateOutBox()
    {
        Group data = new Group();

        data.Title = "unread";

        return data;
    }

    private Group  CreateMail()
    {
        Group data = new Group();

        data.Title = "all";

        return data;
    }

    private Group CreateSendItems()
    {
        Group data = new Group();

        data.Title = "all";

        return data;
    }

}

我该怎么做

【问题讨论】:

  • “我无法在 ViewModel 中进行异步调用。”这是一个要求吗?还是您使用的框架不支持异步等待?
  • 谢谢先生,是的,您是正确的,我想在 CreateDraft() 或 CreateOutBox() 或 CreateSendItems() 等任何构造函数中使用异步,因为我需要使用 azure 数据库为这些构造函数设置值.
  • 然后将它们标记为异步使用任务并使用Task<T> 例如private async Task<Group> CreateDraft()
  • 它在 Mail = CreateMail() 中显示“错误 2 无法将类型 'System.Threading.Tasks.Task' 隐式转换为 'KQ_School.ViewModels.Group'”;我在哪里创建 constr ...
  • 你必须等待它Mail = await CreateMail();`

标签: c# azure windows-phone-8 asynchronous mvvm


【解决方案1】:

您可能会发现我的MSDN article on async data binding 很有帮助。总之,如果您是数据绑定,那么您需要在数据下载完成时提出PropertyChangedTask 没有实现INotifyPropertyChanged,所以需要一点帮助。我的文章中有一个 NotifyTaskCompletion<T> 类型,它可以用作异步操作的数据绑定包装器。

您将在构造函数中启动异步操作,并创建一个包含(数据可绑定)结果的NotifyTaskCompletion<T>

public class MyViewModel
{
  public MyViewModel()
  {
    Mail = new NotifyTaskCompletion<Group>(CreateMailAsync());
  }

  public NotifyTaskCompletion<Group> Mail { get; private set; }

  private async Task<Group> CreateMailAsync()
  {
    // Azure calls go here
  }
}

然后,您的数据绑定代码可以使用 NotifyTaskCompletion&lt;T&gt; 上的属性来更新 UI:

<Grid>
  <!-- Busy indicator -->
  <Label Content="Loading..." Visibility="{Binding Mail.IsNotCompleted, Converter={StaticResource BooleanToVisibilityConverter}}"/>

  <!-- Results -->
  <Label Content="{Binding Mail.Result.Title}" Visibility="{Binding Mail.IsSuccessfullyCompleted, Converter={StaticResource BooleanToVisibilityConverter}}"/>

  <!-- Error details -->
  <Label Content="{Binding Mail.ErrorMessage}" Background="Red" Visibility="{Binding Mail.IsFaulted, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Grid>

请注意,您的应用程序 UI 中需要一些额外的状态。具体来说,操作正在进行时的“加载”状态;以及操作异步失败时的“错误”状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2011-10-02
    • 2012-07-31
    • 2021-06-26
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多