Frage

I am starting to implement MVVM in my application and got an issue of knowing when the user navigated to the view.

To navigate between views, I can just use the navigationService.Navigate(...);

How do I check when I navigated to the view? May I use the event navigationService.Navigated?

Is there no other method I can use like OnNavigatedTo that the page itself provide?

War es hilfreich?

Lösung 3

Thanks for the answers provided. Both were helpful over a period of time until I decided to create a custom implementation of the navigation service that has been created by a few people. I then made a contribution to the Cimbalino toolkit to suggest this and it has been introduced a while back.

I my personal opinion, that solves my issue the best. Have a look at the navigation service in there. The Navigated event pretty much solves my issue I had.

https://github.com/Cimbalino/Cimbalino-Toolkit

It basically comes down to this (in your viewmodel):

_navigationService.Navigated += OnNavigated;

Andere Tipps

XAML:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71" 

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
 DataContext="{Binding titleSearchViewModel, Source={StaticResource Locator}}">
    <i:Interaction.Triggers>
        <i:EventTrigger>
            <cmd:EventToCommand Command="{Binding PageLoaded, Mode=OneWay}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

VM:

 private RelayCommand _PageLoaded;
 public RelayCommand PageLoaded
        {
            get
            {
                if (_PageLoaded == null)
                {
                    _PageLoaded = new RelayCommand(
                                    () => Loaded()
                        );
                }
                return _PageLoaded;
            }
        }

In case this question is still actual, i prefer this solution: http://www.geoffhudik.com/tech/2010/10/10/another-wp7-navigation-approach-with-mvvm.html

If to use it, it is possible to send recipient ViewModel's parameters from the sender ViewModel:

SendNavigationMessage(Settings.NAVIGATION_PRODUCTS_SUBCATEGORIES, 
    new Dictionary<string, object> { { "SelectedIndex", Int32.Parse(item.id) } });

And receiver should define in xaml:

NavigatedToCommand="{Binding RefreshCommand}"

And then in receiver ViewModel:

public ICommand RefreshCommand // Should be set as NavigatedToCommand="{Binding RefreshCommand}" in xaml
{
    get { return new RelayCommand(Refresh); }
}

public void Refresh()
{       
    _dataService.GetList(SelectedIndex, DownloadedCallback); // So, this would be called automatically after navigating is complete. SelectedIndex is updated at this moment.
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top