質問編集履歴

3

コード部分がうまく囲えていなかった部分を修正

2016/12/15 15:05

投稿

nasama
nasama

スコア16

test CHANGED
File without changes
test CHANGED
@@ -260,284 +260,282 @@
260
260
 
261
261
 
262
262
 
263
+
264
+
265
+ ###追記2(12/15)
266
+
267
+ コメントありがとうございます。
268
+
269
+ e.Event.Actionを使用することでe.Actionと同様の動きができました。
270
+
271
+ それにより、実現したい動作を作成することができました。ありがとうございました!
272
+
273
+
274
+
275
+
276
+
277
+ 修正後の内容は以下のコードです。
278
+
263
279
  ```
264
280
 
265
- ###追記2(12/15)
266
-
267
- コメントありがとうございます。
268
-
269
- e.Event.Actionを使用することでe.Actionと同様の動きができました。
270
-
271
- それにより、実現したい動作を作成することができました。ありがとうございました!
272
-
273
-
274
-
275
-
276
-
277
- 修正後の内容は以下のコードです。
278
-
279
-
281
+ using System;
282
+
283
+ using System.Diagnostics;
284
+
285
+ using Android.App;
286
+
287
+ using Android.OS;
288
+
289
+ using Android.Views;
290
+
291
+ using Android.Widget;
292
+
293
+
294
+
295
+
296
+
297
+ namespace test2
298
+
299
+ {
300
+
301
+ [Activity(Label = "test2", MainLauncher = true, Icon = "@mipmap/icon")]
302
+
303
+ public class MainActivity : Activity
304
+
305
+ {
306
+
307
+ System.Threading.CancellationTokenSource cancellationTokenSource;
308
+
309
+ int count = 1;
310
+
311
+ int count2 = 1;
312
+
313
+ private Button button;
314
+
315
+ private Button button2;
316
+
317
+ protected override void OnCreate(Bundle savedInstanceState)
318
+
319
+ {
320
+
321
+ base.OnCreate(savedInstanceState);
322
+
323
+
324
+
325
+ // Set our view from the "main" layout resource
326
+
327
+ SetContentView(Resource.Layout.Main);
328
+
329
+
330
+
331
+ // Get our button from the layout resource,
332
+
333
+ // and attach an event to it
334
+
335
+ button = FindViewById<Button>(Resource.Id.myButton);
336
+
337
+ button2 = FindViewById<Button>(Resource.Id.myButton2);
338
+
339
+ TextView text = new TextView(this);
340
+
341
+ //text = FindViewById<TextView>(Resource.Id.Text);
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+ button.Touch += (object sender, View.TouchEventArgs e) =>
350
+
351
+ {
352
+
353
+ switch (e.Event.Action)
354
+
355
+ {
356
+
357
+ case MotionEventActions.Down:
358
+
359
+
360
+
361
+ button.Text = string.Format("{0} clicks!",count++);
362
+
363
+ if (cancellationTokenSource != null)
364
+
365
+ break;
366
+
367
+ cancellationTokenSource = new System.Threading.CancellationTokenSource();
368
+
369
+ var token = cancellationTokenSource.Token;
370
+
371
+
372
+
373
+ System.Threading.Tasks.Task.Factory.StartNew(() =>
374
+
375
+ {
376
+
377
+ var stopwatch = Stopwatch.StartNew();
378
+
379
+ while (stopwatch.ElapsedMilliseconds < 1000 &&
380
+
381
+ token.IsCancellationRequested == false)
382
+
383
+ {
384
+
385
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
386
+
387
+ }
388
+
389
+
390
+
391
+ while (token.IsCancellationRequested == false)
392
+
393
+ {
394
+
395
+ RunOnUiThread(() =>
396
+
397
+ {
398
+
399
+ button.Text = string.Format("{0} clicks!", count++);
400
+
401
+ });
402
+
403
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
404
+
405
+ }
406
+
407
+ }, token)
408
+
409
+ .ContinueWith(_ =>
410
+
411
+ {
412
+
413
+ cancellationTokenSource.Dispose();
414
+
415
+ cancellationTokenSource = null;
416
+
417
+ });
418
+
419
+ break;
420
+
421
+
422
+
423
+ case MotionEventActions.Up:
424
+
425
+ if (cancellationTokenSource != null)
426
+
427
+ {
428
+
429
+ cancellationTokenSource.Cancel();
430
+
431
+ }
432
+
433
+ break;
434
+
435
+ }
436
+
437
+ };
438
+
439
+ button2.Touch += (object sender, View.TouchEventArgs e) =>
440
+
441
+ {
442
+
443
+ switch (e.Event.Action)
444
+
445
+ {
446
+
447
+ case MotionEventActions.Down:
448
+
449
+
450
+
451
+ button2.Text = string.Format("{0} clicks!",count2++);
452
+
453
+ if (cancellationTokenSource != null)
454
+
455
+ break;
456
+
457
+ cancellationTokenSource = new System.Threading.CancellationTokenSource();
458
+
459
+ var token = cancellationTokenSource.Token;
460
+
461
+
462
+
463
+ System.Threading.Tasks.Task.Factory.StartNew(() =>
464
+
465
+ {
466
+
467
+ var stopwatch = Stopwatch.StartNew();
468
+
469
+ while (stopwatch.ElapsedMilliseconds < 1000 &&
470
+
471
+ token.IsCancellationRequested == false)
472
+
473
+ {
474
+
475
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
476
+
477
+ }
478
+
479
+
480
+
481
+ while (token.IsCancellationRequested == false)
482
+
483
+ {
484
+
485
+ RunOnUiThread(() =>
486
+
487
+ {
488
+
489
+ button2.Text = string.Format("{0} clicks!", count2++);
490
+
491
+ });
492
+
493
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
494
+
495
+ }
496
+
497
+ }, token)
498
+
499
+ .ContinueWith(_ =>
500
+
501
+ {
502
+
503
+ cancellationTokenSource.Dispose();
504
+
505
+ cancellationTokenSource = null;
506
+
507
+ });
508
+
509
+ break;
510
+
511
+
512
+
513
+ case MotionEventActions.Up:
514
+
515
+ if (cancellationTokenSource != null)
516
+
517
+ {
518
+
519
+ cancellationTokenSource.Cancel();
520
+
521
+ }
522
+
523
+ break;
524
+
525
+ }
526
+
527
+ };
528
+
529
+ }
530
+
531
+ }
532
+
533
+
534
+
535
+ }
280
536
 
281
537
  ```
282
538
 
283
- using System;
284
-
285
- using System.Diagnostics;
286
-
287
- using Android.App;
288
-
289
- using Android.OS;
290
-
291
- using Android.Views;
292
-
293
- using Android.Widget;
294
-
295
-
296
-
297
-
298
-
299
- namespace test2
300
-
301
- {
302
-
303
- [Activity(Label = "test2", MainLauncher = true, Icon = "@mipmap/icon")]
304
-
305
- public class MainActivity : Activity
306
-
307
- {
308
-
309
- System.Threading.CancellationTokenSource cancellationTokenSource;
310
-
311
- int count = 1;
312
-
313
- int count2 = 1;
314
-
315
- private Button button;
316
-
317
- private Button button2;
318
-
319
- protected override void OnCreate(Bundle savedInstanceState)
320
-
321
- {
322
-
323
- base.OnCreate(savedInstanceState);
324
-
325
-
326
-
327
- // Set our view from the "main" layout resource
328
-
329
- SetContentView(Resource.Layout.Main);
330
-
331
-
332
-
333
- // Get our button from the layout resource,
334
-
335
- // and attach an event to it
336
-
337
- button = FindViewById<Button>(Resource.Id.myButton);
338
-
339
- button2 = FindViewById<Button>(Resource.Id.myButton2);
340
-
341
- TextView text = new TextView(this);
342
-
343
- //text = FindViewById<TextView>(Resource.Id.Text);
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
- button.Touch += (object sender, View.TouchEventArgs e) =>
352
-
353
- {
354
-
355
- switch (e.Event.Action)
356
-
357
- {
358
-
359
- case MotionEventActions.Down:
360
-
361
-
362
-
363
- button.Text = string.Format("{0} clicks!",count++);
364
-
365
- if (cancellationTokenSource != null)
366
-
367
- break;
368
-
369
- cancellationTokenSource = new System.Threading.CancellationTokenSource();
370
-
371
- var token = cancellationTokenSource.Token;
372
-
373
-
374
-
375
- System.Threading.Tasks.Task.Factory.StartNew(() =>
376
-
377
- {
378
-
379
- var stopwatch = Stopwatch.StartNew();
380
-
381
- while (stopwatch.ElapsedMilliseconds < 1000 &&
382
-
383
- token.IsCancellationRequested == false)
384
-
385
- {
386
-
387
- System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
388
-
389
- }
390
-
391
-
392
-
393
- while (token.IsCancellationRequested == false)
394
-
395
- {
396
-
397
- RunOnUiThread(() =>
398
-
399
- {
400
-
401
- button.Text = string.Format("{0} clicks!", count++);
402
-
403
- });
404
-
405
- System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
406
-
407
- }
408
-
409
- }, token)
410
-
411
- .ContinueWith(_ =>
412
-
413
- {
414
-
415
- cancellationTokenSource.Dispose();
416
-
417
- cancellationTokenSource = null;
418
-
419
- });
420
-
421
- break;
422
-
423
-
424
-
425
- case MotionEventActions.Up:
426
-
427
- if (cancellationTokenSource != null)
428
-
429
- {
430
-
431
- cancellationTokenSource.Cancel();
432
-
433
- }
434
-
435
- break;
436
-
437
- }
438
-
439
- };
440
-
441
- button2.Touch += (object sender, View.TouchEventArgs e) =>
442
-
443
- {
444
-
445
- switch (e.Event.Action)
446
-
447
- {
448
-
449
- case MotionEventActions.Down:
450
-
451
-
452
-
453
- button2.Text = string.Format("{0} clicks!",count2++);
454
-
455
- if (cancellationTokenSource != null)
456
-
457
- break;
458
-
459
- cancellationTokenSource = new System.Threading.CancellationTokenSource();
460
-
461
- var token = cancellationTokenSource.Token;
462
-
463
-
464
-
465
- System.Threading.Tasks.Task.Factory.StartNew(() =>
466
-
467
- {
468
-
469
- var stopwatch = Stopwatch.StartNew();
470
-
471
- while (stopwatch.ElapsedMilliseconds < 1000 &&
472
-
473
- token.IsCancellationRequested == false)
474
-
475
- {
476
-
477
- System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
478
-
479
- }
480
-
481
-
482
-
483
- while (token.IsCancellationRequested == false)
484
-
485
- {
486
-
487
- RunOnUiThread(() =>
488
-
489
- {
490
-
491
- button2.Text = string.Format("{0} clicks!", count2++);
492
-
493
- });
494
-
495
- System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
496
-
497
- }
498
-
499
- }, token)
500
-
501
- .ContinueWith(_ =>
502
-
503
- {
504
-
505
- cancellationTokenSource.Dispose();
506
-
507
- cancellationTokenSource = null;
508
-
509
- });
510
-
511
- break;
512
-
513
-
514
-
515
- case MotionEventActions.Up:
516
-
517
- if (cancellationTokenSource != null)
518
-
519
- {
520
-
521
- cancellationTokenSource.Cancel();
522
-
523
- }
524
-
525
- break;
526
-
527
- }
528
-
529
- };
530
-
531
- }
532
-
533
- }
534
-
535
-
536
-
537
- }
538
-
539
- ```
540
-
541
539
 
542
540
 
543
541
  ※文字数制限に引っかかったので前回のコードは消しました

2

コメントに追記された内容をもと目的の動作ができたので追記

2016/12/15 15:05

投稿

nasama
nasama

スコア16

test CHANGED
File without changes
test CHANGED
@@ -258,14 +258,28 @@
258
258
 
259
259
  Xamarin Studioのクイック修正を使用しエラーを潰しましたが、取りきれないエラーが数多くあり、動作確認ができない状況です。
260
260
 
261
- 教えていただいた内容を元に修正したコードが以下の通りです。
262
-
263
-
264
-
265
261
 
266
262
 
267
263
  ```
268
264
 
265
+ ###追記2(12/15)
266
+
267
+ コメントありがとうございます。
268
+
269
+ e.Event.Actionを使用することでe.Actionと同様の動きができました。
270
+
271
+ それにより、実現したい動作を作成することができました。ありがとうございました!
272
+
273
+
274
+
275
+
276
+
277
+ 修正後の内容は以下のコードです。
278
+
279
+
280
+
281
+ ```
282
+
269
283
  using System;
270
284
 
271
285
  using System.Diagnostics;
@@ -288,16 +302,16 @@
288
302
 
289
303
  [Activity(Label = "test2", MainLauncher = true, Icon = "@mipmap/icon")]
290
304
 
291
- public class MainActivity : Activity, View.IOnTouchListener
305
+ public class MainActivity : Activity
292
306
 
293
307
  {
294
308
 
295
-
296
-
297
309
  System.Threading.CancellationTokenSource cancellationTokenSource;
298
310
 
299
311
  int count = 1;
300
312
 
313
+ int count2 = 1;
314
+
301
315
  private Button button;
302
316
 
303
317
  private Button button2;
@@ -330,276 +344,200 @@
330
344
 
331
345
 
332
346
 
347
+
348
+
349
+
350
+
333
351
  button.Touch += (object sender, View.TouchEventArgs e) =>
334
352
 
335
353
  {
336
354
 
337
- public bool OnTouch(View v, MotionEvent e)
355
+ switch (e.Event.Action)
338
-
356
+
339
- {
357
+ {
358
+
340
-
359
+ case MotionEventActions.Down:
360
+
361
+
362
+
363
+ button.Text = string.Format("{0} clicks!",count++);
364
+
365
+ if (cancellationTokenSource != null)
366
+
367
+ break;
368
+
369
+ cancellationTokenSource = new System.Threading.CancellationTokenSource();
370
+
371
+ var token = cancellationTokenSource.Token;
372
+
373
+
374
+
375
+ System.Threading.Tasks.Task.Factory.StartNew(() =>
376
+
377
+ {
378
+
379
+ var stopwatch = Stopwatch.StartNew();
380
+
381
+ while (stopwatch.ElapsedMilliseconds < 1000 &&
382
+
383
+ token.IsCancellationRequested == false)
384
+
385
+ {
386
+
387
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
388
+
389
+ }
390
+
391
+
392
+
393
+ while (token.IsCancellationRequested == false)
394
+
395
+ {
396
+
397
+ RunOnUiThread(() =>
398
+
399
+ {
400
+
401
+ button.Text = string.Format("{0} clicks!", count++);
402
+
403
+ });
404
+
405
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
406
+
407
+ }
408
+
409
+ }, token)
410
+
411
+ .ContinueWith(_ =>
412
+
413
+ {
414
+
415
+ cancellationTokenSource.Dispose();
416
+
417
+ cancellationTokenSource = null;
418
+
419
+ });
420
+
421
+ break;
422
+
423
+
424
+
341
- switch (e.Action)
425
+ case MotionEventActions.Up:
426
+
427
+ if (cancellationTokenSource != null)
428
+
429
+ {
430
+
431
+ cancellationTokenSource.Cancel();
432
+
433
+ }
434
+
435
+ break;
436
+
437
+ }
438
+
439
+ };
440
+
441
+ button2.Touch += (object sender, View.TouchEventArgs e) =>
342
442
 
343
443
  {
344
444
 
445
+ switch (e.Event.Action)
446
+
447
+ {
448
+
345
- case MotionEventActions.Down:
449
+ case MotionEventActions.Down:
346
-
347
-
348
-
450
+
451
+
452
+
349
- button.Text = string.Format("{0} clicks!", v.Id);
453
+ button2.Text = string.Format("{0} clicks!",count2++);
350
-
454
+
351
- if (cancellationTokenSource != null)
455
+ if (cancellationTokenSource != null)
456
+
457
+ break;
458
+
459
+ cancellationTokenSource = new System.Threading.CancellationTokenSource();
460
+
461
+ var token = cancellationTokenSource.Token;
462
+
463
+
464
+
465
+ System.Threading.Tasks.Task.Factory.StartNew(() =>
466
+
467
+ {
468
+
469
+ var stopwatch = Stopwatch.StartNew();
470
+
471
+ while (stopwatch.ElapsedMilliseconds < 1000 &&
472
+
473
+ token.IsCancellationRequested == false)
474
+
475
+ {
476
+
477
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
478
+
479
+ }
480
+
481
+
482
+
483
+ while (token.IsCancellationRequested == false)
484
+
485
+ {
486
+
487
+ RunOnUiThread(() =>
488
+
489
+ {
490
+
491
+ button2.Text = string.Format("{0} clicks!", count2++);
492
+
493
+ });
494
+
495
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
496
+
497
+ }
498
+
499
+ }, token)
500
+
501
+ .ContinueWith(_ =>
502
+
503
+ {
504
+
505
+ cancellationTokenSource.Dispose();
506
+
507
+ cancellationTokenSource = null;
508
+
509
+ });
352
510
 
353
511
  break;
354
512
 
513
+
514
+
355
- cancellationTokenSource = new System.Threading.CancellationTokenSource();
515
+ case MotionEventActions.Up:
356
-
516
+
357
- var token = cancellationTokenSource.Token;
517
+ if (cancellationTokenSource != null)
358
-
359
-
360
-
361
- System.Threading.Tasks.Task.Factory.StartNew(() =>
518
+
362
-
363
- {
519
+ {
364
-
365
- var stopwatch = Stopwatch.StartNew();
520
+
366
-
367
- while (stopwatch.ElapsedMilliseconds < 1000 &&
368
-
369
- token.IsCancellationRequested == false)
521
+ cancellationTokenSource.Cancel();
370
-
371
- {
372
-
373
- System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
374
522
 
375
523
  }
376
524
 
377
-
378
-
379
- while (token.IsCancellationRequested == false)
380
-
381
- {
382
-
383
- RunOnUiThread(() =>
384
-
385
- {
386
-
387
- button.Text = string.Format("{0} clicks!", count++);
388
-
389
- });
525
+ break;
390
-
391
- System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
526
+
392
-
393
- }
527
+ }
394
-
395
- }, token)
528
+
396
-
397
- .ContinueWith(_ =>
398
-
399
- {
400
-
401
- cancellationTokenSource.Dispose();
402
-
403
- cancellationTokenSource = null;
404
-
405
- });
529
+ };
406
-
407
- break;
408
-
409
-
410
-
411
- case MotionEventActions.Up:
412
-
413
- if (cancellationTokenSource != null)
414
-
415
- {
416
-
417
- cancellationTokenSource.Cancel();
418
-
419
- }
420
-
421
- break;
422
-
423
- }
424
-
425
- return true;
426
530
 
427
531
  }
428
532
 
429
- };
430
-
431
- button2.Touch += (object sender, View.View.TouchEventArgs e) =>
432
-
433
- {
434
-
435
- public bool OnTouch(View v, MotionEvent e)
436
-
437
- {
438
-
439
- object cancellationTokenSource = null;
440
-
441
- switch (e.Action)
442
-
443
- {
444
-
445
- case MotionEventActions.Down:
446
-
447
-
448
-
449
- button2.Text = string.Format("{0} clicks!", v.Id);
450
-
451
- if (cancellationTokenSource != null)
452
-
453
- break;
454
-
455
- cancellationTokenSource = new System.Threading.CancellationTokenSource();
456
-
457
- var token = cancellationTokenSource.Token;
458
-
459
-
460
-
461
- System.Threading.Tasks.Task.Factory.StartNew(() =>
462
-
463
- {
464
-
465
- var stopwatch = Stopwatch.StartNew();
466
-
467
- while (stopwatch.ElapsedMilliseconds < 1000 &&
468
-
469
- token.IsCancellationRequested == false)
470
-
471
- {
472
-
473
- System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
474
-
475
- }
476
-
477
-
478
-
479
- while (token.IsCancellationRequested == false)
480
-
481
- {
482
-
483
- RunOnUiThread(() =>
484
-
485
- {
486
-
487
- button.Text = string.Format("{0} clicks!", count++);
488
-
489
- });
490
-
491
- System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
492
-
493
- }
494
-
495
- }, token)
496
-
497
- .ContinueWith(_ =>
498
-
499
- {
500
-
501
- cancellationTokenSource.Dispose();
502
-
503
- cancellationTokenSource = null;
504
-
505
- });
506
-
507
- break;
508
-
509
-
510
-
511
- case MotionEventActions.Up:
512
-
513
- if (cancellationTokenSource != null)
514
-
515
- {
516
-
517
- cancellationTokenSource.Cancel();
518
-
519
- }
520
-
521
- break;
522
-
523
- }
524
-
525
- return true;
526
-
527
533
  }
528
534
 
529
535
 
530
536
 
531
- ;
532
-
533
-
534
-
535
- }
536
-
537
-
538
-
539
- }
540
-
541
-
542
-
543
537
  }
544
538
 
545
-
546
-
547
539
  ```
548
540
 
549
541
 
550
542
 
551
-
552
-
553
-
554
-
555
- 解決できなかったエラー内容は以下通りす。以下通りです。
543
+ ※文字数制限に引っったので前回コードは消しました
556
-
557
- ```
558
-
559
- MainActivity.cs(37,4): error CS1525: Unexpected symbol `public'
560
-
561
- MainActivity.cs(37,23): error CS1525: Unexpected symbol `('
562
-
563
- MainActivity.cs(37,44): error CS1525: Unexpected symbol `e'
564
-
565
- MainActivity.cs(43,49): error CS1525: Unexpected symbol `.'
566
-
567
- MainActivity.cs(74,4): error CS1525: Unexpected symbol `case'
568
-
569
- MainActivity.cs(74,30): error CS1525: Unexpected symbol `:'
570
-
571
- MainActivity.cs(81,3): error CS1525: Unexpected symbol `return', expecting `;' or `}'
572
-
573
- MainActivity.cs(84,3): error CS1525: Unexpected symbol `button2'
574
-
575
- MainActivity.cs(84,17): error CS1525: Unexpected symbol `+='
576
-
577
- MainActivity.cs(84,61): error CS1525: Unexpected symbol `e'
578
-
579
- MainActivity.cs(86,11): error CS1525: Unexpected symbol `bool'
580
-
581
- MainActivity.cs(88,2): error CS1525: Unexpected symbol `object'
582
-
583
- MainActivity.cs(89,18): error CS1525: Unexpected symbol `)'
584
-
585
- MainActivity.cs(91,3): error CS1525: Unexpected symbol `case'
586
-
587
- MainActivity.cs(91,31): error CS1525: Unexpected symbol `:'
588
-
589
- MainActivity.cs(93,17): error CS1525: Unexpected symbol `='
590
-
591
- MainActivity.cs(93,32): error CS1525: Unexpected symbol `('
592
-
593
- MainActivity.cs(93,52): error CS1525: Unexpected symbol `)'
594
-
595
- MainActivity.cs(96,33): error CS1514: Unexpected symbol `new', expecting `.' or `{'
596
-
597
- MainActivity.cs(96,31): error CS1530: Keyword `new' is not allowed on namespace elements
598
-
599
- MainActivity.cs(96,34): error CS1525: Unexpected symbol `System'
600
-
601
- Task "Csc" execution -- FAILED
602
-
603
-
604
-
605
- ```

1

回答内容を元に修正したコードを追記

2016/12/15 15:03

投稿

nasama
nasama

スコア16

test CHANGED
File without changes
test CHANGED
@@ -229,3 +229,377 @@
229
229
  こんな感じでいいのでしょうか?
230
230
 
231
231
  サンプルコードとか色々見ましたが変数名とかが記載してあるのがほとんどだったと思うので気になり投稿しました。
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+ ###追記(12/15)
246
+
247
+ 回答ありがとうございます。
248
+
249
+ 教えていただいたコードを使用させていただきました。
250
+
251
+
252
+
253
+ しかし、通常のtouch動作であれば作成できましたが、
254
+
255
+ MotionEvent を検知できないようで、長押しができなくなってしまいました。
256
+
257
+ そのためonTouchから入れて試しましたが、どこに}を足してもエラーが出て詰まってしまいます。
258
+
259
+ Xamarin Studioのクイック修正を使用しエラーを潰しましたが、取りきれないエラーが数多くあり、動作確認ができない状況です。
260
+
261
+ 教えていただいた内容を元に修正したコードが以下の通りです。
262
+
263
+
264
+
265
+
266
+
267
+ ```
268
+
269
+ using System;
270
+
271
+ using System.Diagnostics;
272
+
273
+ using Android.App;
274
+
275
+ using Android.OS;
276
+
277
+ using Android.Views;
278
+
279
+ using Android.Widget;
280
+
281
+
282
+
283
+
284
+
285
+ namespace test2
286
+
287
+ {
288
+
289
+ [Activity(Label = "test2", MainLauncher = true, Icon = "@mipmap/icon")]
290
+
291
+ public class MainActivity : Activity, View.IOnTouchListener
292
+
293
+ {
294
+
295
+
296
+
297
+ System.Threading.CancellationTokenSource cancellationTokenSource;
298
+
299
+ int count = 1;
300
+
301
+ private Button button;
302
+
303
+ private Button button2;
304
+
305
+ protected override void OnCreate(Bundle savedInstanceState)
306
+
307
+ {
308
+
309
+ base.OnCreate(savedInstanceState);
310
+
311
+
312
+
313
+ // Set our view from the "main" layout resource
314
+
315
+ SetContentView(Resource.Layout.Main);
316
+
317
+
318
+
319
+ // Get our button from the layout resource,
320
+
321
+ // and attach an event to it
322
+
323
+ button = FindViewById<Button>(Resource.Id.myButton);
324
+
325
+ button2 = FindViewById<Button>(Resource.Id.myButton2);
326
+
327
+ TextView text = new TextView(this);
328
+
329
+ //text = FindViewById<TextView>(Resource.Id.Text);
330
+
331
+
332
+
333
+ button.Touch += (object sender, View.TouchEventArgs e) =>
334
+
335
+ {
336
+
337
+ public bool OnTouch(View v, MotionEvent e)
338
+
339
+ {
340
+
341
+ switch (e.Action)
342
+
343
+ {
344
+
345
+ case MotionEventActions.Down:
346
+
347
+
348
+
349
+ button.Text = string.Format("{0} clicks!", v.Id);
350
+
351
+ if (cancellationTokenSource != null)
352
+
353
+ break;
354
+
355
+ cancellationTokenSource = new System.Threading.CancellationTokenSource();
356
+
357
+ var token = cancellationTokenSource.Token;
358
+
359
+
360
+
361
+ System.Threading.Tasks.Task.Factory.StartNew(() =>
362
+
363
+ {
364
+
365
+ var stopwatch = Stopwatch.StartNew();
366
+
367
+ while (stopwatch.ElapsedMilliseconds < 1000 &&
368
+
369
+ token.IsCancellationRequested == false)
370
+
371
+ {
372
+
373
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
374
+
375
+ }
376
+
377
+
378
+
379
+ while (token.IsCancellationRequested == false)
380
+
381
+ {
382
+
383
+ RunOnUiThread(() =>
384
+
385
+ {
386
+
387
+ button.Text = string.Format("{0} clicks!", count++);
388
+
389
+ });
390
+
391
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
392
+
393
+ }
394
+
395
+ }, token)
396
+
397
+ .ContinueWith(_ =>
398
+
399
+ {
400
+
401
+ cancellationTokenSource.Dispose();
402
+
403
+ cancellationTokenSource = null;
404
+
405
+ });
406
+
407
+ break;
408
+
409
+
410
+
411
+ case MotionEventActions.Up:
412
+
413
+ if (cancellationTokenSource != null)
414
+
415
+ {
416
+
417
+ cancellationTokenSource.Cancel();
418
+
419
+ }
420
+
421
+ break;
422
+
423
+ }
424
+
425
+ return true;
426
+
427
+ }
428
+
429
+ };
430
+
431
+ button2.Touch += (object sender, View.View.TouchEventArgs e) =>
432
+
433
+ {
434
+
435
+ public bool OnTouch(View v, MotionEvent e)
436
+
437
+ {
438
+
439
+ object cancellationTokenSource = null;
440
+
441
+ switch (e.Action)
442
+
443
+ {
444
+
445
+ case MotionEventActions.Down:
446
+
447
+
448
+
449
+ button2.Text = string.Format("{0} clicks!", v.Id);
450
+
451
+ if (cancellationTokenSource != null)
452
+
453
+ break;
454
+
455
+ cancellationTokenSource = new System.Threading.CancellationTokenSource();
456
+
457
+ var token = cancellationTokenSource.Token;
458
+
459
+
460
+
461
+ System.Threading.Tasks.Task.Factory.StartNew(() =>
462
+
463
+ {
464
+
465
+ var stopwatch = Stopwatch.StartNew();
466
+
467
+ while (stopwatch.ElapsedMilliseconds < 1000 &&
468
+
469
+ token.IsCancellationRequested == false)
470
+
471
+ {
472
+
473
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait();
474
+
475
+ }
476
+
477
+
478
+
479
+ while (token.IsCancellationRequested == false)
480
+
481
+ {
482
+
483
+ RunOnUiThread(() =>
484
+
485
+ {
486
+
487
+ button.Text = string.Format("{0} clicks!", count++);
488
+
489
+ });
490
+
491
+ System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
492
+
493
+ }
494
+
495
+ }, token)
496
+
497
+ .ContinueWith(_ =>
498
+
499
+ {
500
+
501
+ cancellationTokenSource.Dispose();
502
+
503
+ cancellationTokenSource = null;
504
+
505
+ });
506
+
507
+ break;
508
+
509
+
510
+
511
+ case MotionEventActions.Up:
512
+
513
+ if (cancellationTokenSource != null)
514
+
515
+ {
516
+
517
+ cancellationTokenSource.Cancel();
518
+
519
+ }
520
+
521
+ break;
522
+
523
+ }
524
+
525
+ return true;
526
+
527
+ }
528
+
529
+
530
+
531
+ ;
532
+
533
+
534
+
535
+ }
536
+
537
+
538
+
539
+ }
540
+
541
+
542
+
543
+ }
544
+
545
+
546
+
547
+ ```
548
+
549
+
550
+
551
+
552
+
553
+
554
+
555
+ 解決できなかったエラー内容は以下の通りです。以下の通りです。
556
+
557
+ ```
558
+
559
+ MainActivity.cs(37,4): error CS1525: Unexpected symbol `public'
560
+
561
+ MainActivity.cs(37,23): error CS1525: Unexpected symbol `('
562
+
563
+ MainActivity.cs(37,44): error CS1525: Unexpected symbol `e'
564
+
565
+ MainActivity.cs(43,49): error CS1525: Unexpected symbol `.'
566
+
567
+ MainActivity.cs(74,4): error CS1525: Unexpected symbol `case'
568
+
569
+ MainActivity.cs(74,30): error CS1525: Unexpected symbol `:'
570
+
571
+ MainActivity.cs(81,3): error CS1525: Unexpected symbol `return', expecting `;' or `}'
572
+
573
+ MainActivity.cs(84,3): error CS1525: Unexpected symbol `button2'
574
+
575
+ MainActivity.cs(84,17): error CS1525: Unexpected symbol `+='
576
+
577
+ MainActivity.cs(84,61): error CS1525: Unexpected symbol `e'
578
+
579
+ MainActivity.cs(86,11): error CS1525: Unexpected symbol `bool'
580
+
581
+ MainActivity.cs(88,2): error CS1525: Unexpected symbol `object'
582
+
583
+ MainActivity.cs(89,18): error CS1525: Unexpected symbol `)'
584
+
585
+ MainActivity.cs(91,3): error CS1525: Unexpected symbol `case'
586
+
587
+ MainActivity.cs(91,31): error CS1525: Unexpected symbol `:'
588
+
589
+ MainActivity.cs(93,17): error CS1525: Unexpected symbol `='
590
+
591
+ MainActivity.cs(93,32): error CS1525: Unexpected symbol `('
592
+
593
+ MainActivity.cs(93,52): error CS1525: Unexpected symbol `)'
594
+
595
+ MainActivity.cs(96,33): error CS1514: Unexpected symbol `new', expecting `.' or `{'
596
+
597
+ MainActivity.cs(96,31): error CS1530: Keyword `new' is not allowed on namespace elements
598
+
599
+ MainActivity.cs(96,34): error CS1525: Unexpected symbol `System'
600
+
601
+ Task "Csc" execution -- FAILED
602
+
603
+
604
+
605
+ ```