3

I am not sure what is wrong with my code can someone help fix the error? The error is in the timer.Tick() line. It's supposed to make a stopwatch.

namespace App3
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {                
            this.InitializeComponent();
        }
        private int myCount;

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += new EventHandler<object>(timer_Tick);
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Start();    
        }


        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            myCount++;
            Label.Text = myCount.ToString();
        }
    }

You need to add quotes around your div ids.

loadDiv( "div1" );
loadDiv( "div2" );

http://jsfiddle.net/pp6nv/1/

Since the tables are added after the page loads, you'll have to use .on().

$('body').on("click", "td.editable", function() {
    var cellId = $(this).prop('id');
    alert(cellId);
});

http://jsfiddle.net/pp6nv/3/

4

4 に答える 4

5

DispatcherTimer.TickEventHandlerではなくEventHandler<object>です。

これを正しく指定するには、コードを変更する必要があります。

 timer.Tick += new EventHandler(timer_Tick);

これは短い形式でも記述できることに注意してください。これは通常、より安全です。

timer.Tick += timer_Tick;
于 2013-02-25T20:44:15.110 に答える
4

Delegate types are not required when attaching events, the compiler can figure it out for you. Try this instead:

timer.Tick += timer_Tick;

As for why you get the error currently, the EventHandler delegate is declared like this:

public delegate void EventHandler<TEventArgs>(
    Object sender,
    TEventArgs e
)

This means the type you put in the brackets represents the second argument in the function.

So when you type EventHandler<object>, the compiler looks for a function like timer_Tick(Object sender, object e) which it does not find, hence the error. To use the full delegate type, it would have to be EventHandler<EventArgs>

于 2013-02-25T20:44:39.163 に答える
1

Instead of new EventHandler<object>, do new EventHandler. Or alternatively just put += timer_Tick;.

于 2013-02-25T20:45:03.673 に答える
1

I solved the same issue with two small changes:

1) Change second argument to type object:

INSTEAD OF THIS:

private void timer_Tick(object sender, EventArgs e)

USE THIS:

private void timer_Tick(object sender, **object** e)

2) Simplify the

INSTEAD OF THIS:

timer.Tick += new EventHandler<object>(timer_Tick);

USE THIS:

timer.Tick += timer_Tick;  // as suggested by WildCrustacean above
于 2013-12-30T04:31:48.883 に答える