Windows phone 7 获取AppBar按钮的实例

Windows phone 7 获取AppBar按钮的实例,windows-phone-7,xaml,windows-phone-8,windows-phone,Windows Phone 7,Xaml,Windows Phone 8,Windows Phone,在我的xaml中,我在参考资料中有两个不同的AppBarrs: <phone:PhoneApplicationPage.Resources> <shell:ApplicationBar x:Key="appbar1" IsVisible="True"> <shell:ApplicationBarIconButton x:Name="abMain1" IconUri="/icons/appbar.radeos.png" Text="rade

在我的xaml中,我在参考资料中有两个不同的
AppBarr
s:

 <phone:PhoneApplicationPage.Resources>

    <shell:ApplicationBar x:Key="appbar1" IsVisible="True">
        <shell:ApplicationBarIconButton x:Name="abMain1" IconUri="/icons/appbar.radeos.png" Text="radeos"
                                        Click="abMain_Click" />
    </shell:ApplicationBar>

    <shell:ApplicationBar x:Key="appbar2" IsVisible="True">
        <shell:ApplicationBarIconButton x:Name="abMain" IconUri="/icons/appbar.radeos.png" Text="radeos"
                                        Click="abMain_Click" />
        <shell:ApplicationBarIconButton x:Name="abLast" IconUri="/icons/appbar.back.rest.png" Text="last"
                                        Click="abLast_Click" />
        <shell:ApplicationBarIconButton x:Name="abNext" IconUri="/icons/appbar.next.rest.png" Text="next"
                                        Click="abNext_Click" />
        <shell:ApplicationBarIconButton x:Name="abClosePopup" IconUri="/icons/appbar.cancel.rest.png" Text="close"
                                        Click="abClosePopup_Click" />
    </shell:ApplicationBar>

</phone:PhoneApplicationPage.Resources>


问题是:如何在代码中的任何位置获取AppBarr按钮(例如
abMain1
)的实例
Visual Studio可以看到此字段,但它总是
null
(因为它位于参考资料中)。

您应该可以这样访问

ApplicationBarIconButton abMain1Button = (ApplicationBarIconButton) ApplicationBar.Buttons[0];

谢谢你的回答,但我刚刚解决了我的问题。我知道你的答案ApplicationBar.Buttons[0]-它是一个需要强制转换到ApplicationBarIconButton的对象。我使用这种结构:var abMain1Button=(ApplicationBarIconButton)ApplicationBar.Buttons[0];
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar) Resources["appbar2"];
ApplicationBarIconButton abMain1Button = (ApplicationBarIconButton) ApplicationBar.Buttons[0];