WP 8.1 BarButton Icon changing - c#

I'm just starting to learn C# and WP platform and i find it really hard to do some easy things, like changing a button icon, etc.
I've got a Command Bar and a AppBarButton created in XAML.
<Page.BottomAppBar>
<CommandBar MinHeight="60">
<AppBarButton x:Name="Command_BarButton" Icon="AllApps"
Label="Seletie"
Click="AppBar_Select"/>
</CommandBar>
</Page.BottomAppBar>
I would like to change the AppBarButton icon programatically in C# to another prexistent icon, like the Trash icon. How can i do this ?
Command_BarButton.Label = "Delete";
Command_BarButton.Icon = ?

I knew it would be very easy to change the AppBarButton Icon at runtime.
This is how:
Command_BarButton.Label = "Delete";
Command_BarButton.Icon = new SymbolIcon(Symbol.Delete);
Thanks to this link

to change Icon from code behind try this
ApplicationBar = new ApplicationBar();
ApplicationBarIconButton button1 = new ApplicationBarIconButton();
button1.IconUri = new Uri("/Images/play.png", UriKind.Relative);
button1.Text = "play";
ApplicationBar.Buttons.Add(button1);
source

this shuold help
<AppBarButton Label="BitmapIcon" Click="AppBarButton_Click">
<AppBarButton.Icon>
<BitmapIcon UriSource="ms-appx:///Assets/globe.png"/>
</AppBarButton.Icon>
</AppBarButton>
source

Related

Can I bind AppBarButtons into CommandBar.PrimaryCommands

I want to bind an AppBarButtons into a CommandBar.
I tried looking through the CommandBar documentation and found out that part is PrimaryCommands but I try to bind it but it read-only and I can't write anything to it.
Here is what I wanted to do.
<CommandBar PrimaryCommands={x:Bind AdditionCommands}/>
So, later on, I could just:
<customUI:PauseMenu>
<customUI:PauseMenu.AdditionCommands>
<AppBarButton Label="Some"/>
<AppBarButton Label="Commands"/>
<AppBarButton Label="Heres"/>
</customUI:PauseMenu.AdditionCommands>
</customUI:PauseMenu>
Can I bind AppBarButtons into CommandBar.PrimaryCommands
CommandBar does not support bind PrimaryCommands like the above. The only implementation way of PrimaryCommands is create list AppBarButton under that node. Please refer this document.
<CommandBar.PrimaryCommands>
<AppBarButton Label="Some"/>
<AppBarButton Label="Commands"/>
<AppBarButton Label="Heres"/>
</CommandBar.PrimaryCommands>

How to show AppBarButton Label in initial load?

In the following code, the app bar is rendered without the AppBarButton Label text. Text shows only when hamburger button is clicked and bar is expanded. How can I make text show along with the icon initially?
CommandBar cb2 = new CommandBar();
AppBarButton b3 = new AppBarButton();
b3.Icon = new SymbolIcon(Symbol.Help);
b3.HorizontalAlignment = HorizontalAlignment.Left;
b3.Label = "HELP";
cb2.PrimaryCommands.Add(b3);
BottomAppBar = cb2;
Command Bar has a property IsOpen. Set that to True. Then CommandBar is always open.
Update
<CommandBar IsOpen="True" Name="xCommand" IsSticky="True" IsHitTestVisible="False">
<AppBarButton Icon="Add" Label="Add New Info"/>
</CommandBar>
This is my XAML. The command Bar Stays open at all times.

Changing Grid Image source on Button Click via Xaml.cs

I am fairly new to Windows Store Application Development. I am stuck at trying to change the image of the grid via c# code.
This is my Mainpage.Xaml code
<Grid x:Name="BG" KeyDown="operation" Width="1327" Height="740">
<Grid.Background>
<ImageBrush Stretch="UniformToFill" ImageSource="Assets/background.jpg"/>
</Grid.Background>
<Button x:Name="ThemeButton" BorderBrush="{x:Null}" FontSize="14" Content="Theme 1" Margin="25,0,0,0" HorizontalAlignment="Left" Click="ChangeBG"></Button>
This is my Mainpage.Xaml.cs Code
private void ChangeBG(object sender, RoutedEventArgs e)
{
ImageBrush b1 = new ImageBrush();
b1.ImageSource = new BitmapImage(new Uri(#"D:\Visual Studio Project File\Sadiqali Calculator\Sadiqali Calculator\Assets\Theme2.png"));
BG.Background = b1;
}
When the button is pressed I am getting a black background instead of the picture. The above code is from other threads.
How can I change the image source of the grid via code on click of a button?
Your code works fine here when I use my own images.
Check the location of D:\Visual Studio Project File\Sadiqali Calculator\Sadiqali Calculator\Assets\Theme2.png ... are you sure this is valid?
Also - ChangeBG will run on the UI thread if invoked from the button click.
Thank You Everyone! You have helped me solve it!
This is what I changed the code to and it worked.
ImageBrush b1 = new ImageBrush();
b1.ImageSource = new BitmapImage(new Uri(#"ms-appx:///Assets/Theme2.png", UriKind.RelativeOrAbsolute));
BG.Background = b1;

Manipulating Application Bars in Windows Phone 8.1 XAML from Code Behind

This might be a trivial beginner question and I have found quite a bit of related information for Windows and Silverlight apps but nothing that would directly help me. I'm writing a Windows Phone 8.1/WinRT app in C# and XAML, and I would like to programmatically modify application bars created in XAML. For instance, there is a button I want to include in debug builds only, using preprocessor directives in code behind.
In the following XAML code I'm creating a BottomAppBar with two buttons. How can I create the second button (AppBarAddSampleItemsButton) including all properties in code behind?
<prism:VisualStateAwarePage.BottomAppBar>
<CommandBar >
<CommandBar.PrimaryCommands>
<AppBarButton x:Uid="AppBarNewItemButton"
Label="New item"
Icon="Add" Command="{Binding GoToAddItemPageCommand}" />
</CommandBar.PrimaryCommands>
<CommandBar.SecondaryCommands>
<AppBarButton x:Uid="AppBarAddSampleItemsButton"
Label="Add sample items"
Command="{Binding GoToAddSampleItemssPageCommand}" />
</CommandBar.SecondaryCommands>
</CommandBar>
</prism:VisualStateAwarePage.BottomAppBar>
Here is a sample code creating an AppBarButton in the code behind and adding it to BottomAppBar of the current Page:
private void AddButtonToAppBar()
{
AppBarButton buttonToAdd = new AppBarButton { Label = "Label", Icon = new SymbolIcon(Symbol.Help) };
buttonToAdd.Click += async (sender, e) => await new MessageDialog("Button clicked").ShowAsync();
// add button to Page's BottoAppBar
(BottomAppBar as CommandBar).PrimaryCommands.Add(buttonToAdd);
}
Edit - as for Binding (again from the top of my head, so you will have to check this) this should probably work:
Binding myBind = new Binding();
myBind.Path = new PropertyPath("GoToAddSampleItemssPageCommand");
myBind.Source = DataContext;
buttonToAdd.SetBinding(AppBarButton.CommandProperty, myBind);
More about DataBinding at MSDN.
After some digging around on the Windows Phone dev center, I found this page: How to change app bar icon buttons and menu items dynamically for Windows Phone. Hope it helps!

Load images to application bar in wp7

I'm developing Windows phone apps and I'm new to it,I'm using below code to add PNG image to application bar at runtime
this.ApplicationBar = new ApplicationBar();
this.ApplicationBar.Opacity = 1;
this.ApplicationBar.Mode = ApplicationBarMode.Minimized;
ApplicationBarIconButton btn = new ApplicationBarIconButton();
btn .IconUri = new Uri("/Resources/car.png", UriKind.Relative);
btn .Text = "Car";
this.ApplicationBar.Buttons.Add(btn);
But image is not loading it shows 'X' inside application bar circle, i tried also in design using below code
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Resources/car.png" Text="Car" IsEnabled="False" />
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="About" />
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
but did not work, My image size is 48 x 48 pixels, white foreground graphic for the button in 26 x 26. Did i miss something,Please help me.
Check the properties of the image file in the project: you should have Build Action set to Content in order to the file go with the final .XAP file

Categories

Resources