I made a custom Class named MedinetParse that parses a webrequest. The parsed data should be showen in a listbox named mittSchemaListBox in the my MainPage. The problem that i’m facing now is that IF i write the parsing method in a custom class named MedinetParse the listbox showes nothing. Althought when i put a breakpoint at the very last line of code inside the parse method, i can see that mittSchemaListBox.ItemsSource have all the parsed items. Meanwhile if i move the parsing method into my MainPage.xaml.cs, then i will see all the parsed item in my listbox.
Here is my MedinetParsing class
namespace WindowsPhonePanoramaApplication1
{
public class MedinetParsing : MainPage
{
//Defining class properties
public string Placering { get; set; }
public string Datum { get; set; }
//Defining class methods
public void parseResults(string myresponse)
{
if (string.IsNullOrEmpty(myresponse))
{
return;
}
//Initiating a listbox and add item to it
List<ItemViewModel> mittSchemaList = new List<ItemViewModel>();
//Using HtmlAgilityPack to parse the HTMLcode from the response
HtmlDocument htdoc = new HtmlDocument();
htdoc.LoadHtml(myresponse);
foreach (HtmlNode table in htdoc.DocumentNode.SelectNodes("//table[@class='list-medium']/tbody[1]/tr[@class]"))
{
//Removing ChildNode
table.ChildNodes.RemoveAt(3);
string itemValue = table.InnerText;
//Changing the parsed date into a DateTime
string d;
DateTime datum = DateTime.Parse(itemValue.Remove(11));
d = datum.ToString("D");
//Adding items to the listbox
mittSchemaList.Add(new ItemViewModel() { Datum = d, Placering = itemValue.Remove(0, 15) });
}
mittSchemaListBox.ItemsSource = mittSchemaList;
}
}
}
Here is the code that initiate the parse:-
public void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
MedinetWebRequest mittschema = new MedinetWebRequest();
MedinetParsing mittparse = new MedinetParsing();
mittschema.url = "https://medinet.se/cgi-bin/doctor.pl?action=login&customer=******&language=se";
Action callback = () => Dispatcher.BeginInvoke(() => mittparse.parseResults(mittschema.myresponse));
mittschema.getrequest(callback);
}
And lastly this is my Listbox:-
<ListBox Margin="0,0,-12,0" Name="mittSchemaListBox" DataContext="{Binding}" ItemsSource="{Binding Path=Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Canvas Height="100" Width="100" Margin="12,0,9,0" Background="#FFE5001B">
<TextBlock Text="{Binding Datum}" TextWrapping="Wrap" Height="100" Margin="0" HorizontalAlignment="Right" Width="100" />
</Canvas>
<StackPanel Width="311">
<TextBlock Text="{Binding Placering}" TextWrapping="Wrap" Margin="0,10" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="36" TextAlignment="Center" FontWeight="Normal" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Been trying to fix this issue for the last few hours and not getting anywhere, i decided to ask here. Hope someone can tell me what is the problem.
Could not answer my question last night so here is the answer.
Again after few hours getting back and forth with my code i just found out a solution that fits my app. The solution is that i changed this line of code in my MedinetParsing class
with this one:-
Got rid of this line of code:-
Now it is working like it should and i’m happy ;).
Hope this will help someothers if they face such a problem.
Yours