Вопрос

First of all, I have to mention that i'm a newbie at c#.
I'm developing an windows 8 metro app with xaml and c#.
I created an array of checkboxes dynamically and assigned a pointerpressed event to each of them.
the weird problem is that the pointerpressed event is not firing up the registerd function.
If i create the same checkboxes using design tool(I'm using visual Studio 2012), strangely the function does work this time.
As i don't know the number of checkboxes to be created till runtime, using design tool for handling checkboxes isn't an option.

The code i'm running is as below:


CheckBox[] chk=new CheckBox[count];
int x=20; //Width of each checkbox(to be incremented for every iteration)
for( int i = 0; i < count ; i++ )
{
    chk[i] = new CheckBox();
    chk[i].HorizontalAlignment = HorizontalAlignment.Left;
    chk[i].Margin = new Thickness(1100, x, 0, 0);
    chk[i].VerticalAlignment = VerticalAlignment.Top;
    chk[i].Height = 30;
    chk[i].Width = 35;
    chk[i].PointerPressed += new PointerEventHandler(chkhandler);
    gridit.Children.Add(chk[i]);
    x = x + 35;
}
private static void chkhandler(object obj, PointerRoutedEventArgs arg)
{
    textblock1.Text="Testing";  //Sample Textblock

}




The checkboxes get checked during runtime but the associated function doesn't work.
Thanks in advance

Это было полезно?

Решение

Try;

  • making the event handler public & non-static,
  • make sure nobody is eating up the events.

what about this:

chk[i].AddHandler(PointerPressedEvent, 
    new PointerEventHandler(SomeButton_PointerPressed), true);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top