Link to home
Avatar of Waterside
Waterside

asked on

Conditional Compilation Problem

I'm trying to create a Universal App and I'm following this example http://blogs.msdn.com/b/bingdevcenter/archive/2014/06/24/using-maps-in-universal-apps.aspx

I've run the MapView.cs file through a VB converter but the result fails on  Me.Children.Add(_map). as it's undeclared or inaccessible  The Code is ..

#If WINDOWS_PHONE_APP Then
Imports Windows.UI.Xaml.Controls.Maps
Imports Windows.UI.Xaml.Media
Imports Windows.UI.Xaml.Shapes
Imports Windows.UI.Xaml
#Elif WINDOWS_APP Then
Imports Bing.Maps
#End If

Namespace App3
    Public Class MapView
        Inherits Grid
#If WINDOWS_APP Then
		Private _map As Map
		#Elif WINDOWS_PHONE_APP Then
		Private _map As MapControl
#End If

        Public Sub New()
#If WINDOWS_APP Then
			Private _map = New Map()
			#Elif WINDOWS_PHONE_APP Then
			Private _map = New MapControl()
#End If

            Me.Children.Add(_map)
        End Sub
    End Class
End Namespace

Open in new window

Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Would rearranging your code this way help?
#If WINDOWS_PHONE_APP Then
Imports Windows.UI.Xaml.Controls.Maps
Imports Windows.UI.Xaml.Media
Imports Windows.UI.Xaml.Shapes
Imports Windows.UI.Xaml
#Elif WINDOWS_APP Then
Imports Bing.Maps
#End If

Namespace App3
    Public Class MapView
        Inherits Grid
#If WINDOWS_APP Then
		Private _map As Map
		#Elif WINDOWS_PHONE_APP Then
		Private _map As MapControl
#End If

        Public Sub New()
#If WINDOWS_APP Then
			_map = New Map()
			Me.Children.Add(_map)
			#Elif WINDOWS_PHONE_APP Then
			_map = New MapControl()
			Me.Children.Add(_map)
#End If
            
        End Sub
    End Class
End Namespace

Open in new window

Also consider using a more OOP like approach. Use the strategy pattern instead of conditional compilation.

Create your own IMap interface. Derive two classes MapPhone and MapApp from it. Use an IoC container to inject the correct map "strategy".

Then your code becomes much more cleaner and easier to read. E.g. (untested)
Namespace App3
    Public Class MapView
        Inherits Grid

		Private _map As IMap

        Public Sub New()
			_map = IoC.Resolve(Of IMap)()
			Me.Children.Add(_map)            
        End Sub
    End Class
End Namespace

Open in new window

There are a few strange things in your code. These damn converters always do bad things, even for simple code. You are always better to rewrite the code, specially when it is so short.

The variable is declared both at the class level and in the constructor????

The variable is declared as Private in the constructor. You cannot do that. Only Dim and Static are accepted inside a method. And because it is already declared at the class level, you do not need to declare it in there, simply call the constructor as shown by MlandaT.

As for the conditional compilation, do you see parts of the code in color or is almost everything grey? If the 2 clauses of your conditions both show in grey, then something is wrong with your setup. You might have copied you code in the wrong type of project, in which case the conditional compilation variables are not defined.
Avatar of Waterside
Waterside

ASKER

MlandaT's suggestion has fixed the Add(_map) error but I do have grey code every where.

Any ideas James ?
If you look at the project in the Solution Explorer, do you see (Universal Windows) besides the name of the project?

If not, then you are not in a Universal project and the constants are not defined. Simply create a new project by making sure that you are selecting a Universal Windows project as the project template.
Yes it's Universal.  But I have no template for VB.net so I had to add the Win8.1 and WP8.1 projects.

The Import statements also show a grey.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial