Reputation: 324
I have the error System.StackOverflowException, everytime I run my app. This error is located in the last line of my History class. Below:
public class userHistory
{
public string strTimeDate { get; set; }
public string strUrl { get; set; }
public List<userHistory> lstUserHistory { get; set; }
public userHistory(string timedate, string url)
{
lstUserHistory.Add(new userHistory(timedate, url));
}
}
My MainPage code:
public List<userHistory> lstUserHistory;
public userHistory selectedHistory;
public MainPage()
{
InitializeComponent();
listBox.DataContext = lstUserHistory;
}
private void getHistory(string url)
{
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
userHistory usrHistory = new userHistory(time, url);
lstUserHistory.Add(usrHistory);
listBox.DataContext = null;
listBox.DataContext = lstUserHistory;
}
private void listBox_Tap(object sender, GestureEventArgs e)
{
selectedHistory = listBox.SelectedValue as userHistory;
MessageBox.Show(selectedHistory.strUrl);
browserSearch(selectedHistory.strUrl);
}
The XAML
<Grid>
<ListBox ItemsSource="{Binding}" Foreground="RoyalBlue" Name="listBox" TabIndex="10" Tap="listBox_Tap">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="0,329,0,367" >
<TextBlock Text="{Binding strDateTime}" FontSize="15" Margin="51,1,0,1"/>
<TextBlock Text="{Binding strUrl}" FontSize="28" Margin="51,1,0,1"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I think the error might be because of what happens in getHistory. This method can be called muitply times with the same data. All I would like is for the data to be stored in a List, which can easily add new history records, or remove them. Then be displayed simply in a List box.
Thank you in advance :)
If you need any more details please comment and I will be happy to explain in further detail :)
Upvotes: 0
Views: 136
Reputation: 1503090
Look at this part of userHistory
:
public List<userHistory> lstUserHistory { get; set; }
public userHistory(string timedate, string url)
{
lstUserHistory.Add(new userHistory(timedate, url));
}
There are two bugs here:
lstUserHistory.Add
when lstUserHistory
is definitely nullIt's not clear why you've got a List<userHistory>
in both your MainPage code and in userHistory
. I think you need to think more carefully about the data you're modelling, and what really belongs where.
I suspect your userHistory
class should actually look more like this:
public sealed class HistoryEntry
{
private readonly DateTime timestamp;
private readonly string url;
public DateTime Timestamp { get { return timestamp; } }
public string Url { get { return url; } }
public HistoryEntry(DateTime timestamp, string url)
{
this.timestamp = timestamp;
this.url = url;
}
}
Notes:
Upvotes: 2