I made around 100 Hubtiles for my application, and I want them to read an XML file when they are tapped, and the user to be able to 'pin to start' the desired Hubtile on his screen outside the app.
Every Hubtile reads a different XML file and must be able to be pinned to start.
I know the code to pin it to start, and I know the code to make it read the XML, but there are 100 tiles and that would be many lines of coding and copy/paste.
This is my code for a tile to be able to pop from the application and be pinned to start on xaml:
<toolkit:HubTile Name="Monday" Title="Monday" Source="Days\Weekdays\Monday.png" Margin="15" Tap="day_Tap">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Name="monday" Header="pin to start" Tap="monday_Tap"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</toolkit:HubTile>
And the code behind the CS file to make and check if it already exists:
private void monday_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
CreateLiveTile(Monday, "path-to-xml", "monday_Square_0.png");
}
private void CreateLiveTile(HubTile hubtile, string link, string id)
{
StandardTileData LiveTile = new StandardTileData
{
Title = hubtile.Title,
BackBackgroundImage = (hubtile.Source as BitmapImage).UriSource
};
ShellTile Tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("id=" + id));
{
if (Tile == null)
{
ShellTile.Create(new Uri("/Show.xaml?id=" + id + "&link=" + link, UriKind.Relative), LiveTile);
}
else
{
MessageBox.Show("The tile is already pinned");
}
}
}
Is there a dynamic way for it to happen with one tap event handler or do I need 100 tap event handlers for all 100 tiles and 100 paths for each XML file and 100 event handlers for the pin to start hubtiles and to write the same things on every hubtile on xaml?
Furthermore, I tried, and think it is working, to make one tap event handler for all the hubtiles but I see the whole path to the name of the hubtile, not only the tile name:
private void Week_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
String a = (((Microsoft.Phone.Controls.HubTile)(sender)).Source as BitmapImage).UriSource.OriginalString;
String b = a.Replace(((Microsoft.Phone.Controls.HubTile)(sender)).Title + "_Square_0.png", "week.xml");
String weekPath = a.Replace(((Microsoft.Phone.Controls.HubTile)(sender)).Title + "_Square_0.png", "");
String weekName = ((Microsoft.Phone.Controls.HubTile)(sender)).Title;
NavigationService.Navigate(new Uri("/Show.xaml?parameter=" + b, UriKind.Relative));
}
Every control has a Tag
property. This property is of type object
and has been put there for you. Basically, you can put whatever information you need inside. It's an invaluable help for implementing generic actions, like what you're trying to do.
So you can do something like:
<toolkit:HubTile Name="Monday" Title="Monday" Source="Days\Weekdays\Monday.png" Margin="15" Tap="Tile_Tap" Tag="path-to-xml">
Then in the event handler:
private void Tile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var pathToXml = (string)((FrameworkElement)sender).Tag;
// Whatever
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With