Search code examples
visual-studiosilverlightwindows-phone-8

Can't navigate to file in same folder using relative path?


I am absolutely confounded by this seemingly simple error. I can't navigate to a file in the same folder using this code

NavigationService.Navigate(new Uri("Rotate.xaml", UriKind.Relative));

I also tried

NavigationService.Navigate(new Uri("/Rotate.xaml", UriKind.Relative));

However when I move the file I'm working on to the main project folder and then use this navigation code

NavigationService.Navigate(new Uri("/Pages/Rotate.xaml", UriKind.Relative));

it works! Why can't I navigate to the file when I am in the same folder but I can when I'm in the main project folder?

solution explorer

To give you a better idea here is the view of my solution explorer. This view corresponds to the first two situations. In the third situation I have the PictureSelect.xaml file outside of the Pages folder and try to navigate to the Rotate.Xaml file.


Solution

  • I can't navigate to a file in the same folder using this code

    The path that is being used by the navigator has nothing to do with a current directory. There is no such concept in Silverlight like there is in a windows app.

    The path is used by the Navigator to find a resource file in the xap. Thus you always have to use the full path, ie `/Pages/Rotate.xaml' using the leading forward slash tells SL to start looking at the root.

    Also, the setting of BuildAction also affects where the file is going to be placed at compile time.

    To get a better concept of this, unzip the xap folder and you can see the actual location of items and where it is looking.

    There are times when using relative location seems to work, but it's very easy to break and I avoid using it.

    Greg