In my C# Windows Phone 8 app, I have an AppBar. I have two icons on this AppBar, one new icon, and one edit icon. I want to change the edit icon, to the back icon whenever it is pressed, and then back to the edit icon whenever it is pressed again. I have tried this code, but I get a nullReferenceException:
public static Uri addIcon = new Uri("/Assets/AppBar/new.png", UriKind.Relative);
public static Uri backIcon = new Uri("/Assets/AppBar/back.png", UriKind.Relative);
//Edit the projects
if (editMode.Equals(false))
{
//EditMode is off, enable edit mode and modify the editprojectMenuButton
editMode = true;
editprojectMenuButton.IconUri = backIcon;
editprojectMenuButton.Text = "finish";
} else
{
//EditMode is on, disable edit mode and modify the editprojectMenubutton
editMode = false;
editprojectMenuButton.IconUri = addIcon;
editprojectMenuButton.Text = "edit";
}
Xaml code:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton x:Name="editprojectMenuButton" IconUri="/Assets/AppBar/edit.png" Text="Edit" Click="editprojectMenuButton_Click"/>
<shell:ApplicationBarIconButton x:Name="addprojectMenuButton" IconUri="/Assets/AppBar/new.png" Text="Add" Click="addprojectMenuButton_Click"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem x:Name="aboutButton" Text="About" Click="aboutButton_Click"/>
<shell:ApplicationBarMenuItem x:Name="howtoButton" Text="How To" Click="howtoButton_Click"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
Alternate AppBar Code:
//Create the AppBar
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.IsMenuEnabled = true;
addprojectMenuBtn = new ApplicationBarIconButton(new Uri("BarIcons/add.png", UriKind.Relative));
addprojectMenuBtn.Text = "add";
addprojectMenuBtn.Click += addprojectMenuButton_Click;
editprojectMenuBtn = new ApplicationBarIconButton(new Uri("BarIcons/edit.png", UriKind.Relative));
editprojectMenuBtn.Text = "add";
editprojectMenuBtn.Click += editprojectMenuButton_Click;
ApplicationBar.Buttons.Add(addprojectMenuBtn);
ApplicationBar.Buttons.Add(editprojectMenuBtn);
It seems that when you create AppBar in xaml, your reference to Button in code behind is null.
I'm making my AppBar programatically and I've not spotted such problem:
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.IsMenuEnabled = false;
ApplicationBarIconButton addBtn = new ApplicationBarIconButton(new Uri("BarIcons/add.png", UriKind.Relative));
addBtn.Text = "Add";
addBtn.Click += addBtn_Click;
ApplicationBarIconButton infoBtn = new ApplicationBarIconButton(new Uri("BarIcons/info.png", UriKind.Relative));
infoBtn.Text = "Info";
infoBtn.Click += infoBtn_Click;
ApplicationBar.Buttons.Add(addBtn);
ApplicationBar.Buttons.Add(infoBtn);
You can also add menu items there and what you want. Then if you ApplicationBarIconButton is 'global' variable, you can change it any time, and it works.
EDIT - some explanations
Here I've found similar problem, and at this blog is explanation.
There is also a code which works:
Microsoft.Phone.Shell.ApplicationBarIconButton btn = ApplicationBar.Buttons[0] as Microsoft.Phone.Shell.ApplicationBarIconButton;
btn.IconUri = backIcon;
btn.Text = "My button";
Related
I am coding an Application to create a Mindmap like System. At the Moment I do that by Spawning a Button with specific text:
Button newButton = new Button();
this.Controls.Add(newButton);
ColorDialog MyDialog = new ColorDialog();
MyDialog.AllowFullOpen = false;
MyDialog.ShowHelp = true;
MyDialog.Color = Color.White;
if (MyDialog.ShowDialog() == DialogResult.OK)
newButton.BackColor = MyDialog.Color;
newButton.Text = inputfield.Text;
newButton.Location = new Point(70, 170);
newButton.Size = new Size(100, 50);
newButton.Show();
Now I am trying to assign a drag and drop function to it, and also have the code for it which is working, but my problem is that I cant assign a newButton_MouseDown event on it.
I tried this:
newButton.MouseDown += new MouseEventHandler(this.newButton_MouseDown);
But it doesn't work. I also couldn't find an answer anywhere else.
I am currently working in Windows 10 and I am working on AppBar and Command Bar controls. I created an Appbar inside which I added a commandBar which contains and app bar button. Now the issue is it is showing more icon for both commandBar and AppBar. Can you please suggest how can I remove this?
My Code is below
CommandBar commandBar = new CommandBar();
if (this.BottomAppBar == null)
{
this.BottomAppBar = new AppBar();
this.BottomAppBar.IsOpen = true;
this.BottomAppBar.IsSticky = true;
}
commandBar.PrimaryCommands.Clear();
commandBar.IsSticky = true;
commandBar.IsOpen = true;
commandBar.ClosedDisplayMode = AppBarClosedDisplayMode.Compact;
AppBarButton appBarButton = new AppBarButton();
appBarButton.Foreground = MCSExtensions.GetColorFromHex(foreground_color);
appBarButton.Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Images/submit.png", UriKind.Absolute) };
appBarButton.Label = formButton.B_NAME;
commandBar.PrimaryCommands.Add(appBarButton);
this.BottomAppBar.Content=commandBar;
The above is generating following output
What I actually want is left side bar that is commandBar, not the gray portion. Can someone suggest what I am doing wrong? I also tried just adding CommmandBar inside grid but it didn't display at all.
Update
CommandBar commandBar=new CommmandBar();
commandBar.IsOpen=true;
commandBar.IsSticky=true;
commandBar.ClosedDisplayMode=AppBarClosedDisplatMode.Compact;
AppBarButton appBarButton = new AppBarButton();
appBarButton.Foreground = MCSExtensions.GetColorFromHex(foreground_color);
appBarButton.Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Images/submit.png", UriKind.Absolute) };
appBarButton.Label = formButton.B_NAME;
commandBar.PrimaryCommands.Add(appBarButton);
pageLayoutRootGrid.Children.Add(commandBar);
I also tried adding a StackPanel in AppBar as below but not giving proper results.
if (this.BottomAppBar == null)
{
this.BottomAppBar = new AppBar();
this.BottomAppBar.IsOpen = true;
this.BottomAppBar.IsSticky = true;
}
StackPanel appBarPanel=new StackPanel();
appBarPanel.Orientation=Orientation.Horizontal;
AppBarButton appBarButton = new AppBarButton();
appBarButton.Foreground = MCSExtensions.GetColorFromHex(foreground_color);
appBarButton.Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Images/submit.png", UriKind.Absolute) };
appBarButton.Label = formButton.B_NAME;
appBarPanel.Children.Add(appBarButton);
this.BottomAppBar.Content=appBarPanel;
For xaml:
<Page.BottomAppBar>
<CommandBar>
<CommandBar.PrimaryCommands>
<AppBarButton Icon="Accept"/>
</CommandBar.PrimaryCommands>
</CommandBar>
</Page.BottomAppBar>
this line make your UI messed:
this.BottomAppBar = new AppBar();
as you can see from my xaml, create only 1 CommandBar inside BottomAppBar
Edit: if you want to use code, use this:
var commandBar = new CommandBar();
commandBar.PrimaryCommands.Add(new AppBarButton() { Label = "test" });
commandBar.VerticalAlignment = VerticalAlignment.Bottom;
this.root.Children.Add(commandBar);
UPDATE
Full xaml code:
<Page
x:Class="CommandBarSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CommandBarSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="root">
</Grid>
</Page>
The code behind:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var commandBar = new CommandBar();
commandBar.VerticalAlignment = VerticalAlignment.Bottom;
var appBarButton = new AppBarButton() { Icon = new SymbolIcon(Symbol.Accept), Label = "Accept" };
commandBar.PrimaryCommands.Add(appBarButton);
root.Children.Add(commandBar);
}
}
I need to add a grid to a popup in Silverlight project. I have added this code in the mainpage.xaml.cs file I have created a popup.
but I need to add a grid showing a table from the database.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
popup p = new Popup();Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(5.0);
StackPanel panel1 = new StackPanel();
panel1.Background = new SolidColorBrush(Colors.LightGray);
Button button1 = new Button();
button1.Content = "Close";
button1.Margin = new Thickness(2.0);
button1.Click += new RoutedEventHandler(button1_Click);
TextBlock textblock1 = new TextBlock();
textblock1.Text = "The popup control";
textblock1.Margin = new Thickness(5.0);
panel1.Children.Add(textblock1);
panel1.Children.Add(button1);
border.Child = panel1;
Grid grid1 = new Grid();
// i need to bind this grid to data from the sql database and attach this grid to the popup
// Set the Child property of Popup to the border
// which contains a stackpanel, textblock and button.
p.Child = border;
p.DataContext =
// Set where the popup will show up on the screen.
p.VerticalOffset = 200;
p.HorizontalOffset = 300;
// Open the popup.
p.IsOpen = true;
}
The Grid is a layout control, and not for displaying tables. For tabular data, you'll need a DataGrid control.
List<Customer> customerList = new List<Customer>();
var dataGrid = new DataGrid();
dataGrid.ItemsSource = customerList;
Popup popup = new Popup();
popup.Child = dataGrid;
popup.IsOpen = true;
Icons do not appear in ApplicationBar.
MainPage.xaml.cs
// Конструктор
public MainPage()
{
InitializeComponent();
// Локализация ApplicationBar
BuildLocalizedApplicationBar();
}
// ApplicationBar
private void BuildLocalizedApplicationBar()
{
// Установка нового экземпляра ApplicationBar.
ApplicationBar = new ApplicationBar();
// Задаем опции
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.Opacity = 1.0;
ApplicationBar.IsVisible = true;
ApplicationBar.IsMenuEnabled = true;
// Кнопка "Add"
ApplicationBarIconButton Add = new ApplicationBarIconButton(new Uri("/Assets/AppBar/Add.png", UriKind.Relative));
Add.Text = AppResources.AppBarAdd;
ApplicationBar.Buttons.Add(Add);
Add.Click += new EventHandler(Button_Add_Click);
// Кнопка "Edit"
ApplicationBarIconButton Edit = new ApplicationBarIconButton(new Uri("/Assets/AppBar/Edit.png", UriKind.Relative));
Edit.Text = AppResources.AppBarEdit;
ApplicationBar.Buttons.Add(Edit);
Edit.Click += new EventHandler(Button_Edit_Click);
// Кнопка "Delete"
ApplicationBarIconButton Delete = new ApplicationBarIconButton(new Uri("/Assets/AppBar/Delete.png", UriKind.Relative));
Delete.Text = AppResources.AppBarDelete;
ApplicationBar.Buttons.Add(Delete);
Delete.Click += new EventHandler(Button_Delete_Click);
// Кнопка "Connect"
ApplicationBarIconButton Connect = new ApplicationBarIconButton(new Uri("/Assets/AppBar/Connect.png", UriKind.Relative));
Connect.Text = AppResources.AppBarConnect;
ApplicationBar.Buttons.Add(Connect);
Connect.Click += new EventHandler(Button_Connect_Click);
}
Icons in the folder / Assets / AppBar / is, standard icons, pulled out of most SDK, size 76x76, for Dark theme.
The problem on the emulator and on the phone.
I've tried several things and eventualy just put the image directly inside C:\Users\Gebruiker\Documents\Visual Studio 2012\Projects\FolderMonitor\FolderMonitor\bin\Debug. This works for now, but idealy I'd like to set the notifyIcon.Icon = new Icon("folder.ico") to an image inside a images folder in the solution. But I can't figure out how this works..
public FolderMonitorApplicationContext()
{
this.monitor = new Monitor();
notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon("folder.ico");
notifyIcon.Text = "Folder Monitor";
notifyIcon.Visible = true;
contextMenu = new ContextMenuStrip();
openMonitor = new ToolStripMenuItem();
exitApplication = new ToolStripMenuItem();
notifyIcon.ContextMenuStrip = contextMenu;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
openMonitor.Text = "Open";
openMonitor.Click += new EventHandler(OpenMonitor_Click);
contextMenu.Items.Add(openMonitor);
exitApplication.Text = "Exit..";
exitApplication.Click += new EventHandler(ExitApplication_Click);
contextMenu.Items.Add(exitApplication);
}
So it's currently working, but not how it i'd like it to work. Hope you can help me out here, thanks in advance.
After adding the picture file to your project, bring up the item properties by clicking on it, and pressing F4. Under Build Action, change it to "Embedded Resource".
You can access embedded resources in Stream form like this:
public FolderMonitorApplicationContext()
{
this.monitor = new Monitor();
notifyIcon = new NotifyIcon();
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
"<project namespace>.<folder path>" + "filename.ico"))
{
notifyIcon.Icon = new Icon(stream);
}
notifyIcon.Text = "Folder Monitor";
notifyIcon.Visible = true;
contextMenu = new ContextMenuStrip();
openMonitor = new ToolStripMenuItem();
exitApplication = new ToolStripMenuItem();
notifyIcon.ContextMenuStrip = contextMenu;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
openMonitor.Text = "Open";
openMonitor.Click += new EventHandler(OpenMonitor_Click);
contextMenu.Items.Add(openMonitor);
exitApplication.Text = "Exit..";
exitApplication.Click += new EventHandler(ExitApplication_Click);
contextMenu.Items.Add(exitApplication);
}
Use the same method to insert the icon on the form.
To find the same code in your application to copy in your systemtray please:
check if, in the file .resx, there is the icon called with $ symbol(example:"$this.icon")
find your code to include the icon on the [name of form].desiner.cs.
example of code in my application:
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(systemtray));
trayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));