I spent most of the day today messing around with the Halo Reach Stats API. I wrote some C# wrappers around the JSON services and put it up on Codeplex for anyone who might be interested.
There are Win Phone 7 versions and regular Windows class libraries. I even wrote a simple little proof of concept app that will get a player's details by Gamertag.
Everything is pretty untested at this point. But I'd like to add a video viewer to the Win Phone app sometime tomorrow.
See the project on Codeplex for more code samples and source code downloads.
Now Playing - Otis Taylor - Nasty Letter
Showing posts with label mobile. Show all posts
Showing posts with label mobile. Show all posts
Saturday, October 2, 2010
Sunday, August 29, 2010
Show Custom In-House Ads in Win Phone 7 with MoAds
One of the problems I've been running into is a lack of ad service providers to get ads from. After putting together a previous post showing the MoAds control using the AdMob Ad Adapter I decided someone might get some use out of creating a custom ad service to serve their own "In House" Ads in their Win Phone 7 application.
Just want the code? Get it here
There is also the sort of unsettling issue of Microsoft's Developer agreement being sort of fuzzy on advertising services (as pointed out by this MobileTechWorld article about MoAds).
At any rate, here's a brief run down of creating your own custom ad service using a new MVC 2 Website and some JSON services.
The Custom Ad Adapter generally has an underlying service component (in our example it's called CustomAdService) that calls out to some service and gets some ad information, massages the response and fires our GotAdResponse event. Here's a brief run down of what our CustomAdAdapter class looks like.
Once we have our Custom Ad Adapter in place we can plug it in to our AdDisplay control and start serving ads.
And that's about it. Hopefully I'll find some other ad service providers to implement soon. If anyone has any recommendations I'd be happy to hear them.
Grab the updated example source from here or the Project Page at BitBucket (Codeplex is still Garbage).
Now Playing - Notorious B.I.G - Mo Money Mo Problems
Just want the code? Get it here
There is also the sort of unsettling issue of Microsoft's Developer agreement being sort of fuzzy on advertising services (as pointed out by this MobileTechWorld article about MoAds).
At any rate, here's a brief run down of creating your own custom ad service using a new MVC 2 Website and some JSON services.
The Web Services Site - .Net MVC 2
In the example provided, I've created a new .Net MVC 2 website and added an AdController. The AdController is responsible for handling our AdRequests. In this case, we add two methods; Fetch and Click. The Fetch will be responsible for returning the Ad information (ImageUrl, AdText and ClickUrl) and the Click will redirect to the actual Ad Url after recording the client click.public ActionResult Fetch(string pubId, string clientId = "")
{
// TODO: You'd probably want to keep track of fetches by PubId for analytics, but this is just a demo.
var ad = AdRepository.GetRandomAd();
Impression impression;
var clickUrl = GenerateClickUrl(ad, out impression);
ImpressionRepository.Impressions.Add(impression);
return new JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new FetchResponse()
{
ClickUrl = clickUrl,
AdText = ad.AdText,
ImageUrl = ad.ImageUrl
}
};
}
public ActionResult Click(string id)
{
// TODO: Track the click for analytics and what-not.
var impression = ImpressionRepository.Impressions.FirstOrDefault(x => x.Token == id);
if (impression == null)
return View(); // TODO: return not found result.
var ad = AdRepository.GetAdById(impression.AdId);
return new RedirectResult(ad.AdUrl);
}
Creating a Custom Ad Adapter
The Custom Ad Adapter generally has an underlying service component (in our example it's called CustomAdService) that calls out to some service and gets some ad information, massages the response and fires our GotAdResponse event. Here's a brief run down of what our CustomAdAdapter class looks like.
public class CustomAdAdapter : IAdAdapter
{
private CustomAdService adService = new CustomAdService();
/// <summary>
/// Gets or sets the publisher id.
/// </summary>
/// <value>The publisher id.</value>
public string PublisherId { get; set; }
/// <summary>
/// Gets or sets the client id.
/// </summary>
/// <value>The client id.</value>
public string ClientId { get; set; }
public CustomAdAdapter()
{
adService.GotResult += new EventHandler<Service4u2.Common.EventArgs<CustomAdFetchResponse>>(adService_GotResult);
adService.GotError += new EventHandler<Service4u2.Common.EventArgs<Exception>>(adService_GotError);
}
void adService_GotError(object sender, Service4u2.Common.EventArgs<Exception> e)
{
if (GotError != null)
GotError(this, e);
}
void adService_GotResult(object sender, Service4u2.Common.EventArgs<CustomAdFetchResponse> e)
{
var adResponse = new AdResponse()
{
AdText = e.Argument.AdText,
ImageUrl = e.Argument.ImageUrl,
ClickUrl = e.Argument.ClickUrl
};
if (GotAdResponse != null)
GotAdResponse(this, new Service4u2.Common.EventArgs<AdResponse>() { Argument = adResponse });
}
#region IAdAdapter Members
public event EventHandler<Service4u2.Common.EventArgs<AdResponse>> GotAdResponse;
public event EventHandler<Service4u2.Common.EventArgs<Exception>> GotError;
public void GetAdResponseAsync()
{
adService.FetchAdAsync(this.PublisherId, this.ClientId);
}
#endregion
}
Hooking up our Custom Ad Adapter in XAML
Once we have our Custom Ad Adapter in place we can plug it in to our AdDisplay control and start serving ads.
<!-- Custom Ad Service example -->
<moad:AdDisplay
Style="{StaticResource AnimationAdMobStyle}"
VerticalAlignment="Bottom"
Foreground="White"
RefreshSeconds="30">
<moad:AdDisplay.AdAdapter>
<local:CustomAdAdapter
PublisherId="SomePubId"
ClientId="SomeClientId"/>
</moad:AdDisplay.AdAdapter>
</moad:AdDisplay>
And that's about it. Hopefully I'll find some other ad service providers to implement soon. If anyone has any recommendations I'd be happy to hear them.
Grab the updated example source from here or the Project Page at BitBucket (Codeplex is still Garbage).
Now Playing - Notorious B.I.G - Mo Money Mo Problems
Wednesday, July 28, 2010
A Clearable TextBox for Win Phone 7
Update: I've updated this UserControl to a Custom Control for better templating and posted it for download. Check it out in the Clearable TextBox Custom Control post.
After seeing a tweet about the lack of a clearable text box similar to the IPhone by Eric Malamisura, I was inspired to whip together a simple control that fills this need.
Here is some code for the control, in case you don't want to download the full example. Just create a new UserControl called ClearableEditBox and paste these parts in the relevant places.
You can also download a sample project containing the control here.
Clearable TextBox UserControl
Now Playing: Eminem - Airplanes
After seeing a tweet about the lack of a clearable text box similar to the IPhone by Eric Malamisura, I was inspired to whip together a simple control that fills this need.
Here is some code for the control, in case you don't want to download the full example. Just create a new UserControl called ClearableEditBox and paste these parts in the relevant places.
<!-- Put inside your new UserControl -->
<Grid x:Name="LayoutRoot">
<TextBox
MinWidth="50"
Text="{Binding ElementName=uiThis, Path=Text, Mode=TwoWay}"
x:Name="editBox"/>
<Button
MinWidth="30"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Click="clrBtn_Click"
x:Name="clrBtn">
<Button.Template>
<ControlTemplate>
<TextBlock
FontSize="25"
Margin="0 0 20 0"
Foreground="Gray"
FontWeight="Bold"
Text="X"/>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
// Put inside your code behind for your new UserControl.
public partial class ClearableTextBox : UserControl, INotifyPropertyChanged
{
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for EditText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(ClearableTextBox), new PropertyMetadata(string.Empty, HandleTextChanged));
public ClearableTextBox()
{
InitializeComponent();
}
private static void HandleTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var box = obj as ClearableTextBox;
if (null != box)
box.OnPropertyChanged("Text");
}
private void clrBtn_Click(object sender, RoutedEventArgs e)
{
Text = string.Empty;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
if (null != PropertyChanged)
PropertyChanged(null, new PropertyChangedEventArgs(name));
}
#endregion
}
You can also download a sample project containing the control here.
Clearable TextBox UserControl
Now Playing: Eminem - Airplanes
Monday, July 26, 2010
Updated Win Phone 7 Busy Service That Does Not Change RootVisual
Evidently, you are not supposed to change the RootVisual in a Win Phone 7 application. There was a forum post posed by Matt Hidinger and answered by Peter Torr about Changing the RootVisual that indicates this might cause problems during the approval process.
As a result, I've updated my sample to not require wrapping the root level PhoneApplicationFrame with a Grid. It turns out, most pages already have a Grid as their LayoutRoot element and you can just pass this in to a new RootPaneBusyService and use it in the same manner. So instead of passing the App.Current.RootVisual as Grid, we pass the LayoutRoot.
I've also updated the CreateBusyElement method to detect how many rows and columns are in the passed in root visual and set the ColumnSpan and RowSpan accordingly.
Download the updated sample
As a result, I've updated my sample to not require wrapping the root level PhoneApplicationFrame with a Grid. It turns out, most pages already have a Grid as their LayoutRoot element and you can just pass this in to a new RootPaneBusyService and use it in the same manner. So instead of passing the App.Current.RootVisual as Grid, we pass the LayoutRoot.
protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var busyServ = new RootPanelBusyService(this.LayoutRoot);
this.DataContext = new MainPageVM(busyServ);
}
I've also updated the CreateBusyElement method to detect how many rows and columns are in the passed in root visual and set the ColumnSpan and RowSpan accordingly.
// Set the row and column span's if the root is a grid.
if (rootVisual is Grid)
{
var gridRoot = rootVisual as Grid;
int rowSpan = 1;
if (gridRoot.RowDefinitions != null)
rowSpan = gridRoot.RowDefinitions.Count + 1;
int columnSpan = 1;
if (gridRoot.ColumnDefinitions != null)
columnSpan = gridRoot.ColumnDefinitions.Count + 1;
Grid.SetRowSpan(root, rowSpan);
Grid.SetColumnSpan(root, columnSpan);
}
Download the updated sample
Now Playing: The Roots - Get Busy
Subscribe to:
Posts (Atom)


