从异步方法绑定数据

时间:2013-09-12 18:40:13

标签: c# data-binding mvvm async-await

我仍然在与我的项目作斗争,我需要一点帮助才能走向。 在MainViewModel中,我调用Refresh方法将数据从XML绑定到ObservableCollection。

private async Task Refresh()
    {
        var listOfBadges = (await _badgeService.GetAll());
        var listOfSets = (await _setService.GetAll());

        BadgesList.Clear();
        SetsList.Clear();

        BadgesList = listOfBadges.ToObservableCollection<Badge>();

        SetsList = listOfSets.ToObservableCollection<Set>();
    }

没关系;)在QuestionViewModel我正在使用BadgeService中的这个方法来编辑这些数据,这些方法实现了存储库模式。

public async Task Update(int id)
    {
        StorageFolder sf = await ApplicationData.Current.LocalFolder.GetFolderAsync("Data");
        StorageFile st = await sf.GetFileAsync("Badgess.xml");

        var xmlStream = await FileIO.ReadTextAsync(st);

        XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(st);

        using (var stream = await st.OpenAsync(FileAccessMode.ReadWrite))
        {
            XmlElement root = xmlDoc.DocumentElement;
            IXmlNode xee = root.SelectSingleNode("//badge/id[.='" + id + "']");
            xee.NextSibling.NextSibling.NextSibling.InnerText = "true";
        }

        var xmlStreamUp = xmlDoc.GetXml();

        if (xmlDoc != null)
            await xmlDoc.SaveToFileAsync(st);
    }

数据有变化,但是当我回到MainViewModel时,我无法在绑定后看到更改,但是当我按下Refresh按钮调用的Command方法绑定{{3}}时,更改后的数据被加载并且一切都好。 回到MainViewModel以正确绑定数据后如何避免点击刷新?

2 个答案:

答案 0 :(得分:0)

由于您的应用程序使用xml作为后备存储,请考虑将对象保留在内存中:

保存数据的存储库:

public interface IBadgeRepository
{
    // Save Xml File
    Task Save(IEnumerable<Badge> badges);
    // Load Xml File
    Task<Badge> GetAll();
}

操纵数据的服务:

public sealed class BadgeService : IBadgeService
{
    private readonly IBadgeRepository _badgeRepository;
    private readonly List<Badge> _badges = new List<Badge>();

    public BadgeService(IBadgeRepository badgeRepository)
    {
        _badgeRepository = badgeRepository;
    }

    public async Task<IList<Badge>> GetAll()
    {
        if(!_badges.Any())
        {
            var badges = _badgeRepository.GetAll();
            _badges.AddRange(badges);
        }

        return Task.FromResult(_badge);
    }

    public async Task Update(Badge badge)
    {
        if(!_badges.Contains(badge))
        {
            _badges.Add(badge);
        }
        else
        {
            var index = _badges.IndexOf(badge);
            _badges[index] = badge;
        }

        _badgeRepository.Save(_badges);
    }
}

答案 1 :(得分:0)

我找到答案问题是调用DialogService来显示弹出窗口,更新xml并返回MainViewModel

DialogService.ShowMessage("You should try another questions set!", "Game Over!", "Main Menu", GoToMainMenu);

这是不可能的,所以我把它改为:

await DialogService.ShowMessage("You should try another questions set!", "Game Over!");
await GoToMainMenu();

以适当的方式从BadgeService调用Update()方法,如下所示:

private async Task GoToMainMenu()
    {
        await _badgeService.Update(SelectedSet.Id);
        ViewModelLocator.Cleanup();
        _navigationService.Navigate("MainPage");
    }