How do I update the parent viewmodel when child vi

2019-01-26 00:23发布

问题:

In my first view model (renamed to MainViewModel) I have a list of ActionViewModels. In my xaml i have a listbox which is bound to the list, in the listbox i have a template which binds to properties from the ActionViewModel.

So far so good and everything works. When selecting one of the listitems i navigate to an ActionViewModel and pass the id with it. The ActionViewModel retrieves information from a static list in memory from which the MainViewModel also retrieved the information to create the list of actionviewmodels.

So far still so good, i can edit the properties, all the bindings do work fine and i'm all happy. By clicking the save button the information is gathered and stored in the static list. When i hit the back button i go back to the list, but unfortunately the values showing there are still the same, is there some way to send a command to reload the items in the list? To pass a complete viewmodel as reference to a new ActionViewModel? Or some property which tells the parent 'this viewmodel in your list has been updated'?

I am sure the above text is a bit confusing, so here is some code to clarify it a bit (hopefully)

MainViewModel.cs

private List<ActionViewModel> _actionViewModels;
public List<ActionViewModel> ActionViewModels
{
    get { return _actionViewModels; }
    set { _actionViewModels = value; RaisePropertyChanged(() => ActionViewModels); }
}


private Cirrious.MvvmCross.ViewModels.MvxCommand<int> _navigateToAction;
public System.Windows.Input.ICommand NavigateToAction
{
    get
    {
        _navigateToAction = _navigateToAction ?? new Cirrious.MvvmCross.ViewModels.MvxCommand<int>((action) => NavigateToTheDesiredAction(action));
        return _navigateToAction;
    }
}

private void NavigateToTheDesiredAction(int action)
{
    ShowViewModel<ActionViewModel>(new { id = action });
}

// Get DTOs from server or from cache and fill the list of ActionViewModels
public async Task Load()
{
    ActionService actionService = new ActionService();

    List<ActionViewModel> actionViewModels = new List<ActionViewModel>();

    MyActions = await actionService.GetMyActions();
    foreach (ActionDTO action in MyActions)
    {
        ActionViewModel actionViewModel = new ActionViewModel();
        await actionViewModel.Load(action.id);
        actionViewModels.Add(actionViewModel);
    }

    ActionViewModels = actionViewModels;
}

ActionViewModel.cs

public int ID
{
    get { return TheAction.id; }
    set { TheAction.id = value; RaisePropertyChanged(() => ID); }
}

public string Title
{
    get { return TheAction.Title; }
    set { TheAction.Title = value; RaisePropertyChanged(() => Title); }
}

public async Task Load(int actionId)
{
    ActionDTO TheAction = await actionService.GetAction(actionId);
    this.ID = TheAction.id;
    this.Title = TheAction.Title;
}

private Cirrious.MvvmCross.ViewModels.MvxCommand _save;
public System.Windows.Input.ICommand Save
{
    get
    {
        _save = _save ?? new Cirrious.MvvmCross.ViewModels.MvxCommand(PreSaveModel);
        return _save;
    }
}

private void PreSaveModel()
{
    SaveModel();
}

private async Task SaveModel()
{
    ValidationDTO result = await actionService.SaveAction(TheAction);
}

ActionService.cs

public static List<ActionDTO> AllActions = new List<ActionDTO>();

public async Task<ActionDTO> GetAction(int actionId)
{
    ActionDTO action = AllActions.FirstOrDefault(a => a.id == actionId);
    if (action == null)
    {
        int tempActionId = await LoadAction(actionId);
        if (tempActionId > 0)
            return await GetAction(actionId);
        else
            return new ActionDTO() { Error = new ValidationDTO(false, "Failed to load the action with id " + actionId, ErrorCode.InvalidActionId) };
    }
    return action;
}

private async Task<int> LoadAction(int actionId)
{
    ActionDTO action = await webservice.GetAction(actionId);
    AllActions.Add(action);
    return action.id;
}

public async Task<ValidationDTO> SaveAction(ActionDTO action)
{
    List<ActionDTO> currentList = AllActions;
    ActionDTO removeActionFromList = currentList.FirstOrDefault(a => a.id == action.id);
    if (removeActionFromList != null)
        currentList.Remove(removeActionFromList);

    currentList.Add(action);
    AllActions = currentList;
    return await webservice.SaveAction(action);
}

回答1:

There are 3 ways I can think of that would allow you to do this.

  1. The ActionService could send out some sort of notification when data changes. One easy way to do this is to use the MvvmCross Messenger plugin. This is the way the CollectABull service works in CollectionService.cs in the N+1 days of mvvmcross videos (for more info watch N=13 in http://mvvmcross.wordpress.com)

    This is the approach I generally use. It has low overhead, uses WeakReferences (so doesn't leak memory), it is easily extensible (any object can listen for changes), and it encourages loose coupling of the ViewModel and Model objects

  2. You could implement some kind of Refresh API on the list ViewModel and could call this from appropriate View events (e.g. ViewDidAppear, OnNavigatedTo and OnResume).

    I don't generally use this approach for Refreshing known data, but I have used it for enabling/disabling resource intensive objects - e.g. timers

    For certain shape of model data (and especially how often it changes), then I can imagine scenarios where this approach might be more efficient than the messenger approach.

  3. You could extend the use of INotifyPropertyChanged and INotifyCollectionChanged back into your model layer.

    I've done this a few times and it's worked well for me.

    If you do choose this approach, be careful to ensure that all Views do subscribe to change events using WeakReference subscriptions such as those used in MvvmCross binding - see WeakSubscription. If you didn't do this, then it could be possible for the Model to cause Views to persist in memory even after the UI itself has removed them.