How to navigate to another project inside same sol

2019-01-19 22:20发布

问题:

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

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?

回答1:

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

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



回答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));


回答3:

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.

Uncheck Deploy check-box from MVVMTestApp.



回答4:

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