在Windows Phone 7的環境中,在"功能表的部分也做了變更,現在除了傳統的文字功能表之外,也加入了ICON,而今天就來看看這個部分,主要是操作ApplicationBar與ApplicationBarIconButton,而建立的方式可以由XAML的介面建立,也可以純用程式碼來做,這一篇先來看看程式碼的部分
在Windows Phone 7的環境中,在"功能表的部分也做了變更,現在除了傳統的文字功能表之外,也加入了ICON,而今天就來看看這個部分,主要是操作ApplicationBar與ApplicationBarIconButton,而建立的方式可以由XAML的介面建立,也可以純用程式碼來做,這一篇先來看看程式碼的部分
{
public partial class MainPage : PhoneApplicationPage
{
TextBox tbSelectedItem = null;
Button btnShowAppBar = null;
// Constructor
public MainPage()
{
InitializeComponent();
this.ApplicationTitle.Text = "ApplicationBar Demo";
//set up content grid
RowDefinition RowOne = new RowDefinition();
RowOne.Height = new GridLength(100, GridUnitType.Pixel);
ContentGrid.RowDefinitions.Add(RowOne);
RowDefinition RowTwo = new RowDefinition();
RowTwo.Height = new GridLength(100, GridUnitType.Pixel);
ContentGrid.RowDefinitions.Add(RowTwo);
RowDefinition RowThree = new RowDefinition();
RowThree.Height = new GridLength(100, GridUnitType.Auto);
ContentGrid.RowDefinitions.Add(RowThree);
//add a new textbox to content grid
tbSelectedItem = new TextBox();
tbSelectedItem.Name = "txtSelectedItem";
tbSelectedItem.Text = "some text here";
tbSelectedItem.Visibility = Visibility.Visible;
Grid.SetRow(tbSelectedItem, 0);
this.ContentGrid.Children.Add(tbSelectedItem);
//add a button to contetn grid
btnShowAppBar=new Button();
btnShowAppBar.Content = "Show AppBar";
Grid.SetRow(btnShowAppBar, 1);
this.ContentGrid.Children.Add(btnShowAppBar);
btnShowAppBar.Click += new RoutedEventHandler(btnShowAppBar_Click);
//add a slider control to content grid
Slider sdChangeOpacity = new Slider();
sdChangeOpacity.Maximum=100; sdChangeOpacity.Minimum=0;
Grid.SetRow(sdChangeOpacity, 2);
this.ContentGrid.Children.Add(sdChangeOpacity);
sdChangeOpacity.ValueChanged += new RoutedPropertyChangedEventHandler(sdChangeOpacity_ValueChanged);
}
//改變ApplicationBar的透明度
void sdChangeOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
if (this.ApplicationBar == null)
return;
this.ApplicationBar.Opacity = e.NewValue / 100;
}
void btnShowAppBar_Click(object sender, RoutedEventArgs e)
{
AddIcon();
}
private void AddIcon()
{
if (this.ApplicationBar != null)
this.ApplicationBar.Buttons.Clear();
else
this.ApplicationBar = new ApplicationBar();
ApplicationBarIconButton[] newIconButton = new ApplicationBarIconButton[4];
for (int i = 0; i < 4; i++)
{
newIconButton[i] = new ApplicationBarIconButton();
newIconButton[i].IconUri = new Uri("Images/img" + i.ToString() + ".png", UriKind.Relative);
newIconButton[i].Text = "Icon-" + i.ToString();
newIconButton[i].Click += new EventHandler(IconButton_Click);
this.ApplicationBar.Buttons.Add(newIconButton[i]);
}
this.ApplicationBar.IsVisible = true;
}
//applicationBar按鈕按下時的動作
void IconButton_Click(object sender, EventArgs e)
{
ApplicationBarIconButton item = sender as ApplicationBarIconButton;
tbSelectedItem.Text = item.Text;
}
}
}
執行的結果來看看影片的示範