Why is #if WINDOWS_PHONE_APP directive not working?...

Sep 19, 2014 at 5:58 PM
IDE: VS Express 2013 for Windows
Project: Universal
IoC: MEF
Target OS: 8.1

Hi,

In testing on my Windows Phone App on my Device the #if WINDOWS_PHONE_APP directive is not working in my ViewModel, I'm injecting my ViewModels into the DataContext of the my Page's like this.

Injection:
[Export(typeof(HubPage))]
public sealed partial class HubPage : VisualStateAwarePage
{
    public HubPage()
    {
        this.InitializeComponent();
        Debug.WriteLine("HubPage InitializeComponent");

#if WINDOWS_PHONE_APP
            Debug.WriteLine("directive WINDOWS_PHONE_APP is defined...");
#elif WINDOWS_APP
            Debug.WriteLine("directive WINDOWS_APP is defined...");
#else
            Debug.WriteLine("Error directive WINDOWS_PHONE_APP/WINDOWS_APP not defined...");
#endif

    }

    [Import]
    public NathsarTS.UILogic.Interfaces.IHubPageViewModel ViewModel
    {
        set
        {
            this.DataContext = value;
        }
        get
        {
            return DataContext as NathsarTS.UILogic.Interfaces.IHubPageViewModel;
        }
    }

    /// <summary>
    /// Called when a part's imports have been satisfied and it is safe to use.
    /// </summary>
    [OnImportsSatisfied]
    public void OnImportsSatisfied()
    {
        // IPartImportsSatisfiedNotification is useful when you want to coordinate doing some work
        // with imported parts independent of when the UI is visible.
        Debug.WriteLine("HubPage OnImportsSatisfied instantiation");

        this.ViewModel.ObserverHubPageLoadedEventArgs =
            Observable.FromEventPattern<object, RoutedEventArgs>(this.pageRoot, "Loaded");
        this.ViewModel.DisposableHubPageLoadedEvent =
            this.ViewModel.ObserverHubPageLoadedEventArgs.Subscribe(evt => this.ViewModel.OnPageRootHubPageLoadedEvent(evt.Sender, evt.EventArgs));

        //NathsarTS.UILogic.Interfaces.IObservableService ObservableService = ServiceLocator.Current.GetInstance<NathsarTS.UILogic.Interfaces.IObservableService>();
        //this.ViewModel.NavigationHelper = new NathsarTS.Common.Logic.NavigationHelper(this);
        //this.ViewModel.NavigationHelper.LoadState += this.ViewModel.NavigationHelperLoadState;
    }
}
In my HubPage (above) and App (below) I check to see if the directive is defined:
        public App()
        {
#if WINDOWS_PHONE_APP
            Debug.WriteLine("directive WINDOWS_PHONE_APP is defined...");
#elif WINDOWS_APP
            Debug.WriteLine("directive WINDOWS_APP is defined...");
#else
            Debug.WriteLine("Error directive WINDOWS_PHONE_APP/WINDOWS_APP not defined...");
#endif
            this.InitializeComponent();
            this.RequestedTheme = ApplicationTheme.Light;
        }
Results: directive WINDOWS_PHONE_APP is defined...

In my ViewModel I check again after the Page is Loaded:
        public async void OnPageRootHubPageLoadedEvent(object sender, RoutedEventArgs args)
        {
            Debug.WriteLine("OnPageRootHubPageLoadedEvent instantiation");

#if WINDOWS_PHONE_APP
            Debug.WriteLine("OnPageRootHubPageLoadedEvent (Phone) instantiation");

            var sampleDataGroups = await SampleDataSource.GetGroupsAsync();
            this.DefaultDataModel["Groups"] = sampleDataGroups;
#elif WINDOWS_APP
            Debug.WriteLine("OnPageRootHubPageLoadedEvent (Windows) instantiation");

            var sampleDataGroup = await NathsarTS.Common.DataModel.SampleDataSource.GetGroupAsync("Group-4");
            this.DefaultDataModel["Section3Items"] = sampleDataGroup;
#else
            Debug.WriteLine("Error directive WINDOWS_PHONE_APP/WINDOWS_APP not defined...");
            await Task.FromResult<object>(null);
#endif
        }
Results: Error defining Windows/Phone...

I'm assuming its because the ViewModel become apart of the XAML DataContext? If so, looks like I may have to use a property setting for this?
Sep 21, 2014 at 3:22 AM
Edited Sep 21, 2014 at 3:23 AM
As an result of the directive not available after injection, I had to create a Boolean Property in my ViewModel and set it during __OnImportsSatisfied__():
 /// <summary>
        /// Called when a part's imports have been satisfied and it is safe to use.
        /// </summary>
        [OnImportsSatisfied]
        public void OnImportsSatisfied()
        {
            // IPartImportsSatisfiedNotification is useful when you want to coordinate doing some work
            // with imported parts independent of when the UI is visible.
            Debug.WriteLine("HubPage OnImportsSatisfied instantiation");

#if WINDOWS_PHONE_APP
            Debug.WriteLine("directive WINDOWS_PHONE_APP is defined...");
            this.ViewModel.Windows_Phone_App = true;
#elif WINDOWS_APP
            Debug.WriteLine("directive WINDOWS_APP is defined...");
            this.ViewModel.Windows_Phone_App = false;
#else
            Debug.WriteLine("Error directive WINDOWS_PHONE_APP/WINDOWS_APP not defined...");
#endif
        }