Visual studio is great for debugging but when you’re creating and testing live tiles you need to close your app then reopen it, which disconnects Visual Studio from the emulator. To get around this simply create a dummy scheduled agent. This will trick Visual Studio into keeping the connection to the emulator open.

Just add this ExtendedTask to your WMAppManifest.xml

<Tasks>
  <DefaultTask  Name ="_default" NavigationPage="MainPage.xaml"/>
  <ExtendedTask Name="BackgroundTask">
    <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="ScheduledTaskAgent1" Source="DummyAgent" Type="DummyAgent.ScheduledAgent" />
  </ExtendedTask>
</Tasks>

…don’t forget to remove it later.

In this post I outlined a simple way of data binding to a templated control in WP7. That works ok, until you have more than one text field that you would like to update. In that case you can bind to a simple class.

XAML
You might have a style with a control template like this:

<Style x:Key="AlarmToggleButtonStyle" TargetType="ToggleButton">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ToggleButton">
        <Grid Height="184" Width="432" >
          <TextBlock Text="{Binding Path=Title1}" />
          <TextBlock Text="{Binding Path=Title2}" />
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

And be applying it to a ToggleButton like this:

<ToggleButton x:Name="myToggle" Style="{StaticResource AlarmToggleButtonStyle}" />

C#
To bind to the control, create a class then set the DataContext of the ToggleButton with the properties you want:

public class toggleProperties
{
   public string Title1 { get; set; }
   public string Title2 { get; set; }
}

myToggle.DataContext = new toggleProperties() { Title1 = "This is title one", Title2 = "This is title two" };

Yet another simple concept that took me far too long to grasp..

When you’re a hack like I me, you sometimes need a simple way to rip through a Control Template within a ListBox to find a named element. To find an element by name from within a Windows Phone application try something like this…

// Where SongSelection is your item class
SongSelection data = (sender as Button).DataContext as SongSelection;

// SongSelectionListBox is the ListBox that is bound to it
ListBoxItem pressedItem = this.SongSelectionListBox.ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;

// What is returned to myImage is something you can then reference.
// If you're looking for something other than an image
// change FindChild to something else, like FindChild
var myImage = FindChild(pressedItem, "iconPlay");

// Now we can do things like toggle the visibility of this element
myImage Visibility = Visibility.Visible;

Here is the helper method that is used

public static T FindChild(DependencyObject parent, string childName) where T : DependencyObject
{
    // Confirm parent and childName are valid.
    if (parent == null)
    {
        return null;
    }
    T foundChild = null;
    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        // If the child is not of the request child type child
        var childType = child as T;
        if (childType == null)
        {
            // recursively drill down the tree
            foundChild = FindChild(child, childName);
            // If the child is found, break so we do not overwrite the found child.
            if (foundChild != null)
            {
                break;
            }
        }
        else if (!string.IsNullOrEmpty(childName))
        {
            var frameworkElement = child as FrameworkElement;
            // If the child's name is set for search
            if (frameworkElement != null && frameworkElement.Name == childName)
            {
                // if the child's name is of the request name
                foundChild = (T) child;
                break;
            }
            // Need this in case the element we want is nested in another element of the same type
            foundChild = FindChild(child, childName);
        }
        else
        {
            // child element found.
            foundChild = (T) child;
            break;
        }
    }
    return foundChild;
}

Credit goes to this guy over at StackOverflow

This is real

Some things in windows phone application development are harder for me to get than others. One thing I needed was to detect when my application lost focus. The answer is easy to find, if you know what to search for. On windows phone Blur and Focus are called Obscured and Unobscured. To check for these events place these in a MainPage_Loaded method.

(Application.Current as App).RootFrame.Obscured  += new EventHandler(RootFrame_Obscured);
(Application.Current as App).RootFrame.Unobscured  += new EventHandler(RootFrame_Unobscured);

…and let Visual studio create the event handlers

void RootFrame_Obscured(object sender, EventArgs e){}
void RootFrame_Unobscured(object sender, EventArgs e){}

This seems like the simplest thing, but coming from Flash development, it took me forever to figure out how to bind variable in code behind to a TextBlock that is nested within a templated control in XAML.

Normally you can simply reference the name of the control and push in a value like this:

XAML

<TextBlock x:Name="myTextBlock" Text="">

C#

myTextBlock.Text="FOO";

But when a control is nested within a template it’s not visible that way in the code behind. In order to update the text you need to data bind. A quick down and dirty way to do that is like this:

XAML

<TextBlock x:Name="myTextBlock" Text="{Binding}">

C#

string myString = "FOO";
myTextBlock.DataContext = myString;

Seems simple now, but man did I pull my hair out trying to figure out why the code behind couldn’t see the text field. Hope this helps someone.


Ralph Baer is often called the father of video games. His invention, the Magnavox Odyssey, was the first home console system.


Test for controlling hardware with Processing via Arduino serial connection.

Arduino sketch

const int motorPin = 9;

void setup(){
  Serial.begin(9600);
  pinMode(motorPin, OUTPUT);
}

void loop() {
  byte myPulse;
  if (Serial.available()) {
    myPulse = Serial.read();
    analogWrite(motorPin, myPulse);
  }
}

Processing sketch

import processing.serial.*;
import controlP5.*;
Serial port;
ControlP5 controlP5;
PFont Gibson;

int VOLTAGE = 140;

void setup() {
  size(500, 130);
  smooth();
  Gibson = loadFont("Gibson-Bold-48.vlw");

  port = new Serial(this, "COM4", 9600);

  controlP5 = new ControlP5(this);
  Slider s01 = controlP5.addSlider("VOLTAGE", 0, 255, 140, 47, 90, 318, 15);
  s01.setLabelVisible(false);
}

void draw() {
  background(0);
  port.write(int(VOLTAGE));

  float floatVoltage = map(VOLTAGE, 0, 255, 0, 10);
  int intVoltage = floor(floatVoltage);

  fill(0, 105, 140);
  ellipse(426, 63, 90, 90);

  textAlign(LEFT);
  textFont(Gibson, 73);
  text("VOLTAGE", 30, 10, 500, 120);

  fill(0);
  textAlign(CENTER);
  textFont(Gibson, 50);
  text(str(intVoltage), 375, 33, 100, 100);
}

Arduino diagram


NyID marker tracking test video. Created with the NyART Toolkit for Processing.