簡體   English   中英

如何對這些選項卡使用用戶控件

[英]How to use the user control for these tabs

我已經為wp7創建了一個Web瀏覽器應用程序。 在那我在應用程序欄菜單項中添加了一些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);
}
}

就像下面的瀏覽器一樣。 在此處輸入圖片說明

誰能幫我這個? 謝謝你的幫助!

使用樞軸頁面,並關聯您的輕擊事件處理程序功能(類似於TabMenuItem_Click),以輕按樞軸頭上的手勢。

或者根據您的問題使用標題為1,2,3,4的數據透視頁。 在與事件關聯的事件處理程序中,pivot_selectionchanged檢查樞軸索引,並相應地進行工作。

我希望這是您想要的,否則請進一步解釋您的問題

如果您仍然想創建一個用戶控件,那就沒有那么難了。 在項目文件夾中,將新項目添加為Windows Phone用戶控件。 在創建的新頁面中,根據需要鍵入UI的xaml代碼(這與任何其他xaml頁面類似),並在后面的代碼中顯示其行為。

在您的主頁中添加以下代碼行
<phone:PhoneApplicationPage xmlns:views =“ clr-namespace:ProjectName.NameSpace”>

然后使用此語句<views:controlName> </ views:controlName>引用您的控件

我希望這有幫助。 需要幫助請叫我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM