The XBox 360 Widget that I use on this site was fairly straight forward to build thanks to mads and the team for making such an awesome framework for widgets. This post will run you through how to create such a widget.
It is fairly easy to create a widget as all you need to have is an edit.ascx to handle the edit fields and a widget.ascx to handle the display of the actual widget.
Edit: This article + code is horribly out of date. There is a better version of this widget located on biztron.net
~/widgets/XboxGamerCard/edit.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="edit.ascx.cs" Inherits="widgets_XboxGamerCard_edit" %>
<fieldset>
<legend>Add your Gamer Tag ID</legend>
<label for="<%=txtGamerTag.ClientID %>">Gamer Tag ID</label>
<asp:RequiredFieldValidator runat="Server" ControlToValidate="txtGamerTag" ErrorMessage="Please enter a gamer tag" ValidationGroup="add" /><br />
<asp:TextBox runat="server" ID="txtGamerTag" /><br /><br />
<asp:Button runat="server" ID="btnAdd" Text="Add gamer tag" ValidationGroup="add" />
</fieldset>
~/widgets/XboxGamerCard/edit.ascx.cs:
#region Using
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
#endregion
public partial class widgets_XboxGamerCard_edit : WidgetEditBase
{
///
/// Handles the Load event of the Page control.
///
/// The source of the event.
/// The instance containing the event data.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
XmlNode node = Xml.SelectSingleNode("GamerTag");
if (node != null)
{
txtGamerTag.Text = node.InnerText;
}
}
}
///
/// Saves this the basic widget settings such as the Title.
///
public override void Save()
{
if (Xml.ChildNodes.Count == 0)
{
XmlNode node = Xml.CreateElement("GamerTag");
Xml.AppendChild(node);
}
Xml.SelectSingleNode("GamerTag").InnerText = txtGamerTag.Text;
SaveXml();
Cache.Remove(txtGamerTag.Text + "_XboxGamerCard");
}
}
~/widgets/XboxGamerCard/widget.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="widget.ascx.cs" Inherits="widgets_XboxGamerCard_widget" %>
<asp:Table ID="gamerTag" runat="server">
<asp:TableRow>
<asp:TableCell RowSpan="2" VerticalAlign="top"><b><asp:HyperLink ID="gamerName" Target="_new" runat="server" /></b><br /><asp:HyperLink ID="mainTag" Target="_new" runat="server" /></asp:TableCell>
<asp:TableCell><b>Gamer Score: </b><asp:Literal ID="gamerScore" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Repeater ID="gameLinks" runat="server">
<ItemTemplate>
<asp:HyperLink ID="GameImage" Target="_new" NavigateUrl='<%# Eval("DetailsURL") %>' ImageUrl='<%# Eval("Image32Url") %>' Text='<%# Eval("Name") + " (" + Eval("Score") + ")" %>' runat="server" />
</ItemTemplate>
</asp:Repeater>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="2"><asp:Label ID="gamerOnline" runat="server" /><br /><asp:Label ID="gamerSeen" runat="server" /><br /><asp:Label ID="gamerLocation" runat="server" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
~/widgets/XboxGamerCard/widget.ascx.cs:
#region Using
using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.Collections.Generic;
using System.Web.Caching;
using BlogEngine.Core;
#endregion
public partial class widgets_XboxGamerCard_widget : WidgetBase
{
///
/// Handles the Load event of the Page control.
///
/// The source of the event.
/// The instance containing the event data.
protected void Page_Load(object sender, EventArgs e)
{
if (!Request.IsSecureConnection && !Page.IsPostBack)
{
gamerTag.Visible = true;
XmlDownload();
}
else
gamerTag.Visible = false;
base.Name = "XboxGamerCard";
}
private void XmlDownload()
{
try
{
string gamerTagName = "";
XmlNode node = base.Xml.SelectSingleNode("GamerTag");
if (node != null)
gamerTagName = node.InnerText;
DataSet gamerDataSet = (DataSet)Cache[gamerTagName + "_XboxGamerCard"];
if (gamerDataSet == null)
{
string url = String.Format("http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag={0}", gamerTagName);
gamerDataSet = new DataSet();
XmlTextReader xml = new XmlTextReader(url);
gamerDataSet.ReadXml(xml);
}
Cache[gamerTagName + "_XboxGamerCard"] = gamerDataSet;
DataTable xboxInfo = gamerDataSet.Tables["XboxInfo"];
DataTable presenceInfo = gamerDataSet.Tables["PresenceInfo"];
DataTable recentGameInfo = gamerDataSet.Tables["XboxUserGameInfo"];
DataTable recentGame = gamerDataSet.Tables["Game"];
DataTable modRecentGame = new DataTable();
modRecentGame.Columns.Add("DetailsURL");
modRecentGame.Columns.Add("Image32Url");
modRecentGame.Columns.Add("Name");
modRecentGame.Columns.Add("Score");
for (int x = 0; x < recentGameInfo.Rows.Count; x++)
modRecentGame.Rows.Add(new object[] { recentGameInfo.Rows[x]["DetailsURL"], recentGame.Rows[x]["Image32Url"], recentGame.Rows[x]["Name"], recentGameInfo.Rows[x]["GamerScore"] });
gamerName.Text = Convert.ToString(xboxInfo.Rows[0]["Gamertag"]);
gamerName.NavigateUrl = Convert.ToString(xboxInfo.Rows[0]["ProfileUrl"]);
mainTag.ImageUrl = Convert.ToString(xboxInfo.Rows[0]["TileUrl"]);
mainTag.Text = Convert.ToString(xboxInfo.Rows[0]["Gamertag"]);
mainTag.NavigateUrl = Convert.ToString(xboxInfo.Rows[0]["ProfileUrl"]);
gamerScore.Text = Convert.ToString(xboxInfo.Rows[0]["GamerScore"]);
gameLinks.DataSource = modRecentGame.DefaultView;
gameLinks.DataBind();
gamerOnline.Text = Convert.ToString(presenceInfo.Rows[0]["StatusText"]);
gamerSeen.Text = Convert.ToString(presenceInfo.Rows[0]["LastSeen"]);
gamerLocation.Text = Convert.ToString(presenceInfo.Rows[0]["Info"]);
gamerLocation.Text += Convert.ToString(presenceInfo.Rows[0]["Info2"]);
}
catch (Exception ex)
{
gamerTag.Visible = false;
string foobar = ex.ToString();
}
}
}
XboxGamerCard.zip (2.92 kb)