I want to create tabs with Pivot, I have WebView2 in the tab which is the important content. I wanted to create new PivotItem and duplicate the WebView2 already present on my first Tab.
Here is my codes:
MainPage.xaml
<Pivot x:Name="pivot" Margin="40,0,0,40">
<PivotItem Header="Tab 1">
<Grid>
<controls:WebView2 x:Name="webView2" x:FieldModifier="public"
Source="https://example.com"
NavigationStarting="webView2_NavigationStarting" NavigationCompleted="webView2_NavigationCompleted" Margin="-12,0,-12,0"/>
</Grid>
</PivotItem>
</Pivot>
-
<AppBarButton x:Name="addButton" Icon="Add" Label="New Tab" Click="addButton_Click"/>
<AppBarButton x:Name="removeButton" Icon="Remove" Label="Remove Tab" Click="removeButton_Click"/>
MainPage.xaml.cs
private void addButton_Click(object sender, RoutedEventArgs e)
{
pivotItem.Header = "New Tab" + null;
pivotItem.Content = null; // temporary null before I got a solution
pivot.Items.Add(pivotItem);
pivotItem = null;
}
private void removeButton_Click(object sender, RoutedEventArgs e)
{
pivot.Items.Remove(pivot.SelectedItem);
// Close the software if there is no PivotItems
if (pivot.Items.Count == 0)
{
Application.Current.Exit();
}
}