我创建了一个用户控件,顶部有导航按钮和文本框,底部有一个webview,用户控件叫做“browser”。在我的MainPage.xaml中,我有一个Pivot,我加载“这样的浏览器......
<PivotItem>
<local:Browser/>
</PivotItem>
这一切都有效!在我的Pivot上,我添加和删除按钮以创建带有“浏览器”的新PivotItems,并且一切正常!我无法弄清楚如何在“浏览器”控件中从WebView获取WebView.DocumetTitle,以便在创建新的PivotItem时将其放在PivotItem.Header中?
答案 0 :(得分:0)
您可以定义依赖项属性并将PivotItem的标头绑定到它。然后,您可以为WebView注册NavigationCompleted
事件。触发此事件时,您可以获取当前文档页面的标题并将此值分配给依赖项属性。然后,PivotItem的标题将被更新。
我做了一个简单的代码示例供您参考:
<UserControl
x:Class="App1.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="8*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="7*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox x:Name="txbUri" Grid.Row="0" Grid.Column="0"></TextBox>
<Button Content="Go" Grid.Column="1" Grid.Row="0" Click="Button_Click"></Button>
<WebView Grid.Row="1" x:Name="webview" Grid.ColumnSpan="2" NavigationCompleted="WebView_NavigationCompleted"></WebView>
</Grid>
public sealed partial class MyUserControl1 : UserControl
{
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(MyUserControl1), new PropertyMetadata("default"));
public MyUserControl1()
{
this.InitializeComponent();
}
private void WebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
this.Title = sender.DocumentTitle;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
Uri uri = new Uri(txbUri.Text.Trim());
webview.Navigate(uri);
}
catch
{
}
}
}
<Pivot>
<PivotItem Header="{Binding ElementName=uc,Path=Title}">
<local:MyUserControl1 x:Name="uc"></local:MyUserControl1>
</PivotItem>
</Pivot>