我正在 WebView2 中构建一个简单的浏览器,并尝试将页面标题添加到选项卡。我已经编写了脚本来获取页面标题和选项卡名称,但现在我无法设置标题本身。
这是使用选项卡将数据发送到主窗口的脚本。文本变量是选项卡名称。
public void UpdateTab(string text)
{
MainWindow mainwindow = ((MainWindow)Application.Current.MainWindow);
mainwindow.UpdateTabName(webView.CoreWebView2.DocumentTitle, text);
}
编辑:完整代码:(字符串可以是波兰语)
主窗口.xaml
<controls:MicaWindow x:Class="BringBackTheOldEdge.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="clr-namespace:MicaWPF.Controls;assembly=MicaWPF"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BringBackTheOldEdge"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
TitleBarType="WinUI"
SystemBackdropType="Acrylic"
ChangeTitleColorWhenInactive="False"
Title=" " Height="450" Width="800" TitleBarHeight="40"
>
<controls:MicaWindow.TitleBarContent>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center">Edge</TextBlock>
</StackPanel>
</controls:MicaWindow.TitleBarContent>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition Width="23*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Rectangle Fill="#55000000" Grid.ColumnSpan="3" Margin="0,-81,0,0"/>
<TabControl x:Name="tabControl1" Grid.ColumnSpan="3" Background="{x:Null}" SelectionChanged="tabControl1_SelectionChanged">
<TabItem x:Name="AddTabButton" Header="➕"/>
</TabControl>
<Menu x:Name="menu1" Margin="0,2,4,0" Background="{x:Null}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="23" Width="28" HorizontalContentAlignment="Center" Grid.Column="2" MinWidth="28" MinHeight="24" BorderBrush="#66888888" BorderThickness="1,1,1,1">
<MenuItem Header="⚙️" Height="20" Width="27" HorizontalContentAlignment="Center" FontWeight="Regular" HorizontalAlignment="Left" RenderTransformOrigin="0.765,0.502">
<MenuItem Header="_1" IsCheckable="true" Background="{x:Null}" />
<MenuItem Header="_2" IsCheckable="true" Background="{x:Null}" />
<MenuItem Header="_3" IsCheckable="true" Background="{x:Null}" />
</MenuItem>
</Menu>
</Grid>
</controls:MicaWindow>
MainWindow.xaml.cs
using System.Reflection.PortableExecutable;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using MicaWPF.Controls;
using MicaWPF.Core.Services;
using Microsoft.Web.WebView2.Core;
using static System.Net.Mime.MediaTypeNames;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace BringBackTheOldEdge
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MicaWindow
{
public int tabnum = 0;
public MainWindow()
{
InitializeComponent();
var converter = new System.Windows.Media.BrushConverter();
var brush = (Brush)converter.ConvertFromString("#55000000");
int idx = tabControl1.Items.Count;
tabControl1.SelectedIndex = idx - 1;
TabItem ti = new TabItem();
ti.Header = idx / 2;
int tabidx = idx - 1;
RegisterName("tab0", ti);
tabnum++;
var ucont = new UserControl1();
ti.Content = ucont;
ucont.UpdateName("tab0");
ti.Background = brush;
tabControl1.Items.Insert(tabControl1.Items.Count - 1, ti);
TabItem closeBtn = new TabItem();
closeBtn.Header = "x";
closeBtn.Background = brush;
ti.IsSelected = true;
tabControl1.Items.Insert(tabControl1.Items.Count - 1, closeBtn);
tabControl1.SelectedIndex += 1;
}
private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string tabItem = ((sender as TabControl).SelectedItem as TabItem).Header as string;
switch (tabItem)
{
case "➕":
var converter = new System.Windows.Media.BrushConverter();
var brush = (Brush)converter.ConvertFromString("#55000000");
int idx = tabControl1.Items.Count;
tabControl1.SelectedIndex = idx - 1;
TabItem ti = new TabItem();
ti.Header = idx/2;
tabnum++;
RegisterName("tab" + tabnum.ToString(), ti);
var ucont = new UserControl1();
ti.Content = ucont;
ucont.UpdateName("tab" + tabnum.ToString());
ti.Background = brush;
TabItem closeBtn = new TabItem();
closeBtn.Header = "x";
closeBtn.Background = brush;
tabControl1.Items.Insert(tabControl1.Items.Count - 1, ti);
ti.IsSelected = true;
tabControl1.Items.Insert(tabControl1.Items.Count - 1, closeBtn);
break;
case "x":
if (tabControl1.Items.Count == 3)
{
tabControl1.SelectedIndex -= 1;
if (MessageBox.Show("Czy chcesz zamknąć przeglądarkę?", "Uwaga", MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.OK)
{
System.Windows.Application.Current.Shutdown();
}
break;
}
if (tabControl1.SelectedIndex != 1)
{
int idx1 = tabControl1.SelectedIndex - 1;
tabControl1.Items.RemoveAt(idx1);
int idx2 = tabControl1.SelectedIndex;
tabControl1.SelectedIndex-=2;
tabControl1.Items.RemoveAt(idx2);
}
else
{
int idx1 = tabControl1.SelectedIndex - 1;
tabControl1.Items.RemoveAt(idx1);
int idx2 = 0;
tabControl1.SelectedIndex += 1;
tabControl1.Items.RemoveAt(idx2);
}
break;
default:
return;
}
}
public void UpdateTabName(string text, string tab)
{
//here i need to update the tab names
}
}
}
浏览器显示.xaml
<UserControl x:Class="BringBackTheOldEdge.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:BringBackTheOldEdge"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Frame x:Name="viewer" Source="BrowserPage.xaml"/>
</UserControl>
浏览器显示.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BringBackTheOldEdge
{
/// <summary>
/// Logika interakcji dla klasy UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public void UpdateName(string text)
{
var page = new Page1() as Page1;
page.UpdateName(text);
}
}
}
浏览器页面.xaml
<Page x:Class="BringBackTheOldEdge.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:controls="clr-namespace:MicaWPF.Controls;assembly=MicaWPF"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:local="clr-namespace:BringBackTheOldEdge"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid>
<wv2:WebView2 x:Name="webView"
Source="about:blank" Margin="0,43,0,0" SourceChanged="webView_SourceChanged" NavigationCompleted="webView_NavigationCompleted"
/>
<controls:TextBox x:Name="textBox1" KeyDown="OnKeyDownHandler" Margin="163,0,0,0" VerticalAlignment="Top" Background="#00000000" Watermark="Wyszukaj lub wpisz adres..." Height="37" GotMouseCapture="textBox1_GotMouseCapture" GotKeyboardFocus="textBox1_GotKeyboardFocus"/>
<controls:Button Content="←" HorizontalAlignment="Left" VerticalAlignment="Top" Height="37" Width="37" Margin="10,1,0,0" FontSize="10" Background="#00000000" BorderBrush="#00000000" Click="Button_Click"/>
<controls:Button Content="→" HorizontalAlignment="Left" VerticalAlignment="Top" Height="37" Width="37" Margin="47,1,0,0" FontSize="10" Background="#00000000" BorderBrush="#00000000" Click="Button_Click_1"/>
<controls:Button Content="🏠" HorizontalAlignment="Left" VerticalAlignment="Top" Height="37" Width="37" Margin="84,1,0,0" FontSize="10" Background="#00000000" BorderBrush="#00000000" Click="Button_Click_2"/>
<controls:Button Content="🔃" HorizontalAlignment="Left" VerticalAlignment="Top" Height="37" Width="37" Margin="121,1,0,0" FontSize="10" Background="#00000000" BorderBrush="#00000000" Click="Button_Click_3"/>
</Grid>
</Page>
浏览器页面.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MicaWPF.Core.Services;
using Microsoft.Web.WebView2.Core;
namespace BringBackTheOldEdge
{
/// <summary>
/// Logika interakcji dla klasy Page1.xaml
/// </summary>
///
public partial class Page1 : Page
{
public static string tabname; // Modifiable
public Page1()
{
InitializeComponent();
webView.Source = new System.Uri("https://www.msn.com/spartan/dhp?locale=en-US&market=US&enableregulatorypsm=0&enablecpsm=0&ishostisolationenforced=0&targetexperience=default");
MicaWPFServiceUtility.AccentColorService.UpdateAccentsColorsFromWindows();
textBox1.Focus();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
webView.GoBack();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
webView.GoForward();
}
private void webView_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
{
string strr = webView.Source.ToString();
if (webView.Source.ToString() == "https://www.msn.com/spartan/dhp?locale=en-US&market=US&enableregulatorypsm=0&enablecpsm=0&ishostisolationenforced=0&targetexperience=default")
{
strr = "";
}
if (strr.Contains("edge://"))
{
textBox1.Text = "❇️Edge | " + StripPrefix(strr, "edge://"); ;
}
else
{
textBox1.Text = "🔃Wczytywanie | " + StripPrefix(StripPrefix(strr, "https://"), "http://"); ;
}
}
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
if (textBox1.Text.Contains(':'))
{
webView.Source = new System.Uri(textBox1.Text);
}
else
{
webView.Source = new System.Uri("http://" + textBox1.Text);
}
}
}
public static string StripPrefix(string text, string prefix)
{
return text.StartsWith(prefix) ? text.Substring(prefix.Length) : text;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
webView.Source = new System.Uri("https://www.msn.com/spartan/dhp?locale=en-US&market=US&enableregulatorypsm=0&enablecpsm=0&ishostisolationenforced=0&targetexperience=default");
}
private void webView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
string strr = webView.Source.ToString();
if (strr.Contains("https:"))
{
strr = "🔒Zabezpieczona | " + strr;
}
else
{
strr = "⚠️Potencjalnie niebezpieczna | " + strr;
}
if (webView.Source.ToString() == "https://www.msn.com/spartan/dhp?locale=en-US&market=US&enableregulatorypsm=0&enablecpsm=0&ishostisolationenforced=0&targetexperience=default")
{
strr = "";
}
textBox1.Text = StripPrefix(StripPrefix(strr, "https://"), "http://"); ;
UpdateTab(tabname);
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
webView.Reload();
}
private void textBox1_GotMouseCapture(object sender, MouseEventArgs e)
{
textBox1.SelectAll();
}
private void textBox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
}
public void UpdateName(string text)
{
tabname = text;
}
public void UpdateTab(string text)
{
MainWindow mainwindow = ((MainWindow)Application.Current.MainWindow);
mainwindow.UpdateTabName(webView.CoreWebView2.DocumentTitle, text);
}
}
}
快速解决方案: 改进 UpdateTabName
public void UpdateTabName(string text, string tab)
{
var list = tabControl1.Items.Cast<TabItem>().ToList();
TabItem tabItem;
if (tab.Equals("tab0"))
{
tabItem = list.Where(p => p.Name.Equals("tab1")).FirstOrDefault();
}
else
{
tabItem = list.Where(p => p.Name.Equals(tab)).FirstOrDefault();
}
tabItem.Header = text;
}
在 tabControl1_SelectionChanged 方法中,在 RegisterName("tab" + tabnum.ToString(), ti); 上方添加以下代码
ti.Name = "tab" + tabnum.ToString();
RegisterName("tab" + tabnum.ToString(), ti);
我在你的项目中发现了其他bug,你可以检查一下。
Bug1:你的RegisterName方法似乎没有效果
解决方案:使用 ti.Name = "tab" + tabnum.ToString();命名
Bug2:创建第一个TabItem时执行了tabControl1_SelectionChanged方法,但TabItem的名称错误
解决方案:这里我会用条件判断来解决你一开始的问题,后面你可以完善你的逻辑
Bug3:TabItem 创建和 Header 更新之间存在延迟
解决方案: 在 tabControl1_SelectionChanged 中
TabItem ti = new TabItem();
ti.Header = idx / 2;
ti.Header可以直接给出正确的文本,所以不需要执行UpdateTabName