2011-12-10 188 views
0

我正在开发Windows Phone 7.1应用程序。相对或绝对路径混乱

我有以下文件夹结构:

- Images (**folder**) 
    | 
    - appbar.questionmark.rest.png 

- Views(**folder**) 
    | 
    - About.xaml 

- MainPage.xaml 

...

我试图用编程的方式创建一个应用栏:

private void SetUpAppBar() 
    { 
     // Set the page's ApplicationBar to a new instance of ApplicationBar. 
     ApplicationBar = new ApplicationBar(); 

     // Create a new button and set the text value to the localized string from AppResources. 
     ApplicationBarIconButton helpButton = new ApplicationBarIconButton(new Uri("..//Images//appbar.questionmark.rest.png", UriKind.Relative)); 
     helpButton.Text = AppResources.Help; 
     helpButton.Click += new EventHandler(helpButton_Click); 
     ApplicationBar.Buttons.Add(helpButton); 

     // Create a new menu item with the localized string from AppResources. 
     ApplicationBarMenuItem appBarHelpMenuItem = new ApplicationBarMenuItem(AppResources.Help); 
     appBarHelpMenuItem.Click += new EventHandler(helpButton_Click); 
     ApplicationBar.MenuItems.Add(appBarHelpMenuItem); 
    } 

但我不能看应用栏上的图标。我究竟做错了什么?

我有测试此:

ApplicationBarIconButton helpButton = new ApplicationBarIconButton(new Uri("..//Images//appbar.questionmark.rest.png", UriKind.Relative)) 

,但我得到一个无效的路径异常。我也改变了UriKind到相对,绝对和AbsoluteOrRelative。

appbar.questionmark.rest.png被标记为资源,并复制到目录设置为“不复制”。

+0

你可以在开始时用单点试试这条线。 ApplicationBarIconButton helpButton = new ApplicationBarIconButton(new Uri(“..// Images // appbar.questionmark.rest.png”,UriKind.Relative));你应该看看你的PNG的属性 – BigL

+0

对不起,但它不工作。 – VansFannel

+0

对不起,我知道我的第一条评论不起作用,只是从你的代码中复制出来,然后很快输入,我编辑它。 – BigL

回答

0

图片构建动作应该是内容。

ApplicationBarIconButton helpButton = new ApplicationBarIconButton(new Uri("/Images/appbar.questionmark.rest.png", UriKind.Relative)); 

试试这个,我希望这会工作。:)

我这是怎么MSDN describes how to add a button to the AppBar

ApplicationBarIconButton button1 = new ApplicationBarIconButton(); 
button1.IconUri = new Uri("/Images/YourImage.png", UriKind.Relative); 
button1.Text = "button 1"; 
ApplicationBar.Buttons.Add(button1); 

认为你的问题是,你的图像设定为资源而不是内容。

+0

不,它不工作。 – VansFannel

+0

现在它工作? :) – BigL

+0

它的作品,因为我设置图片作为内容。非常感谢! – VansFannel