2016-12-06 7 views
0

既存のxaml StackLayoutにテキストを含むラベルを追加するメソッドがあります。NFCタグの読み取り後にxamlコントロールを変更できない

このメソッドは、xaml ListViewによって起動されたイベントとNFCタグの読み込みの2か所から呼び出されます。どちらのシナリオでも、この方法はコードビハインドでヒットします。

このメソッドは、ラベルを作成して画面に追加する別のメソッドを呼び出します。 ListViewイベントから発生したものは正常に動作しますが、NFCタグからのものは何もしません。例外を発生させることなくコードの各行を渡しますが、画面には何も追加しません。この後、StackLayoutの子カウントは1であり、再度行うと1のままです。

NFC方式:

public async void HandleNFC(string convertedtag) 
{ 
    int result = 0; 
    try 
    { 
     var mp = (MainPage)App.Current.MainPage; 
     Label sl1 = mp.CurrentPage.FindByName<Label>("timeLabel"); 
    } 
    catch (Exception e) 
    { } 

    Label sl = timeLabel; 
    string time = sl.Text; 

    PeopleLocationsForUserRoot peoplelocationforuser = await WebDataAccess.GetPeopleLocationForUser(UserInfoRepository.GetUserName(), _locationID); 
    DateTime dt = Convert.ToDateTime(time); 

    long timeticks = (long)((dt.ToUniversalTime().Ticks - DatetimeMinTimeTicks)/10000); 
    getServerTime(); 
    string name = ""; 

    try 
    { 
     foreach (var person in peoplelocationforuser.locationPeople) 
     { 
      if (person.TATokenValue == convertedtag) 
      { 
       var action = await DisplayActionSheet(person.FirstName + " " + person.LastName, "Cancel", null, "IN", "OUT"); 
       string act = action; 
       string formattedact = act; 
       int swipedirection = 0; 

       name = person.FirstName + " " + person.LastName; 

       if (act == "IN") 
       { 
        formattedact = "in"; 
        swipedirection = 1; 
       } 

       if (act == "OUT") 
       { 
        formattedact = "out"; 
        swipedirection = 0; 
       } 

       if (act != "Cancel") 
       { 
        result = SwipeRepository.ClockUserInOut(person.EB_Counter, _locationID, swipedirection, dt, timeticks, 1, UserInfoRepository.GetLatt(), UserInfoRepository.GetLongi()); 

        addToReadout(name, time, formattedact); 
       } 
      } 

     } 
     if (name == "") 
     { 
      await DisplayAlert("Tag Error", "Tag not recognised", "cancel"); 
     } 
    } 
    catch (Exception ex) 
    { 

     ErrorRepository.InsertError(ex.ToString()); 
    } 

    await WebDataAccess.SaveSwipesToCloud(); 

} 

それが呼び出す「addToReadOut」メソッド:

public void addToReadout(string name, string time, string inout) 
{ 
    try 
    { 
     Label label1 = new Label { Text = name + " Successfully clocked " + inout + " @ " + time, TextColor = Color.Black }; 

     try 
     { 
      readOut.Children.Add(label1); 
      StackLayout sl = this.FindByName<StackLayout>("readOut"); 
      sl.Children.Add(label1); 
      sl.Focus(); 
      timeLabel.Text = "test"; 
     } 
     catch (Exception e) 
     { } 
     // StackLayout sl = mp.CurrentPage.FindByName<StackLayout>("readOut"); 

     if (readOut.Children.Count() < 6) 
     { 

      readOut.Children.Add(label1); 
      readOut.Children.Count(); 
     } 
     else 
     { 
      readOut.Children.RemoveAt(0); 
      readOut.Children.Add(label1); 
      readOut.Children.Count(); 
     } 

    } 
    catch (Exception ex) 
    { 

     ErrorRepository.InsertError(ex.ToString()); 
    } 
} 

あなたは私も「timelabel」と呼ばれるオブジェクトを変更しようとしたが、またしていることがわかります画面上で変化しません。

ここで問題を引き起こしているNFCイベントの後に何か別のものが起きているに違いありませんが、何が原因か分かりません。

答えて

0

NFCイベントがバックグラウンドスレッドで発生しています。あなたのUIアップデートはUIスレッド上で発生する必要があります

Device.BeingInvokeOnMainThread(() => { 
    // UI Code goes here 
}); 
+0

変更を加えるコードを囲むようにコードを追加しましたが、恐れはありません。 – connersz

+0

このコードを挿入する必要がありますか? – connersz

関連する問題