I am attempting to populate a grid in my MainPage with web browser items which are added in the code behind. For some reason, I cannot get the grid to remember which elements have been added already. I am unsure of how to store this information so that when the MainPage is navigated from, and then back to, the grid knows which values were previously added. To better explain, what I have is as follows:
MainPage.xaml
<Grid x:Name="BrowserHost" Grid.Row="1">
//Items will be added in code behind
</Grid>
MainPage.xaml.cs
private void ShowTab(int index)
{
this.currentIndex = index;
int count = Settings.BrowserList.Value.Count;
if (count <= 0)
{
MessageBox.Show("count <= 0");
}
if (currentIndex <= (count - 1)) //in correct range
{
// BrowserHost does not remember previously added children elements added previously when navigated back to this page and performing this check
if(!BrowserHost.Children.Contains(Settings.BrowserList.Value[currentIndex].Browser))
//Attempt to add browser if it was already added previously throws InvalidOperationException
BrowserHost.Children.Add(Settings.BrowserList.Value[currentIndex].Browser);
}
if (currentIndex > (count - 1))
{
MessageBox.Show("currentIndex > (count - 1)");
}
for (int i = 0; i < Settings.BrowserList.Value.Count; i++)
{
if (Settings.BrowserList.Value.ElementAt(i) != null)
{
Settings.BrowserList.Value.ElementAt(i).Browser.Visibility = i == this.currentIndex ? Visibility.Visible : Visibility.Collapsed;
}
}
}
Essentially, the ShowTab method is called with a specific index value everytime the MainPage loads. A check is performed to see if Settings.BrowserList.Value[currentIndex].Browser (a collection with web browser instances) exists at the currentIndex in the list (these are added in another page). If the BrowserHost grid on MainPage already contains the child element (a browser) with a particular index, then it should skip over the check and continue to show that browser instance on the screen. This is supposed to basically implement a tabbed browsing scenario for a custom web browser control I have created. My issue is, I can consecutively add new browsers to BrowserHost and show them on the screen, but when I try to call a previously added webbrowser at certain index in my collection, I get an InvalidOperationException. How would I fix this? My debugging shows that in ShowTab when I check to see if a browser has been added, regardless of it has or not, it jumps into attempting to add that browser to BrowserHost child element. If the browser has been created previously, thats when the InvalidOperationException occurs.
I don’t think what you are doing is a good idea. However, the issue seems to be that the control is already parented by a different control and so you cannot add it again. Try doing the following:
You also need to make sure that the Browser is placed in a Grid in your other form (if it’s not directly inside a Grid, just wrap it in one).
This may work, but I strongly suggest you find a different way of doing it.