Rui Huang
Rui Huang

Reputation: 382

How to call async method in the View Model everytime when page is navigated to

I am using MVVM light and Cimbalino Toolkit in my Project, I want to update my ListView when the page is navigated to. I implemented one async method in the View Model:

private async void getMyNoteList()
     {
         ObservableCollection<Note> list = await _noteSessionService.getNoteList();
         NoteList = new ObservableCollection<Note>(list);        
     }

Here, in my NoteListPageViewModel.cs. I have one property NoteList:

public ObservableCollection<Note> NoteList
    {
        get { return _noteList; }

        set { Set(() => NoteList, ref _noteList, value); }
    }

and it is bounded to ListView in the page.

<ListView  x:Name="NoteListView" ItemsSource="{Binding NoteList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border 
                    BorderBrush="White"
                    BorderThickness="2"
                    CornerRadius="5"
                    Width="360"
                    Margin="10,5">

                    <FlyoutBase.AttachedFlyout>
                        <MenuFlyout>
                            <MenuFlyoutItem Text="Delete"  
                        />

                            <MenuFlyoutItem Text="Edit" 
                         />
                        </MenuFlyout>
                    </FlyoutBase.AttachedFlyout>

                    <StackPanel >
                        <TextBlock
                               FontSize="30" Text="{Binding NoteTitle}"/>
                        <TextBlock
                               FontSize="25"
                               TextWrapping="Wrap" Text="{Binding NoteContent}"/>
                    </StackPanel>

                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Since the note list is added/deleted by other service, I need to call the async method everytime when the page is navigated to, so that the list is the lastest updated.

I put getMyNoteList() here:

 public NoteListPageViewModel(INavigationService navigationService,
        INoteSessionService noteSessionService,
        IMessageBoxService messageBox,
        ILogManager logManager)
    {
        _navigationService = navigationService;
        _noteSessionService = noteSessionService;
        _messageBox = messageBox;
        _logManager = logManager;
        getMyNoteList();

        DeleteComamand = new RelayCommand(
           () =>
           {


           });

        EditCommand = new RelayCommand(
           () =>
           {

           });

    }

and the page can only be updated for the first time navigated to.

Since I am using GalaSoft.MvvmLight, in the viewModelLocator, I have already registered the ViewModel:

SimpleIoc.Default.Register<NoteListPageViewModel>();

Is there any solution that can meet my expectation? In the default relevant Page.xaml.cs, I don't do anything here:

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {


    }

Upvotes: 0

Views: 85

Answers (1)

Depechie
Depechie

Reputation: 6142

Best way, is to use a MVVM Light message. So in the OnNavigatedTo method of the page, just send a NotificationMessage with some string value to indicate that the page has been navigated to.

In your viewmodel register for the NotificationMessage and act upon it to reload your list by using your Async method.

If you don't know the messaging system of MVVM Light - take a look at the in depth tutorial on the MSDN magazine site here https://msdn.microsoft.com/en-us/magazine/dn745866.aspx

Upvotes: 1

Related Questions