为所有Xaml页面创建相同的应用程序栏

问题描述:

您好

我正在使用Windows phone 8应用程序。

I am working on Windows phone 8 app.

我有应用程序栏,其中有不同的应用程序栏菜单项登录的人(包含注销,更改密码等选项) 或不...如果有人没有登录,那么它有登录,在app栏上注册选项....

I have application bar which have different app bar menu items depending on person logged in (contains logout, change password etc options)  or not ... and if person is not logged in then it have login ,register options on app bar....

 我可以使用以下行在.xaml.cs文件中的应用栏上动态添加菜单项

 I am able to add menu items dynamically on app bar in .xaml.cs file using following lines

但我没有得到如何概括这一点,因为我想在每个xaml页面上使用相同的功能。 ..如果我正在制作不同的.cs文件,那么我无法访问。 

but I am not getting how to generalize this as i want same functionality on every xaml page ... If I am making differnt .cs file then I am not able to access. 

  ApplicationBar.MenuItems.Add(login);

 ApplicationBar.MenuItems.Add(login);

它给出错误:  错误
4
非静态字段,方法或属性'Microsoft.Phone.Shell.ApplicationBar.MenuItems.get'

It gives error: Error 4 An object reference is required for the non-static field, method, or property 'Microsoft.Phone.Shell.ApplicationBar.MenuItems.get'

一个非常糟糕的解决方案是在每个页面上做同样的事情,请有人帮助我。

One very bad solution is to do same thing on every page , Please someone  help me .

谢谢

...

我想创建一个Helper方法来创建ApplicationBarMenu Item并从需要应用程序栏的所有页面调用它。

I would say create a Helper method to create ApplicationBarMenu Item and call it from all the pages where application bar is required.

检查这篇文章:
http://dotnetspeaks.com/DisplayArticle.aspx?ID=272