4
votes

In Windows 8.1, when you add a View/Page based upon a Windows 8.1 Basic Page it no longer inherits from LayoutAware class because it no longer exists in Win 8.1. All Basic Pages now inherit directly from Page class. Additionally, it no longer has an OnNavigatedTo/OnNavigatedFrom event as the Win8.1 Basic page now leverages the NavigationHelper class and calls this.navigationHelper.LoadState and this.navigationHelper.SaveState event handlers. If using the TipCalc sample and adding a Windows Store Basic Page, TipView, initial page would look like:

public sealed partial class TipView : Page
{

    private NavigationHelper navigationHelper;
    private ObservableDictionary defaultViewModel = new ObservableDictionary();

    /// <summary>
    /// This can be changed to a strongly typed view model.
    /// </summary>
    public ObservableDictionary DefaultViewModel
    {
        get { return this.defaultViewModel; }
    }

    /// <summary>
    /// NavigationHelper is used on each page to aid in navigation and 
    /// process lifetime management
    /// </summary>
    public NavigationHelper NavigationHelper
    {
        get { return this.navigationHelper; }
    }

    public TipView()
    {
        this.InitializeComponent();
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += navigationHelper_LoadState;
        this.navigationHelper.SaveState += navigationHelper_SaveState;
    }

Since the TipView page now inherits directly from Page, if you change the TipView Page to inherit from MvxStorePage like noted below:

public sealed partial class TipView : MvxStorePage
  {

      private NavigationHelper navigationHelper;
      private ObservableDictionary defaultViewModel = new ObservableDictionary();

Since the Page is a partial class the following error occurs:

Partial declarations of 'TipCalc.CrossPlat.WinStore.Views.TipView' must not specify different base classes.

And even if it would allow for the change in the base class to MvxStorePage, you cannot add base.OnNavigatedTo(e) in LoadState event handler as noted:

private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
   base.OnNavigatedTo(e);
}

because e parameter in OnNavigatedTo is looking for NavigationEventArgs vs. LoadStateEventArgs.

Any help will be greatly appreciated as I need to complete my cross-platform PCL project which has a Windows 8.1 implementation.

1
Try stackoverflow.com/questions/19925297/… - basic procedure is the same. onnavigatedto still existsStuart
Hi Stuart! I looked at that question but in Win 8.1 LayoutAware.cs doesn't exist so I can't do: public sealed partial class TipView : MvxStorePage because it is required in 8.1 that I inherit from Page class now. So I can't start the app w/TipView page if I change App.xaml.cs I have hacked it like this: public sealed partial class TipView : Page, IMvxStoreView, IMvxView, IMvxDataConsumer and implemented SuspensionManager and ViewModel public new IMvxViewModel ViewModel { get { return _viewModel; } set { _viewModel = (TipViewModel)value; } }TaraW
MvxStorePage inherits from Page - so if TipView inherits from MvxStorePage, then TipView inherits from Page - github.com/MvvmCross/MvvmCross/blob/v3/Cirrious/… (CommonLayoutAware also used to inherit from Page too - so all 8.1 is doing is removing a layer)Stuart
Stuart: Agreed that makes perfect sense, it is simple inheritance. That was the very first thing I tried was to directly change public class TipView: MvxStorePage but Visual Studio will not compile with the above code and is throwing the following error: Partial declarations of 'TipCalc.CrossPlat.WinStore.Views.TipView' must not specify different base classes. Am I missing another class that I need to change to make this compile?TaraW
Just make sure you change the base class in both the xaml and the c# - that linked question has the stepsStuart

1 Answers

6
votes

I found the answer to how to resolve this without the hack I noted below, but the hack lead me to the answer. I am posting for anyone else that may have the issue in wanting to add a Win8.1 page that inherits from the MvvmCross Store Page.

The answer lies in that the Win8/Win8.1 Page class that is found in Page.xaml.cs is a partial class which the entire completed class definition is built/comprised both in CS and in XAML which are collectively compiled to build entire class for the page.

So if you have a TipView page, change the TipView page inherit from MvxStorePage by altering code as follows:

public sealed partial class TipView: MvxStorePage

then go to the TipView page XAML file and change declaration of the default root Page node from:

<Page
    x:Name="pageRoot"
    x:Class="TipCalc.CrossPlat.WinStore.Views.TipView"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TipCalc.CrossPlat.WinStore.Views"
    xmlns:common="using:TipCalc.CrossPlat.WinStore.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

to be changed to the following Page node declaration and add MvvmCross MvxStorePage namespace as follows:

<StorePage:MvxStorePage
    x:Name="pageRoot"
    x:Class="TipCalc.CrossPlat.WinStore.Views.TipView"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TipCalc.CrossPlat.WinStore.Views"
    xmlns:StorePage="using:Cirrious.MvvmCross.WindowsStore.Views"
    xmlns:common="using:TipCalc.CrossPlat.WinStore.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"> 

Key thing to note: The StorePage namespace in StorePage: MvxStorePage is being referenced by adding the namespace to Cirrious.MvvmCross.WindowsStore.Views by including the line:

xmlns:StorePage="using:Cirrious.MvvmCross.WindowsStore.Views"

in the XAML page declaration. Once I have done this, it works like a charm.