Как найти TabItem по имени?C#

Место общения программистов C#
Anonymous
Как найти TabItem по имени?

Сообщение Anonymous »

Я создаю простой браузер в WebView2 и пытаюсь добавить заголовки страниц на вкладки. Сделал скрипт для получения заголовка страницы и названия вкладки, только сейчас не могу задать сам заголовок.
Вот скрипт для отправки данных в главное окно с вкладками . Текстовая переменная — это имя вкладки.
public void UpdateTab(string text)
{
MainWindow mainwindow = ((MainWindow)Application.Current.MainWindow);
mainwindow.UpdateTabName(webView.CoreWebView2.DocumentTitle, text);
}

РЕДАКТИРОВАТЬ: Полный код: (строки могут быть на польском языке)
MainWindow.xaml




Edge






















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
{
///
/// Interaction logic for MainWindow.xaml
///
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
}
}

}

BrowserDisplay.xaml




BrowserDisplay.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
{
///
/// Logika interakcji dla klasy UserControl1.xaml
///
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}

public void UpdateName(string text)
{
var page = new Page1() as Page1;
page.UpdateName(text);

}
}
}

BrowserPage.xaml

xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/marku ... ility/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">











BrowserPage.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
{
///
/// Logika interakcji dla klasy Page1.xaml
///
///

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= ... ce=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= ... ce=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= ... ce=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= ... ce=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);
}
}
}



Подробнее здесь: https://stackoverflow.com/questions/786 ... em-by-name

Вернуться в «C#»