Microsoft’s XAML provides a way for us to define our UI in a declarative manner. Combined with databinding it means you should – in almost every case – never have to directly reference a UI control to accomplish some work. Add the MVVM design pattern in to the mix and I find myself “rethinking” any implementation where I think x:Name=”” is needed on a XAML control.
Let’s see how we can remove an ad control application-wide and upon purchase of its corresponding IAP without ever naming the control itself.
It’s worthwhile to note that if you put this control its own Grid Row, do not specify the height of the grid row. Instead, specify it as Auto. This ensures that when the Ad Control goes Collapsed, the grid row collapses to 0 as well, freeing up the piece of your UI previously eaten by the ad. Like so:
When working with Microsoft’s PubCenter ad control, the height and width of you ad are pre-determined. It’s therefore recommend you specify this in the control’s declaration (and oftentimes it won’t work if you don’t) like so:
Notice right away the lack of naming of the control. To remove this control from the form, we bind its Visibility property to a ShowAds property on our ViewModel. For your reference, here’s the declaration of the ViewModel for the page (from its Resources area):
And here’s the code for that BoolToVisibility converter:
and its XAML declaration in the page’s Resources area:
Now that we’ve got all that in place, let’s see how we can both pull and trigger the state of Ad Showing in our app.
First thing’s first, the implementation of the state of the Ad Removal product we’re offering. In our ViewModel’s ShowAds property we put:
Our ViewModel might be INotifyPropertyChanged, but we don’t have to worry about that for this property as it’s getter-only. You’ll see later how we trigger its effects to the UI.
Now our ProductPurchaseHelper needs a way to purchase the Ad Removal product, of course:
This method simply executes the Windows Phone Store purchase for an arbitrary product id and, if the product is consumable, reports it as fullfilled. Durable products don’t need this part executed. The salient point of what this does fires the OnProductPurchased event. This is the dude that’ll notify all its subscribers that a product got purchased – hopefully you can see where this is going :) Here’s what that event signature looks like:
In our Page, we’ll want to subscribe to this event like so:
This notifies the UI that the ShowAds property has changed, and it re-evaluates the Visibility of our AdControl. Since the product has been purchased, our Helper class will show as such when its ShowAds property is fetched again:
And the ad control’s visibility property will evaluate to ‘Collapsed’ and hide the control.
Hope you’ve found this helpful!
* Disclaimer: While pulling this code out of an existing app, I augmented the code right in the blog post to strip out my app’s infrastructure. As such this hasn’t been tested exactly as it’s written, but hopefully the idea comes across clearly if it doesn’t work out-of-the-box. Good luck!