0

我仍在与我的项目作斗争,我几乎不需要任何帮助。在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 时,我无法看到绑定后的变化,但是当我点击绑定Command的按钮调用的Refresh方法时,会加载更改后的数据并且一切正常。返回 MainViewModel 正确绑定数据后如何避免点击刷新?

4

2 回答 2

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");
    }
于 2013-09-12T20:15:57.923 回答
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);
    }
}
于 2013-09-12T19:33:17.207 回答