Adding a new tab in tabview with webview?

Mark Mather 156 Reputation points
2020-05-09T23:02:35.287+00:00

I added the code for the add tab but I'm getting errors I tried for a couple of days to correct the errors. I need some assistance. I posted the code below with a screenshot of code here of the new tab button.

        private void AddTab_Click(object sender, RoutedEventArgs e)  
        {  
            var newTab = new TabViewItem();  
            newTab.IconSource = new SymbolIconSource() { Symbol = Symbol.Document };  
            newTab.Header = "New Tab";  
  
            Frame frame = new Frame();![8012-error.png][1]  
            newTab.Content = frame;  
            frame.Navigate(typeof("www.msn.com"));  
  
            sender.TabItems.add(newTab);  
        }  
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Daniele 1,996 Reputation points
    2020-05-10T06:55:49.35+00:00

    In addition to the 2 errors in the image attached I think that there is this error too: you are trying to open a website with a Frame, I think you need a WebView.

    Moreover I think that you are using Microsoft.Toolkit.Uwp.UI.Controls.TabView but you are looking sample code for Microsoft.UI.Xaml.Controls.TabView

    • Microsoft.Toolkit.Uwp.UI.Controls.TabView comes with Microsoft.Toolkit.Uwp.UI.Controls NuGet package
    • Microsoft.UI.Xaml.Controls.TabView comes with Microsoft.UI.Xaml NuGet package

    Try to change your method in this way:

    private void AddTab_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        var newTab = new TabViewItem {Icon = new SymbolIcon {Symbol = Symbol.Document}, Header = "New Tab"};
    
        WebView frame = new WebView(); 
        newTab.Content = frame;
        frame.Navigate(new Uri("http://www.msn.com"));
    
        Tabs.Items.Add(newTab);
        Tabs.SelectedItem = newTab;
    }
    

1 additional answer

Sort by: Most helpful
  1. Suresh Patra 1 Reputation point
    2020-05-09T23:07:36.34+00:00

    Hi Mark,

    Have you added Microsoft.Toolkit.Uwp.UI.Controls.dll assembly?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.