How to change the image on the ApplicationBar from

2020-04-14 08:08发布

问题:

I have a small C# project that has an ApplicationBar. But I have a small problem: I want 8 icons on the bar, and the ApplicationBar only supports 4. I came up with a solution (in C#): add a small CheckBox to ask if the user wants to use the first or second set of tools.

But I'm still not able to change the icons on the ApplicationBar. I tried removing the old ones, first with ApplicationBar.MenuItems.Remove(Button1); and then with ApplicationBar.Buttons.Remove(Button1); but neither worked. I tried changing the .IconUri property of the button, but that gave me a NullReferenceException.

回答1:

I don't understand what you mean by changing it from "C#, not Silverlight". C# is a programming language and Silverlight is a framework. Nevertheless, the link you posted to explains exactly how you do it. The ApplicationBar is not a Silverlight control, it's part of the native OS. You can use the code in the link or do something like this:

firstAppBarButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
firstAppBarButton.Text = "New Text";
firstAppBarButton.IconUri = new Uri("/appbarIcon.png",UriKind.Relative);

You need to get the ApplicationBarIconButton via the index (0 for first one, 1 for second etc..) instead of by name.



回答2:

You can't refer to the application buttons by name. Try:

((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Remove

I would also suggest that you do not present two groups of 4 icons to the user. The limit is 4 for a reason. Any more than that requires a UI re-think. Perhaps divide the functionality over a few pages?



回答3:

The syntax above gave me a compile error. With some additional research, I got this to work for me:

ApplicationBar.Buttons.Remove((ApplicationBarIconButton) ApplicationBar.Buttons[0]);