简体   繁体   English

更改应用程序栏中的按钮?

[英]Changing the buttons in the Applicationbar?

I want to change the icons and what they do in code in my WP7 application. 我想更改图标及其在WP7应用程序中的代码功能。 I've tried in the MainPage constructor and in the MainPage_Loaded but it says ApplicationBar ( x:Name ) is null all the time. 我试着在MainPage构造函数和在MainPage_Loaded但它说应用程序任务栏( x:Name )为空所有的时间。

How can I change it since I use same page and different states? 由于我使用相同的页面和不同的状态,该如何更改?

尝试删除x:Name属性,然后使用Page.ApplicationBar对其进行访问

Unfortunately the application bar buttons are not accessible via code at the moment. 不幸的是,目前无法通过代码访问应用程序栏按钮。 I followed one of the examples of the official Mircosoft Training Kit for WP7 to accomplish that task. 我按照WP7官方Mircosoft培训套件的示例之一来完成该任务。 Here is the XAML and some code: 这是XAML和一些代码:

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar>
        <shell:ApplicationBarIconButton IconUri="/icons/appbar.pin.png" IsEnabled="True" Text="Anpinnen" x:Name="appPinPage" Click="appPinPage_Click" />
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

Please note that ApplicationBar is a property of PhoneApplicationPage so that you do not have to give an explicit name to the ApplicationBar object that you assign to the ApplicationBar property of the PhoneApplicationPage. 请注意,ApplicationBar是PhoneApplicationPage的属性,因此您不必为分配给PhoneApplicationPage的ApplicationBar属性的ApplicationBar对象提供明确的名称。 Here is an example of changing the picture of the button in the xaml code above. 这是在上面的xaml代码中更改按钮图片的示例。 That code is called in the overridden OnNavigatedTo() method of the PhoneApplicationPage. 该代码在PhoneApplicationPage的重写的OnNavigatedTo()方法中调用。

        if (this.ViewModel.IsPinned())
        {
            ((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).Text = Resource1.txtUnpin;
            ((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).IconUri = new Uri("/icons/appbar.unpin.png", UriKind.Relative);
        }
        else
        {
            ((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).Text = Resource1.txtPin;
            ((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).IconUri = new Uri("/icons/appbar.pin.png", UriKind.Relative);
        }

Unfortunately, the ApplicationBar isn't a Silverlight control (doesn't inherit from UIElement ); 不幸的是, ApplicationBar不是Silverlight控件(不继承自UIElement )。 what that means is that it can't be accessed with a x:Name neither the buttons inside it, but you can access it inside the page using the ApplicationBar property! 这意味着不能使用x:Name来访问它,也不能使用其中的按钮来访问它,但是您可以使用ApplicationBar属性在页面内对其进行访问!

Check this sample to see how you can create and access the ApplicationBar from the code behind of a page. 检查此示例以查看如何从页面后面的代码创建和访问ApplicationBar。

If you are willing to go with an MVVM architecture, you should check the ApplicationBarBehaviour from the Cimbalino Windows Phone Toolkit , it will save you a lot of work!!! 如果您愿意使用MVVM架构,则应从Cimbalino Windows Phone Toolkit中检查ApplicationBarBehaviour,它将为您节省很多工作!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM