简体   繁体   中英

How to manage webviews that are in tabitems c#?

It's a web browser. It adds new webview for every new tabitem but i can not access from go back button. When i click back button, it should go back in selected webview which is in selected tabitem.

How can i manage webviews to go back, to go forward or refresh etc. '''

My app's xml code:

 <Grid>

        <muxc:TabView x:Name="tabs" AddTabButtonClick="TabView_AddTabButtonClick" TabCloseRequested="TabView_TabCloseRequested" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="{x:Null}" >
            <muxc:TabView.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF173493"/>
                    <GradientStop Color="#FF240C3F" Offset="1"/>
                    <GradientStop Color="#FF1B2573" Offset="0.543"/>
                </LinearGradientBrush>
            </muxc:TabView.Background>
            <muxc:TabView.TabStripHeader>
                <Grid x:Name="ShellTitlebarInset" Background="Transparent" Margin="0,3,-3,-1" >
                    <Image x:Name="backBtn" HorizontalAlignment="Left" Height="20" Margin="10,5,0,0" VerticalAlignment="Top" Width="20" Source="/Assets/backBtn.png" Stretch="Fill" Tapped="backBtn_Tapped"/>
                    <Image x:Name="forwardBtn" HorizontalAlignment="Left" Height="20" Margin="30,5,0,0" VerticalAlignment="Top" Width="20" Source="/Assets/forwardBtn.png" Stretch="Fill"/>
                    <Image x:Name="refreshBtn" HorizontalAlignment="Left" Height="25" Margin="55,3,0,0" VerticalAlignment="Top" Width="25" Source="/Assets/—Pngtree—abstract ring purple light effect_5102994.png" Stretch="Fill" Tapped="refreshBtn_Tapped"/>
                    <Image x:Name="homeBtn" HorizontalAlignment="Left" Height="20" Margin="85,5,0,0" VerticalAlignment="Top" Width="20" Source="/Assets/homeBtn.png" Stretch="Fill"/>

                    <TextBox x:Name="searchBox" HorizontalAlignment="Left" Height="10" Margin="120,0,0,0" VerticalAlignment="Top" Width="312"/>

                </Grid>
            </muxc:TabView.TabStripHeader>
            <muxc:TabView.TabStripFooter>
                <Grid x:Name="CustomDragRegion" Background="Transparent" Margin="0,0,0,-41" />
            </muxc:TabView.TabStripFooter>
        </muxc:TabView>






    </Grid>
</Page>

My app's code: '''

    // Add a new Tab to the TabView
    private void TabView_AddTabButtonClick(muxc.TabView sender, object args)
    {
        var newTab = new muxc.TabViewItem();
        newTab.IconSource = new muxc.SymbolIconSource() { Symbol = Symbol.World };
        newTab.Header = "Blank Page";

        // The Content of a TabViewItem is often a frame which hosts a page.
        Frame frame = new Frame();
        newTab.Content = frame;



        WebView wb = new WebView();
        newTab.Content = wb;
        wb.Navigate(new Uri("http://google.com"));
        sender.TabItems.Add(newTab);


        backBtn_Tapped(sender, (TappedRoutedEventArgs)args);

    }

    // Remove the requested tab from the TabView
    private void TabView_TabCloseRequested(muxc.TabView sender, muxc.TabViewTabCloseRequestedEventArgs args)
    {
      
        sender.TabItems.Remove(args.Tab);
    }



    private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
    {
        if (FlowDirection == FlowDirection.LeftToRight)
        {
            CustomDragRegion.MinWidth = sender.SystemOverlayRightInset;
            ShellTitlebarInset.MinWidth = sender.SystemOverlayLeftInset;
        }
        else
        {
            CustomDragRegion.MinWidth = sender.SystemOverlayLeftInset;
            ShellTitlebarInset.MinWidth = sender.SystemOverlayRightInset;
        }

        CustomDragRegion.Height = ShellTitlebarInset.Height = sender.Height;
    }


    private void refreshBtn_Tapped(object sender, TappedRoutedEventArgs e)
    {
        
        
    }

    private void backBtn_Tapped(object sender, TappedRoutedEventArgs args)
    {

    }
}

}

WebView provides operation methods on historical records, such as WebView.GoBack() , WebView.GoForward(); , etc. You can directly call these methods to achieve the operations you need.

Here back as an example:

private void backBtn_Tapped(object sender, TappedRoutedEventArgs args)
{
    var currentTab = tabs.SelectedItem as TabViewItem;
    if (currentTab != null)
    {
        var webView = currentTab.Content as WebView;
        if (webView.CanGoBack)
        {
            webView.GoBack();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM