如何将viewmodel与不同类型的项目一起使用

时间:2015-04-11 16:57:43

标签: wpf wcf mvvm windows-phone viewmodel

我有一个相当复杂的项目,存在多个子项目。

  • 应用
    • WPF申请
    • Windows应用商店应用
    • Windows Phone App
  • 合同
    • 服务合同(WPF的WCF)
    • 服务控制器(Windows Phone for Windows phone)
  • 基础设施(EventAggregator / Prism)
  • ViewModels(最好由WPF,Windows Phone和Windows App使用)
  • 观点(WPF特定)

只要我专注于WPF,我就没有问题。在我的viewmodel中,我可以调用webservice并用所需的数据填充它。对于Windows Phone / Windows Store App,我无法始终使用WCF。但是视图模型保持不变。我将如何发送"对我的viewmodel的正确服务调用?

public async override Task<object> RetrieveItems()
    {
        if ((Customer != null))
        {
            return await Task.Run(() => Current.ApplicationService.Schedule_Appointments_GetItems_By_Relation(Customer.Relation_ID));
        }
        else
        {
            return null;
        }
    }

只要它是一个wcf服务,这个函数就可以正常工作。 是否有可能根据使用此viewmodel的视图更改此函数?

这里是一个视图模型的完整代码:

namespace ISynergy.Modules.Relations
{
public class Customer_Activities_ViewModel : Customer_Base_ViewModel
{

    public Customer_Activities_ViewModel()
        : base()
    {
    }

    public override void Add()
    {
        throw new NotImplementedException();
    }

    public override Task Delete(object vItem)
    {
        throw new NotImplementedException();
    }

    public override void Edit(object vItem)
    {
        throw new NotImplementedException();
    }

    public async override Task<object> RetrieveItems()
    {
        if ((Customer != null))
        {
            return await Task.Run(() => Current.ApplicationService.Schedule_Appointments_GetItems_By_Relation(Customer.Relation_ID));
        }
        else
        {
            return null;
        }
    }
}

}

当然,这同样适用于其他被覆盖的程序/功能(添加,删除和编辑)

1 个答案:

答案 0 :(得分:0)

好的,我想我得到了答案。 我有任何建议或评论请随意提供一个。 也许这个答案可以帮助其他人解决同样的问题。

解决方案包括两项调整。

public async override Task<object> RetrieveItems()
    {
        if ((Customer != null))
        {
            return await Task.Run(GetItems_Action);
            //return await Task.Run(() => Current.ApplicationService.Schedule_Appointments_GetItems_By_Relation(Customer.Relation_ID));
        }
        else
        {
            return null;
        }
    }

并添加下一行以允许从视图中进行调整。

public Action Add_Action;
public Action<object> Edit_Action;
public Func<object, Task> Delete_Action;
public Func<Task<object>> GetItems_Action;