<disclaimer>I'm working on a proof of concept app and the requirements are very "flexible" at the moment. Of course I'd plan things better than this in advance under normal scenarios.</disclaimer>
Starting simply, I want to add an ObservableCollection<MyPin> to my VM. But there's a problem.
public class MyPin { public GeoCoordinate Coordinate { get; set; } public string Name { get; set; } }
The problem is that The GeoCoordinate lives in System.Device.Location and so can't be referenced in the PCL. This means I won't be able to bind to it directly. Frustrating!
Upon reflection this makes sense. That I'm using a GeoCoordinate is an implementation detail of the UI. It could be displayed as anything. It just makes my life a little bit more complicated.
In reality my class should be more generic:
public class MyPin { public double Longitude { get; set; } public double Latitude { get; set; } public string Name { get; set; } }
But this doesn't help with my binding.
So, I need to be able to convert a Latitude and Longitude value into a GeoCoordinate. Sounds like a job for an IValueConverter.
I'm not a big fan of using ValueConverters. They have 2 issues in my mind.
1. They are a performance overhead.
2. They are hard to write coded tests for.
These aren't really issues any more though.
1. This was true in the early days of WP7, where having lots of converters could noticeably impact page loading, but this isn't really a problem any more. You'd have to use loads for this to be an issue on WP8 and I'm only using it where I have to.
2. In this instance I'm only using them for a presentation details though so a lack of automated tests for how locations are displayed on the map isn't a big deal.
So let's add a converter:
Then I just define my resource:public class PinToGeoCoordinateConverter : IValueConverter { public object Convert(object value, Type targetType, object para, CultureInfo culture) { var pin = value as MyPin; return pin != null ? new GeoCoordinate(pin.Latitude, pin.Longitude) : new GeoCoordinate(); } public object ConvertBack(object valu, Type targetType, object para, CultureInfo culture) { throw new NotImplementedException(); } }
<phone:PhoneApplicationPage.Resources> <converters:PinToGeoCoordinateConverter x:Key="PinToGeo" /> </phone:PhoneApplicationPage.Resources>
and then I can use it
<map:Map x:Name="TheMap"> <toolkit:MapExtensions.Children> <toolkit:MapItemsControl> <toolkit:MapItemsControl.ItemTemplate> <DataTemplate> <toolkit:Pushpin GeoCoordinate="{Binding Converter={StaticResource PinToGeo}}" Content="{Binding Name}" /> </DataTemplate> </toolkit:MapItemsControl.ItemTemplate> </toolkit:MapItemsControl> </toolkit:MapExtensions.Children> </map:Map>
Simples.
All in all this feels like a bit of a kludge. At first it felt like a very bad approach but as time goes on I'm feeling more comfortable with it. I don't know if this is just be getting used to the idea or if it's really not that bad after all.
If you have any better approaches to dealing with this scenario or there are some issues you see with the above I'd love to hear from you.
On of my biggest painpoints with PCLs was not being able to use Color in VMs, I mean, how common is that...
ReplyDeleteSo yeah, as unnecessary as it sounds, bring on HexToColorConverter and HexToSolidColorBrushConverter :)
@Andrej I've been pointed to https://github.com/paulcbetts/splat as a solution for handling Colors, Points and Bitmaps. It's still not a perfect solution but worth a look.
ReplyDeleteSince GeoCoordinate is not Sealed you can refactor an IGeoCoordinate and RealGeoCoordinate : GeoCoordinate, IGeoCoordinate - this is the one you use at runtime and is interchangeable with GeoCoordinate. You can then create FakeGeoCoordinate : IGeoCoordinate and you can test.
ReplyDeleteIn line Coach Outlet Online is bringing Michael Kors Outlet unique Coach Factory Outlet to a Toms Shoes USA shoe for Cheap Jerseys Spring/Michael Kors Outlet 2014 Coach Factory Outlet, and this Coach Outlet, it’s a Coach Outlet. The Coach Outlet Sale Sandal Giuseppe Zanotti Outlet.Supermodel mom Coach Factory Outlet was Coach Factory Outlet Online being all kinds of hot this weekend Coach Factory Outlet she Coach Store Online out in a Michael Kors Outlet-turning, Celine Outlet I did, I was Celine Bags possessed by Fitflops Flipflops suit madness.Fitflops USA-there string Michael Kors during Michael Kors Store vacation in Tomss Hoes Bahamas. Fitflop Footwear by her family, the 40-year-old looked happy and relaxed - Fitflops Outlet, uh, did we Fitflops Sandals super hot? - as she Giuseppe Zanotti by the pool.I've Giuseppe Zanotti Designer making my own vanilla extract for years. I love it! It's cost-effective, and in my opinion, Vanilla extract is also (easily) the most used extract in my Prada Outlet , sort of pretty (although it kind of looks like spiders too) tastes way better than imitation vanilla extract.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletechicago bears, http://www.chicagobearsjerseys.us/
ReplyDeletenobis outlet, http://www.wellensteyn.com.co/
tory burch shoes, http://www.toryburchoutletonline.in.net/
replica watches, http://www.replicawatchesforsale.us.com/
ed hardy shoes,ed hardy,ed hardy clothing,ed hardy shoes for women,ed hardy clothing for men,ed hardy perfume,ed hardy watches
christian louboutin, http://www.scarpelouboutin.it/
ugg outlet store, http://www.uggboots-outlet.co.uk/
nike huarache, http://www.nike-airhuarache.co.uk/
nike air max, http://www.airmax2015.in.net/
ray ban, http://www.occhiali-rayban.it/
louis vuitton bags on sale, http://www.louisvuittonbag.us.com/
nike air max,nike roshe,nike huarache
canada goose outlet, http://www.canadagoose-coats.us.com/
hermes belt for men, http://www.hermesbelts.us/
north face jackets, http://www.thenorthfaceclearances.us.com/
ray ban sunglasses,ray ban sunglasses outlet,ray ban,rayban,ray bans,ray ban outlet,ray-ban,raybans,ray ban wayfarer,ray-ban sunglasses,raybans.com,rayban sunglasses,cheap ray ban
cheap oakley sunglasses, http://www.oakleysunglassesdiscount.us.com/
woolrich clothing, http://www.canadagoosejackets.in.net/
kkkk10 13
jianbin1208
ReplyDeletemichael kors uk outlet
fred perry shirts
lebron james shoes,lebron 12,lebron 12 shoes,lebron sneakers,lebron iv,lebron 11,lebron 10
nike air max 2014
timberland outlet
adidas jeremy scott
adidas outlet store
canada goose jackets
nike air huarache,nike huarache,nike huarache sneakers,nike huarache shoes
uggs outlet
north face jackets
football shirts uk,soccer jerseys uk,cheap soccer jerseys uk
coach outlet
winter coats outlet
ray ban sale
chanel handbags
michael kors usa
louis vuitton outlet online
lebron 12
michael kors outlet online
uggs outlet
ray ban sunglasses outlet,ray ban sunglasses,ray ban,rayban,ray bans,ray ban outlet,ray-ban,raybans,ray ban wayfarer,ray-ban sunglasses,raybans.com,rayban sunglasses,cheap ray ban
canada goose jackets
cheap mlb jerseys
uggs outlet
cheap ugg boots
winter jackets outlet
los angeles clippers
dallas cowboys