W8 RP Third day, hard discoveries

Today implementing commands make me a little crazy. A typical button command Works with issues, and a button inside style Works with more issues. And what about navigating with MVVM more issues. Let’s check how to solve all these matters.

MVVM Command inside Style

Taking care about the previous post, a button has the typical Command:

Command=”{Binding SelectSlotCommand}”

After making several trials I get with the solution to implement in a style. In my case I use a button to represent an item inside the gridview:

Command=”{Binding DataContext.SelectSlotCommand, ElementName=pageRoot}” CommandParameter=”{Binding ElementName=pageRoot}”

The parameter ‘pageRoot’ is neccesary to avoid ‘release preview’ issues.

The Navigation Command in the ViewModel

I get the parameter and casting to MainPage I can get the Frame and Navigate. Other methods does not work because you have to override OnNavigatedFrom in all your pages because there is a null issue

       public SlotsViewModel()
        {
            SelectSlotCommand = new VMCommand<object>((ob) =>
            {
                Actions.Delayed(0.5, () => (ob as MainPage).Frame.Navigate(typeof(LessonsPage)));
            });

            GetSlotsCommand = new VMCommand<object>(GetSlots);
            GetSlots(null);
        }

And OnNavigatedFrom:

protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            //base.OnNavigatedFrom(e);
        }

So we have navigation from the mvvm viewmodel that is great and commanding from the template of the ítems, that is great too. Many headaches solved today.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s