2017-10-02 10 views
0

今日、私のアプリケーションをプログラミングしているうちに、私は突然奇妙な動作に出くわしました。私は私のアプリケーションのバックスペースボタンを作成していたが、コンパイラはちょうどコードをスキップしていた。 ここに私のmain.axmlのレイアウトがあります。Xamarinでコードが不思議にスキップされるVisual Studio 2017

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:minWidth="25px" 
android:minHeight="25px"> 
<EditText 
    android:text="123456123456123456" 
    android:id="@+id/input" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:gravity="center|left" 
    android:background="#ffffff" 
    android:textSize="20sp" 
    android:textColor="#000000" /> 
<TextView 
    android:text="Text" 
    android:id="@+id/result" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:gravity="center|left" 
    android:background="#555555" 
    android:textSize="20sp" 
    android:textColor="#ffffff" /> 
<Button 
    android:text="Backspace" 
    android:id="@+id/bksp" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="#555555" 
    android:textSize="20sp" 
    android:textColor="#ffffff" /> 
</LinearLayout> 

そして、ここでは、私が展開され、Androidの6デバイスでこのコードを実行すると、私のMainActivity.csコンテンツ

using Android.App; 
using Android.Widget; 
using Android.OS; 
using Android.Views; 
using System.Timers; 

namespace App2 
{ 
[Activity(Label = "App2", MainLauncher = true, WindowSoftInputMode = SoftInput.StateAlwaysHidden)] 
public class MainActivity : Activity 
{ 
    private bool Bksp_isdown = false; 
    private int intt = 0; 
    private Timer Bksp_tmr; 
    private Button Bksp; 
    private EditText Inputt; 
    private TextView Resultt; 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.Main); 
     this.Bksp_tmr = new Timer(500); 
     this.Bksp = this.FindViewById<Button>(Resource.Id.bksp); 
     this.Inputt = this.FindViewById<EditText>(Resource.Id.input); 
     this.Resultt = this.FindViewById<TextView>(Resource.Id.result); 
     this.Inputt.ShowSoftInputOnFocus = false; 
     this.Bksp.Touch += this.B_backspace_Touch; 
     this.Bksp_tmr.Elapsed += this.Bksp_tmr_Elapsed; 
    } 

    private void Bksp_tmr_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     this.Resultt.Text = "worked" 
         + this.Bksp_isdown.ToString() + this.Inputt.SelectionStart.ToString(); 
     while (true) 
     { 
      this.intt = this.intt + 100; 
      this.Backspace(); 
     } 
    } 

    private void B_backspace_Touch(object sender, View.TouchEventArgs e) 
    { 
     this.Bksp_isdown = true; 
     if (e.Event.Action == MotionEventActions.Down) 
     { 
      this.Backspace(); 
      this.Bksp_tmr.Start(); 
     } 
     else if (e.Event.Action == MotionEventActions.Up) 
     { 
      this.Bksp_tmr.Stop(); 
      this.Bksp_isdown = false; 
     } 
    } 
    private bool Backspace() 
    { 
     intt++; 
     this.Bksp.Text = intt.ToString(); 
     if (this.Inputt.SelectionStart == 0) 
      return false; 

     string str = this.Inputt.Text; 
     int sel = this.Inputt.SelectionStart; 
     str = str.Substring(0, sel - 1) + str.Substring(sel); 
     this.Inputt.Text = str; 
     this.Inputt.SetSelection(sel - 1); 
     return true; 
    } 
} 
} 

ですが、私は奇妙な行動を見ました。 上記のBksp_tmr_Elapsedという名前のメソッドがありました。このメソッドでは、while(true)とその中のコードが完全に無視されたことを部分的にプログラムで実行しました。私は一生のうちにそのような行動を見たことがありません。 whileループで定数ブール式を与えた理由を聞かれるかもしれません。これは、アプリケーションがクラッシュするまで無限ループが予想されるため、コードが正常に実行されていないためですが、残念ながらそうではありませんでした。助けてください!!

更新:私はコードをチェックし、アプリケーションでBksp_tmr_Elapsedメソッド内の最初の行だけが実行されていることに注意してください。スクリーンショットを提供しているコードをいくつか変更しました。

Check here the first two lines in the method Bksp_tmr_Elapsed.

Check here the updated code.

今、私たちは明らかに、デバイス内のアプリが1に更新され、単に方法の他のすべての行を無視して結論を​​作ることができます。コンパイラが私が知らないメソッドの2行目に追加する、または私が知らない何かを隠すreturn文のようです。これを説明してください!!!

答えて

0

==編集==

iは下図のように、コードのあなたの平和にトライキャッチを追加し、

を次のように私が得たエラーだった

private void Bksp_tmr_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     try 
     { 

      this.Resultt.Text = "worked" 
          + this.Bksp_isdown.ToString() + this.Inputt.SelectionStart.ToString(); 
      while (true) 
      { 
       this.intt = this.intt + 100; 
       this.Backspace(); 
      } 
     } 
     catch(Exception ex) 
     { 
      Toast.MakeText(this, ex.Message, ToastLength.Long); 
     } 
    } 

問題を発見しました{Android.Util.AndroidRuntimeException:ビュー階層を作成した元のスレッドのみがそのビューに触れることができます。これは、次の操作を行い固定するために見てHere

を持っているより、このエラーについて読むためにSystem.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

で (プロジェクトにスレッドを追加)

RunOnUiThread(() => 
      { 

       this.Resultt.Text = "worked" 
           + this.Bksp_isdown.ToString() + this.Inputt.SelectionStart.ToString(); 
       while (true) 
       { 
        this.intt = this.intt + 100; 
        this.Backspace(); 
       } 
      }); 

あなたはRunOnUiThreadに答えをHere

+0

感謝を読み取ることができますが、いや、問題は偽の展開ではなかったです。私は質問を更新しました。 –

+0

病気を再現できるかどうかを見てください –

+0

私はこの問題が私がこれを早く考えなかったとは信じられませんでした あなたのコードにtry catchを追加してこのエラーが見つかりました {Android。 Util.AndroidRuntimeException:ビュー階層を作成した元のスレッドのみがそのビューに触れることができます。 –

関連する問題