我为wp7创建了一个网络浏览器应用。我在应用程序栏菜单项中添加了6个标签。但现在我想在用户控制页面中单独使用这6个选项卡,如果我单击选项卡2或任何选项卡,它应该现在可以正常工作。
MainPage.xaml中
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox x:Name="UrlTextBox"
KeyDown="UrlTextBox_KeyDown" />
<Grid x:Name="BrowserHost"
Grid.Row="1" />
</Grid>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="1"
Click="TabMenuItem_Click" />
<shell:ApplicationBarMenuItem Text="2"
Click="TabMenuItem_Click" />
<shell:ApplicationBarMenuItem Text="3"
Click="TabMenuItem_Click" />
<shell:ApplicationBarMenuItem Text="4"
Click="TabMenuItem_Click" />
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
MainPage.xaml.cs中
public partial class MainPage : PhoneApplicationPage
{
private const int NumTabs = 4;
private int currentIndex;
private string[] urls = new string[NumTabs];
private WebBrowser[] browsers = new WebBrowser[NumTabs];
public MainPage()
{
InitializeComponent();
ShowTab(0);
}
private void ShowTab(int index)
{
this.currentIndex = index;
UrlTextBox.Text = this.urls[this.currentIndex] ?? "";
if (this.browsers[this.currentIndex] == null)
{
WebBrowser browser = new WebBrowser();
this.browsers[this.currentIndex] = browser;
BrowserHost.Children.Add(browser);
}
for (int i = 0; i < NumTabs; i++)
{
if (this.browsers[i] != null)
{
this.browsers[i].Visibility = i == this.currentIndex ? Visibility.Visible : Visibility.Collapsed;
}
}
}
private void UrlTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Uri url;
if (Uri.TryCreate(UrlTextBox.Text, UriKind.Absolute, out url))
{
this.urls[this.currentIndex] = UrlTextBox.Text;
this.browsers[this.currentIndex].Navigate(url);
}
else
MessageBox.Show("Invalid url");
}
}
private void TabMenuItem_Click(object sender, EventArgs e)
{
int index = Int32.Parse(((ApplicationBarMenuItem)sender).Text) - 1;
ShowTab(index);
}
}
就像下面的浏览器一样。
任何人都可以帮我吗?谢谢你的帮助!
答案 0 :(得分:0)
使用数据透视页面,并将点按事件处理程序功能(类似于您的TabMenuItem_Click)关联,以点按数据透视表标题上的手势。
或者根据您的问题简单地使用带有标题为1,2,3,4的数据透视表页面。在与事件pivot_selectionchanged相关联的事件处理程序中,检查数据透视索引并相应地执行您的工作。
我希望这是你想要的,否则请进一步解释你的问题
答案 1 :(得分:0)
如果您仍想创建用户控件,则不会那么难。在您的项目文件夹中添加新项目作为Windows Phone用户控件。在创建的新页面中,根据需要键入UI的xaml代码(这与任何其他xaml页面类似)及其在后面代码中的行为。
在您的主页中添加以下代码行
&lt; phone:PhoneApplicationPage xmlns:views =“clr-namespace:ProjectName.NameSpace”&gt;
然后使用此语句引用您的控件 &LT;意见:控件&GT; &LT; /视图:控件&GT;
我希望这会有所帮助。如果您还有其他需要,请告诉我