5

Hey Hope somebody can help.

I have in group developed on many different projects and we now want to combine these. I therefore include the projects. In the startup project I include the reference to the other projects. And then I use the code from another stackoverflow thread:

How to navigate to view from another project

Which gives me the following

    NavigationService.Navigate(new Uri("/MVVMTestApp;/component/View/MainPage.xaml", UriKind.Relative));

The project builds and runs. But when I press the button that should activate the code I get an error after/during the execution of the line.

The break error is located in the MainPage.g.cs file which looks like:

namespace MVVMTestApp {


public partial class MainPage : Microsoft.Phone.Controls.PhoneApplicationPage {

    internal System.Windows.Controls.TextBlock MainPageTitle;

    internal System.Windows.Controls.TextBlock LEgE;

    private bool _contentLoaded;

    /// <summary>
    /// InitializeComponent
    /// </summary>
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public void InitializeComponent() {
        if (_contentLoaded) {
            return;
        }
        _contentLoaded = true;
        *******System.Windows.Application.LoadComponent(this, new System.Uri("/MVVMTestApp;component/View/MainPage.xaml", System.UriKind.Relative));**
        this.MainPageTitle = ((System.Windows.Controls.TextBlock)(this.FindName("MainPageTitle")));
        this.LEgE = ((System.Windows.Controls.TextBlock)(this.FindName("LEgE")));
    }
}
}

The line where I inserted the stars, is where it breaks. What do I need to do to make it work?

Extra

As told in the comments, I need to include a forwardslash "/" before component, which is odd since it is not used in any place, else like the link above and this link

http://www.geekchamp.com/tips/wp7-navigating-to-a-page-in-different-assembly

But including the forwardslash I get an error that there does not exist any xaml file where I point. Excluding the forwardslash and I get a XamlParseException occured....

So I still have the problem of navigating to a view in another project.

ODD

I now do not have to have the leading forwardslash before component. As long as I write MVVMTestAppAssembly. I have looked in the assembly files and cannot find this included =?

I tried it out after looking at msdn http://msdn.microsoft.com/en-us/library/cc296240%28v=vs.95%29.aspx

But still no luck, with getting the navigation to work

OVerview of reference and included the full path to the elements enter image description here

enter image description here

One Solution As stated in one answer one solution is to use the windows phone class library. But when I use MVVM structure, I cannot get the viewmodelLocator to work. Therefore, this is not a solution for me.

But if you use MVVM, I need another solution. Hopefully somebody has an idea for this. Another solution As stated below in an answer you can use the following Solution 1:

You use the same type of projects "Windows Phone App". In the solution you need to have one Windows Phone App project and other projects should be of type Windows Phone Class Library.

Then you can navigate to view in another project inside same solution with this line of code:

    NavigationService.Navigate(new Uri("/MVVMTestApp;/component/View/MainPage.xaml", UriKind.Relative));

To navigate between projects. However you get an error in the generated xaml file FILENAME.g.cs where it inserts the line loadcomponent. The error occurs because of the view being connected by datacontext to a viewmodel, as far as I can understand. And I have not been able to solve it.

Hopfully somebody has a solution for this?

Community
  • 1
  • 1
JTIM
  • 2,774
  • 1
  • 34
  • 74
  • add / before component – techloverr Dec 06 '13 at 09:22
  • @techloverr thank you, that resolved the error, but now I get another error in RootFrame_NavigationFailed. "No XAML found...". My startup Project is TestUserControls and the project I want to go to is MVVMTestApp, and the path I showed above. Any Idea for this error? And this error is caught in the app.xaml.cs file of the project I want to go from? – JTIM Dec 06 '13 at 09:48
  • which is your startup page – techloverr Dec 06 '13 at 10:05
  • @techloverr I start in project TestUserControl. Page1: FirstPage, navigates to page2: AnimationPage, where I do some start up animation, page3: Menu overview. In menu I click a button and use the navigationservice to go to the new project, MVVMTestApp. And the new error occurs. – JTIM Dec 06 '13 at 10:07
  • in MVVMTestApp, where is the view located? – techloverr Dec 06 '13 at 10:08
  • inside View folder. Path from properties is MVVMTestApp\View\MainPage.xaml. And MVVMTestApp folder is on the same "height" in the folder structure as the solution and the other projects. – JTIM Dec 06 '13 at 10:09

4 Answers4

3

Just remove "/" before "component", like this:

NavigationService.Navigate(new Uri("/MVVMTestApp;component/View/MainPage.xaml", UriKind.Relative));

Rokman
  • 46
  • 3
2

First you have to give reference to the Parent Project then

you can solve that issue via this line

  NavigationService.Navigate(new
 Uri("/MVVMTestApp;/View/MainPage.xaml", UriKind.Relative));
techloverr
  • 2,597
  • 1
  • 16
  • 28
  • I am trying it right now, and it gives the same error of no XAML found at the location. – JTIM Dec 06 '13 at 11:13
  • What do you mean by parent project? I would like to not have to change the library as the other solution states – JTIM Dec 06 '13 at 14:34
1

Solution 1:

You use the same type of projects "Windows Phone App". In the solution you need to have one Windows Phone App project and other projects should be of type Windows Phone Class Library.

Then you can navigate to view in another project inside same solution with this line of code:

 NavigationService.Navigate(new Uri("/MVVMTestApp;/component/View/MainPage.xaml", UriKind.Relative));

Solution 2:

Go to Configuration Manager in Debug.

Configuration Manager

Uncheck Deploy check-box from MVVMTestApp.

Configuration Manager

ROMAN
  • 1,476
  • 2
  • 11
  • 24
  • I cannot reach that name from the other project? – JTIM Dec 06 '13 at 10:17
  • @JTIM You need to give a name to the view where you try to navigate from in xaml. – ROMAN Dec 06 '13 at 10:21
  • Still does not help, and I need to include a forward slash before component as to not get an XamlParseException. – JTIM Dec 06 '13 at 10:34
  • @Roman That there is no xaml file at the location. Which is odd. – JTIM Dec 06 '13 at 10:56
  • @JTIM Can you make a screenshot of your solution structure? – ROMAN Dec 06 '13 at 10:59
  • @JTIM Okay. Did you add the reference "MVVMTestApp" to TestUserControl. – ROMAN Dec 06 '13 at 11:16
  • @Roman I will include another image with that info. So you can see that. – JTIM Dec 06 '13 at 11:21
  • @Roman No it is a page, or more precisely phone:PhoneApplicationPage – JTIM Dec 06 '13 at 11:46
  • @JTIM What kind of project is TestUserControls and MVVMTestApp? – ROMAN Dec 06 '13 at 11:53
  • @Roman They are both Windows phone application. TestUserControl, is build in xaml, and the code behind (created by designer). and MVVMTestApp is build using the MVVM pattern (created by programmer). – JTIM Dec 06 '13 at 11:55
  • @JTIM Add a test project of type "Windows Phone Class Library", then add MainPage.xaml and try to navigate to the view, don't forget to add the reference. – ROMAN Dec 06 '13 at 12:01
  • it is not compulsary to give a name to view – techloverr Dec 06 '13 at 12:06
  • I have now added a project called PhoneClassLibrary1, and added an phone application page called Page1. with no name to the view. Then using this line NavigationService.Navigate(new Uri("/PhoneClassLibrary1;component/Page1.xaml", UriKind.Relative)); I can navigate to the page. Seems like you need it to be in this class Library form? – JTIM Dec 06 '13 at 12:09
  • @JTIM What is the problem with ViewModelLocator? – ROMAN Dec 06 '13 at 16:29
  • @Roman I tried to convert everything to this class Library you suggested but I got a lot of errors upon building. After a couple of hours trying, I ended upp stopping because I could not get it working. Is there a way to include to windows phone apps and navigate between them? – JTIM Dec 06 '13 at 16:52
  • @JTIM I cannot post any images, so i need explain it conversational. Go to Debug --> Configuration Manager. Then you need to uncheck the Deploy check-box from MVVMTestApp and it should works fine! – ROMAN Dec 06 '13 at 19:48
  • @Roman I did this in the start, to not have to installed projects. So sorry it does work as installing the project hidden. But not for the navigation. If you have a demo solution with two projects inside, one build in MVVM structure, and one without. I would love to see it working. – JTIM Dec 07 '13 at 20:16
  • @Roman could you share a working project just an example, on dropbox or somewhere so I can look, at your solution Because I cannot get it to work. – JTIM Dec 09 '13 at 13:57
  • @JTIM Do you use MVVM Light? – ROMAN Dec 09 '13 at 13:59
  • @Roman I have one project where I do not where I want to navigate to project that uses galasoft MVVM light – JTIM Dec 09 '13 at 14:01
  • @JTIM I will try to make for you an example at night. I'm at work now. :) – ROMAN Dec 09 '13 at 14:22
  • @Roman sorry, for pushing you. But did you have time to look at the problem ? – JTIM Dec 10 '13 at 17:22
  • @JTIM what kind of exception are you getting now? – ROMAN Dec 10 '13 at 18:17
  • @JTIM [Navigation between 2 windows phone 8 projects](https://skydrive.live.com/redir?resid=5DC4489B19F3B357!21191&authkey=!AJAkl17qbuHnY2k&ithint=file%2c.zip) – ROMAN Dec 10 '13 at 18:19
  • @Roman Thank you again. This works as long as you do not set the datacontext, in the view you want to navigate to. If I set the datacontext I get an error about the component in g.cs file. If I on the other hand first navigate to a page in the MVVM project with no datacontext, it works. From here I then try to navigate to a page that has datacontext set to the mainviewmodel, and I get an error regarding no xaml found. :S quite annoying did you stumble across this issue. – JTIM Dec 11 '13 at 11:30
  • @JTIM Show me the exception – ROMAN Dec 11 '13 at 13:36
  • @Roman It is the same errors as before. I have updated your example https://skydrive.live.com/redir?resid=44CFE57AFAEAD786%211671 so you can see the problems. – JTIM Dec 11 '13 at 15:16
  • @Roman I get the same error as the first I specified, in load component, if you rather wanted the error :) – JTIM Dec 12 '13 at 16:24
  • @Roman did you have time to look at it? I cannot get it to work. – JTIM Dec 15 '13 at 10:20
  • @JTIM Hi! I really don't know the solution of your problem. I spend some time trying to get it work, but without any result. Sorry. – ROMAN Dec 15 '13 at 10:25
  • @Roman Thank you so mmuch for trying, just wanted to know :) Hopefully I will find it, myself :) – JTIM Dec 15 '13 at 10:26
0

I think the issue lies in your setup. From your description you name your project MVVMTestApp. I assume this is the start up project. However, your reference is from TestUserControls to MVVMTestApp. This is backwards. I know based on what you are trying to do it seems right, but it is not (unless your project naming is messed up.)

I suggest you have two projects:

  • MVVMTestApp (Windows Phone 8.1 Universal App which is start up project)
  • MVVMTestApp.Lib (Portable Class that supports Windows Phone 8.1)

Add a reference from MVVMTestApp to MVVMTestApp.Lib.

MVVMTestApp will have MainPage.xaml & SecondPage.xaml.

I suggest that your Lib only has Shared User Controls and not Pages to be navigated to. You can put the user control on SecondPage.

If you do want to have the SecondPage in your Shared Lib, it will be tougher to handle but still possible. However you won't be able to use the code everyone else has suggested. It doesn't make sense to have the Shared code call MVVMTestApp because then it is not really shareable. What if you had another app that you were sharing with called MVVMTestApp2. Your could would not work if you had MVVMTestApp hard coded even it you could make it work with MVVMTestApp and therefore you would not have any benefit of putting SecondPage in a separate project.

To have a shareable page that calls back to the app I'm thinking you have two options. You could use Inversion of Control and define INavigationService in your Lib. Then in your MVVMTestApp implement INavigateService and register your implementation with your favorite Ioc container. Then in your Lib you can call a method on your INavigationService. In your MVVMTestApp implementation you can use the code provided in the other posts to navigate to main page.

Another option would be to expose an event in your Lib. You listen to the event from MVVMTestApp and in the handler you navigate to MainPage like in the other posts. In your Lib you would Raise the event when you want to navigate to MainPage.

Good luck & Have fun,

Tom

Thomas
  • 3,532
  • 3
  • 20
  • 22