Xamarin Forms Prism: Is INavigationService necessary to be passed in constructor? Any other way apart from...












0















For prism framework in xamarin forms, to navigate from one view to another, is it mandatory to implement constructor inject and pass INavigationService in ViewModel's constructor?



I Can perform Navigation when I pass INavigationService as



public SomeViewModel(INavigationService navigationService)
{
_navigationService.NavigateAsync("SomeOtherPage");
}


But when I try to resolve whenever its needed, it doesn't work



public SomeViewModel(INavigationService navigationService)
{
ContainerProvider.Resolve<T>();// ContainerProvider is IContainerProvider
}


Is there any other way to access INavigationService apart from constructor injection in every Viewmodel










share|improve this question























  • Are you facing any error while using NavigationService with constructor injection? Also, you don't have to resolve NavigationService manually, it will be resolved via constructor injection.

    – Dishant
    Nov 14 '18 at 5:10
















0















For prism framework in xamarin forms, to navigate from one view to another, is it mandatory to implement constructor inject and pass INavigationService in ViewModel's constructor?



I Can perform Navigation when I pass INavigationService as



public SomeViewModel(INavigationService navigationService)
{
_navigationService.NavigateAsync("SomeOtherPage");
}


But when I try to resolve whenever its needed, it doesn't work



public SomeViewModel(INavigationService navigationService)
{
ContainerProvider.Resolve<T>();// ContainerProvider is IContainerProvider
}


Is there any other way to access INavigationService apart from constructor injection in every Viewmodel










share|improve this question























  • Are you facing any error while using NavigationService with constructor injection? Also, you don't have to resolve NavigationService manually, it will be resolved via constructor injection.

    – Dishant
    Nov 14 '18 at 5:10














0












0








0








For prism framework in xamarin forms, to navigate from one view to another, is it mandatory to implement constructor inject and pass INavigationService in ViewModel's constructor?



I Can perform Navigation when I pass INavigationService as



public SomeViewModel(INavigationService navigationService)
{
_navigationService.NavigateAsync("SomeOtherPage");
}


But when I try to resolve whenever its needed, it doesn't work



public SomeViewModel(INavigationService navigationService)
{
ContainerProvider.Resolve<T>();// ContainerProvider is IContainerProvider
}


Is there any other way to access INavigationService apart from constructor injection in every Viewmodel










share|improve this question














For prism framework in xamarin forms, to navigate from one view to another, is it mandatory to implement constructor inject and pass INavigationService in ViewModel's constructor?



I Can perform Navigation when I pass INavigationService as



public SomeViewModel(INavigationService navigationService)
{
_navigationService.NavigateAsync("SomeOtherPage");
}


But when I try to resolve whenever its needed, it doesn't work



public SomeViewModel(INavigationService navigationService)
{
ContainerProvider.Resolve<T>();// ContainerProvider is IContainerProvider
}


Is there any other way to access INavigationService apart from constructor injection in every Viewmodel







xamarin xamarin.forms prism






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 1:58









TheDeveloperTheDeveloper

378524




378524













  • Are you facing any error while using NavigationService with constructor injection? Also, you don't have to resolve NavigationService manually, it will be resolved via constructor injection.

    – Dishant
    Nov 14 '18 at 5:10



















  • Are you facing any error while using NavigationService with constructor injection? Also, you don't have to resolve NavigationService manually, it will be resolved via constructor injection.

    – Dishant
    Nov 14 '18 at 5:10

















Are you facing any error while using NavigationService with constructor injection? Also, you don't have to resolve NavigationService manually, it will be resolved via constructor injection.

– Dishant
Nov 14 '18 at 5:10





Are you facing any error while using NavigationService with constructor injection? Also, you don't have to resolve NavigationService manually, it will be resolved via constructor injection.

– Dishant
Nov 14 '18 at 5:10












1 Answer
1






active

oldest

votes


















1














What you’re describing is an anti pattern, and generally just poor design. It also will not work because the Navigation Service is a very special service. It is created especially for each ViewModel as the Navigation Service must have the context of the Page you are navigating from. Without it, it would only work for resetting the Navigation Stack. To attempt to resolve the Navigation Service any other way in the ViewModel would likely break the MVVM design pattern.



If you want to use the Navigation Service you must inject it via the constructor. You may also simply use XAML Navigation in Prism 7.1. You can see a sample with the Prism Tests. You can also see this in practice in this demo app.



<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xaml="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"
Title="{Binding Title}"
x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMockA">
<Button x:Name="testButton" Command="{xaml:NavigateTo 'XamlViewMockB'}">
<Button.CommandParameter>
<xaml:NavigationParameters>
<xaml:NavigationParameter Key="Foo" Value="Bar"/>
</xaml:NavigationParameters>
</Button.CommandParameter>
</Button>
</ContentPage>





share|improve this answer


























  • Dan, thank you for the explanation, that helps a lot. I used to resolvers when needed instead of constructor injextion in MvvmCross, so thought there could be something similar. can you share example/Syntax of Xaml navigation?

    – TheDeveloper
    Nov 14 '18 at 14:06








  • 1





    I've added some samples of XAML based Navigation. Note that this doesn't make sense 100% of the time, for certain use cases it can work great. Also be sure to read the docs for a complete understanding of how to use it, there is actually quite a bit of functionality built in, including with determining when you can or cannot navigate.

    – Dan S.
    Nov 14 '18 at 18:11











  • Thanks Dan, I checked your example.

    – TheDeveloper
    Nov 15 '18 at 16:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292119%2fxamarin-forms-prism-is-inavigationservice-necessary-to-be-passed-in-constructor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














What you’re describing is an anti pattern, and generally just poor design. It also will not work because the Navigation Service is a very special service. It is created especially for each ViewModel as the Navigation Service must have the context of the Page you are navigating from. Without it, it would only work for resetting the Navigation Stack. To attempt to resolve the Navigation Service any other way in the ViewModel would likely break the MVVM design pattern.



If you want to use the Navigation Service you must inject it via the constructor. You may also simply use XAML Navigation in Prism 7.1. You can see a sample with the Prism Tests. You can also see this in practice in this demo app.



<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xaml="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"
Title="{Binding Title}"
x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMockA">
<Button x:Name="testButton" Command="{xaml:NavigateTo 'XamlViewMockB'}">
<Button.CommandParameter>
<xaml:NavigationParameters>
<xaml:NavigationParameter Key="Foo" Value="Bar"/>
</xaml:NavigationParameters>
</Button.CommandParameter>
</Button>
</ContentPage>





share|improve this answer


























  • Dan, thank you for the explanation, that helps a lot. I used to resolvers when needed instead of constructor injextion in MvvmCross, so thought there could be something similar. can you share example/Syntax of Xaml navigation?

    – TheDeveloper
    Nov 14 '18 at 14:06








  • 1





    I've added some samples of XAML based Navigation. Note that this doesn't make sense 100% of the time, for certain use cases it can work great. Also be sure to read the docs for a complete understanding of how to use it, there is actually quite a bit of functionality built in, including with determining when you can or cannot navigate.

    – Dan S.
    Nov 14 '18 at 18:11











  • Thanks Dan, I checked your example.

    – TheDeveloper
    Nov 15 '18 at 16:00
















1














What you’re describing is an anti pattern, and generally just poor design. It also will not work because the Navigation Service is a very special service. It is created especially for each ViewModel as the Navigation Service must have the context of the Page you are navigating from. Without it, it would only work for resetting the Navigation Stack. To attempt to resolve the Navigation Service any other way in the ViewModel would likely break the MVVM design pattern.



If you want to use the Navigation Service you must inject it via the constructor. You may also simply use XAML Navigation in Prism 7.1. You can see a sample with the Prism Tests. You can also see this in practice in this demo app.



<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xaml="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"
Title="{Binding Title}"
x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMockA">
<Button x:Name="testButton" Command="{xaml:NavigateTo 'XamlViewMockB'}">
<Button.CommandParameter>
<xaml:NavigationParameters>
<xaml:NavigationParameter Key="Foo" Value="Bar"/>
</xaml:NavigationParameters>
</Button.CommandParameter>
</Button>
</ContentPage>





share|improve this answer


























  • Dan, thank you for the explanation, that helps a lot. I used to resolvers when needed instead of constructor injextion in MvvmCross, so thought there could be something similar. can you share example/Syntax of Xaml navigation?

    – TheDeveloper
    Nov 14 '18 at 14:06








  • 1





    I've added some samples of XAML based Navigation. Note that this doesn't make sense 100% of the time, for certain use cases it can work great. Also be sure to read the docs for a complete understanding of how to use it, there is actually quite a bit of functionality built in, including with determining when you can or cannot navigate.

    – Dan S.
    Nov 14 '18 at 18:11











  • Thanks Dan, I checked your example.

    – TheDeveloper
    Nov 15 '18 at 16:00














1












1








1







What you’re describing is an anti pattern, and generally just poor design. It also will not work because the Navigation Service is a very special service. It is created especially for each ViewModel as the Navigation Service must have the context of the Page you are navigating from. Without it, it would only work for resetting the Navigation Stack. To attempt to resolve the Navigation Service any other way in the ViewModel would likely break the MVVM design pattern.



If you want to use the Navigation Service you must inject it via the constructor. You may also simply use XAML Navigation in Prism 7.1. You can see a sample with the Prism Tests. You can also see this in practice in this demo app.



<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xaml="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"
Title="{Binding Title}"
x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMockA">
<Button x:Name="testButton" Command="{xaml:NavigateTo 'XamlViewMockB'}">
<Button.CommandParameter>
<xaml:NavigationParameters>
<xaml:NavigationParameter Key="Foo" Value="Bar"/>
</xaml:NavigationParameters>
</Button.CommandParameter>
</Button>
</ContentPage>





share|improve this answer















What you’re describing is an anti pattern, and generally just poor design. It also will not work because the Navigation Service is a very special service. It is created especially for each ViewModel as the Navigation Service must have the context of the Page you are navigating from. Without it, it would only work for resetting the Navigation Stack. To attempt to resolve the Navigation Service any other way in the ViewModel would likely break the MVVM design pattern.



If you want to use the Navigation Service you must inject it via the constructor. You may also simply use XAML Navigation in Prism 7.1. You can see a sample with the Prism Tests. You can also see this in practice in this demo app.



<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xaml="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"
Title="{Binding Title}"
x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMockA">
<Button x:Name="testButton" Command="{xaml:NavigateTo 'XamlViewMockB'}">
<Button.CommandParameter>
<xaml:NavigationParameters>
<xaml:NavigationParameter Key="Foo" Value="Bar"/>
</xaml:NavigationParameters>
</Button.CommandParameter>
</Button>
</ContentPage>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 18:08

























answered Nov 14 '18 at 8:22









Dan S.Dan S.

3,0262820




3,0262820













  • Dan, thank you for the explanation, that helps a lot. I used to resolvers when needed instead of constructor injextion in MvvmCross, so thought there could be something similar. can you share example/Syntax of Xaml navigation?

    – TheDeveloper
    Nov 14 '18 at 14:06








  • 1





    I've added some samples of XAML based Navigation. Note that this doesn't make sense 100% of the time, for certain use cases it can work great. Also be sure to read the docs for a complete understanding of how to use it, there is actually quite a bit of functionality built in, including with determining when you can or cannot navigate.

    – Dan S.
    Nov 14 '18 at 18:11











  • Thanks Dan, I checked your example.

    – TheDeveloper
    Nov 15 '18 at 16:00



















  • Dan, thank you for the explanation, that helps a lot. I used to resolvers when needed instead of constructor injextion in MvvmCross, so thought there could be something similar. can you share example/Syntax of Xaml navigation?

    – TheDeveloper
    Nov 14 '18 at 14:06








  • 1





    I've added some samples of XAML based Navigation. Note that this doesn't make sense 100% of the time, for certain use cases it can work great. Also be sure to read the docs for a complete understanding of how to use it, there is actually quite a bit of functionality built in, including with determining when you can or cannot navigate.

    – Dan S.
    Nov 14 '18 at 18:11











  • Thanks Dan, I checked your example.

    – TheDeveloper
    Nov 15 '18 at 16:00

















Dan, thank you for the explanation, that helps a lot. I used to resolvers when needed instead of constructor injextion in MvvmCross, so thought there could be something similar. can you share example/Syntax of Xaml navigation?

– TheDeveloper
Nov 14 '18 at 14:06







Dan, thank you for the explanation, that helps a lot. I used to resolvers when needed instead of constructor injextion in MvvmCross, so thought there could be something similar. can you share example/Syntax of Xaml navigation?

– TheDeveloper
Nov 14 '18 at 14:06






1




1





I've added some samples of XAML based Navigation. Note that this doesn't make sense 100% of the time, for certain use cases it can work great. Also be sure to read the docs for a complete understanding of how to use it, there is actually quite a bit of functionality built in, including with determining when you can or cannot navigate.

– Dan S.
Nov 14 '18 at 18:11





I've added some samples of XAML based Navigation. Note that this doesn't make sense 100% of the time, for certain use cases it can work great. Also be sure to read the docs for a complete understanding of how to use it, there is actually quite a bit of functionality built in, including with determining when you can or cannot navigate.

– Dan S.
Nov 14 '18 at 18:11













Thanks Dan, I checked your example.

– TheDeveloper
Nov 15 '18 at 16:00





Thanks Dan, I checked your example.

– TheDeveloper
Nov 15 '18 at 16:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292119%2fxamarin-forms-prism-is-inavigationservice-necessary-to-be-passed-in-constructor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python