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
Related
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.
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;
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
Appbar works great but when I use
this.Content = new PAGE(Constructor1, Constructor1, Constructor1);
to navigate to other page....App bar doesn't show up....:|
Same Xaml works for other page what I am navigating from UI...(Right click>navigate to) but not for when I use the code.....
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">
<shell:ApplicationBarIconButton IconUri="/Images/appbar.cancel.rest.png" Text="Cancel"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>
Wouldn't changing this.Content replace the page's content, including the AppBar, by design?
I use NavigationService to navigate between pages. If I have an AppBar I just recreate it on each page.
Alternately, redesign your layout so that the content you are replacing does not include the AppBar (e.g. use a stackpanel or grid to hold your content with the AppBar higher up in the hierarchy).
There are a few issues I notice with the beta version of the WP7 tools:
The ApplicationBar no longer causes a page to resize it contents with the CTP workaround
Using the ApplicationBar from App.xaml and imported as a static resource does not work as expected
In the CTP version of the WP7 tools when using an appbar and navigation from page to page, the appbar remainined on top of the page content rather than having the content resize itself to be on top of the appbar. The work around was to set the IsVisible property to false in the constructor and set to true on the page loaded event. This is no longer working and the appbar remains on top of the loaded page unless turned completely off. I have my appbar xaml in app.xaml and each page uses it as a static resource.
Also the click event is no longer triggered for app buttons, I am using the appbar as a resource in my App.xaml and added as a {StaticResource} in my page's xaml. Any help would be appreciated as this is the only way I have for navigating around my application, aside from rolling my own navigation page.
public CalculatorView()
{
InitializeComponent();
ApplicationBar.IsVisible = false;
SupportedOrientations = SupportedPageOrientation.Portrait;
Application.Current.RootVisual = this;
}
private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
{
ApplicationBar.IsVisible = true;
}
AppBar XAML:
<Shell:ApplicationBar x:Key="GlobalApplicationBar" IsVisible="True" IsMenuEnabled="True">
<Shell:ApplicationBar.Buttons>
<Shell:ApplicationBarIconButton x:Name="CalculaterAppIconButton" Click="CalculaterMenuItemClick" IconUri="/Images/Icons/32/Back.png" Text="Main" />
<Shell:ApplicationBarIconButton x:Name="HistoryAppIconButton" Click="HistoryMenuItemClick" IconUri="/Images/Icons/32/Intl-History.png" Text="History" />
<Shell:ApplicationBarIconButton x:Name="StatisticsAppIconButton" Click="StatisticsMenuItemClick" IconUri="/Images/Icons/32/Stats.png" Text="Stats" />
<Shell:ApplicationBarIconButton x:Name="OptionsAppIconButton" Click="OptionsMenuItemClick" IconUri="/Images/Icons/32/Settings.png" Text="Options" />
</Shell:ApplicationBar.Buttons>
<Shell:ApplicationBar.MenuItems>
<Shell:ApplicationBarMenuItem x:Name="StartingHandsMenuItem" Click="StartingHandsMenuItemClick" Text="Starting Hands" />
<Shell:ApplicationBarMenuItem x:Name="HoleOddsMenuItem" Click="HoleOddsMenuItemClick" Text="Hole Odds" />
</Shell:ApplicationBar.MenuItems>
</Shell:ApplicationBar>
AppBar in Phone Application Page:
ApplicationBar="{StaticResource GlobalApplicationBar}" //In my page.xaml
Putting aside the fact you are refering to perceived differences between the Beta and CTP versions of the tools.
The ApplicationBar appear over the top of the page content based on it's opacity:
If the opacity is set to 1, the
displayed page will be resized to be
the area of the screen not covered by
the Application Bar.
from MSDN