'Xamarin binding sometimes invisible
Inside my Xamarin TicketPage.xaml the binding just shows after a change of the bindingname to notesBlobbed. So i need to change the binding name to notesBlobbed and change it back to notes for displaying the bindings. Moreover, the notes disappear when leaving the page and are not displayed at startup. The first picture shows what it looks like at startup and after leaving the page. The second picture shows how it looks like with the change. The question is how can i display the notes the whole time?
PS: notesBlobbed is needed to deserialize the list for the SQLite datebase.
How it looks like:
https://s20.directupload.net/images/220512/zjdu9nz2.jpg (cant display images because of my reputation)
How it should looks like:
https://s20.directupload.net/images/220512/p6ssqxjt.jpg
TicketPage.xaml
<ContentPage.Content>
<ScrollView>
<StackLayout Padding="10,5,10,5">
<ListView x:Name="ListViewTicket" HasUnevenRows="True" SelectionMode="None" SeparatorVisibility="None" Margin="0,0,0,0">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame CornerRadius="10" BorderColor="Black" Margin="0,5,0,5">
<StackLayout>
<Label>
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="ID: " FontAttributes="Bold" FontSize="18"/>
<Span Text="{Binding pkid}" FontSize="18"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
<Label>
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="Ticketnummer: " FontAttributes="Bold" FontSize="18"/>
<Span Text="{Binding ticketNumber}" FontSize="18"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
<Label>
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="Status: " FontAttributes="Bold" FontSize="18"/>
<Span Text="{Binding status}" FontSize="18"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
<BoxView HorizontalOptions="FillAndExpand" HeightRequest="3" Color="Gray"/>
<Label Text="Betreff" FontAttributes="Bold" FontSize="18"/>
<Label Text="{Binding subject}" FontSize="18" Margin="0,-5,0,0"/>
<Label TextType="Html" Text="{Binding description}" FontSize="12"/>
<Label Text="Notiz" FontAttributes="Bold" FontSize="18" Margin="0,-10,0,0"/>
<!-- here is the problem -->
<StackLayout BindableLayout.ItemsSource="{Binding notes}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding .}" MaxLines="2" LineBreakMode="TailTruncation"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ScrollView>
</ContentPage.Content>
TicketPage.cs
public partial class TicketPage : ContentPage
{
public TicketPage()
{
InitializeComponent();
OnAppearing();
}
//Update & Delete Zeiten
const int deleteInterval = 259200000; //3 Tage
const int updateInterval = 900000; //15 Mins
protected async override void OnAppearing()
{
//lädt Tickets
var ticketList = await App.Database.GetTicket();
//lässt Tickets anzeigen
ListViewTicket.ItemsSource = ticketList;
//alle Tickets durchgehen bezüglich Updates & Delete
foreach (var ticket in ticketList)
{
//überprüft wie lange Ticket geschlossen ist (löscht bei 3 Tagen)
if (ticket.status == "closed")
{
var currentTime = DateTime.Now;
TimeSpan duration = currentTime - ticket.closed;
var result = duration.TotalMilliseconds;
if (result > deleteInterval)
{
await App.Database.DeleteTicket(ticket);
}
}
else
{
//updatet Ticket nach 15 Minuten
var currentTime = DateTime.Now;
TimeSpan duration = currentTime - ticket.updated;
var result = duration.TotalMilliseconds;
if (result < updateInterval)
{
continue;
}
//GET an API um Status & Notizen abzurufen
IsBusy = true;
var ticketId = ticket.id;
var url = "https://.../api/ticket/info/" + ticketId;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("AuthenticationToken", "Token");
HttpResponseMessage response = await client.GetAsync(url);
//updatet Status und Notizen
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Ticket update = JsonConvert.DeserializeObject<Ticket>(content);
ticket.status = update.status;
ticket.updated = DateTime.Now;
if (ticket.status == "closed")
{
ticket.closed = DateTime.Now;
}
ticket.notes = update.notes;
IsBusy = false;
}
else
{
await DisplayAlert("Alert", "Die Verbindung ist fehlgeschlagen prüfen Sie Ihre Internetverbindung und kontaktieren Sie den Support.", "Ok");
}
}
}
}
}
Solution 1:[1]
Firstly,the OnAppearing()
method should be used directly and don't call it in the constructor
again.
Secondly,like Jason suggested, you should process the ticket first and then assign ItemsSource: ListViewTicket.ItemsSource = ticketList;
.
Below is the pseudo code for your reference:
public partial class TicketPage : ContentPage
{
List<String> myList = new List<String>() { "05.05.2022 09:12: Test Kommentar 1", "05.05.2022 09:12: Test Kommentar 2" };
private ObservableCollection<Ticket> ticketList;
public TicketPage()
{
InitializeComponent();
//process ticket to generate data firstly
ticketList = new ObservableCollection<Ticket>
{
new Ticket(){pkid = "3", ticketNumber = "19748",status="open",subject="Title3",description = "Beschreibung",notes=myList },
new Ticket(){pkid = "4", ticketNumber = "19749",status="close",subject="Title4",description = "Beschreibung",notes=myList }
};
}
protected override void OnAppearing()
{
//lädt Tickets
//lässt Tickets anzeigen
ListViewTicket.ItemsSource = ticketList;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |