UWP Prism ViewModel Is Null










0















Using the Visual Studio 2017 Universal Windows Templates I created a test UWP App using Prism. It all works fine untill I added a new blank page to the app. The view is called:



AbcPage


The XAML



<Page
x:Class="UI_Test_1.Views.AbcPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:UI_Test_1.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
Background="ThemeResource ApplicationPageBackgroundThemeBrush"
mc:Ignorable="d">

<Grid>
<Button Click="Button_Click" Content="test" />
</Grid>




I added the



 xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"


The code behind is thus:



 namespace UI_Test_1.Views

public sealed partial class AbcPage : Page

AbcPageViewModel viewModel => DataContext as AbcPageViewModel;
public AbcPage()

this.InitializeComponent();

private void Button_Click(object sender, RoutedEventArgs e)

var vm = viewModel;//this is null





And finally my ViewModel:



namespace UI_Test_1.ViewModels

public class AbcPageViewModel : ViewModelBase

public AbcPageViewModel()

//never called





The conventions appear to be correct or have I made a mistake? Why is the



 AbcViewModel


null? How do I debug this?










share|improve this question






















  • You need to add your AbcPageViewModel in ViewModels folder, that's where prism will try to search by default.

    – Dishant
    Nov 15 '18 at 2:19











  • The AbcPageViewModel is in the ViewModels folder.

    – Paul Stanley
    Nov 15 '18 at 6:18











  • Which prism version you that you used?

    – Nico Zhu - MSFT
    Nov 15 '18 at 6:24











  • Alas there is no code such as that in my project. App.xaml.cs has initialisation routines and you can configure containers for Ioc but nothing like the link.Thanks.

    – Paul Stanley
    Nov 15 '18 at 6:25











  • So, emm, the issue solved?

    – Nico Zhu - MSFT
    Nov 15 '18 at 6:27















0















Using the Visual Studio 2017 Universal Windows Templates I created a test UWP App using Prism. It all works fine untill I added a new blank page to the app. The view is called:



AbcPage


The XAML



<Page
x:Class="UI_Test_1.Views.AbcPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:UI_Test_1.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
Background="ThemeResource ApplicationPageBackgroundThemeBrush"
mc:Ignorable="d">

<Grid>
<Button Click="Button_Click" Content="test" />
</Grid>




I added the



 xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"


The code behind is thus:



 namespace UI_Test_1.Views

public sealed partial class AbcPage : Page

AbcPageViewModel viewModel => DataContext as AbcPageViewModel;
public AbcPage()

this.InitializeComponent();

private void Button_Click(object sender, RoutedEventArgs e)

var vm = viewModel;//this is null





And finally my ViewModel:



namespace UI_Test_1.ViewModels

public class AbcPageViewModel : ViewModelBase

public AbcPageViewModel()

//never called





The conventions appear to be correct or have I made a mistake? Why is the



 AbcViewModel


null? How do I debug this?










share|improve this question






















  • You need to add your AbcPageViewModel in ViewModels folder, that's where prism will try to search by default.

    – Dishant
    Nov 15 '18 at 2:19











  • The AbcPageViewModel is in the ViewModels folder.

    – Paul Stanley
    Nov 15 '18 at 6:18











  • Which prism version you that you used?

    – Nico Zhu - MSFT
    Nov 15 '18 at 6:24











  • Alas there is no code such as that in my project. App.xaml.cs has initialisation routines and you can configure containers for Ioc but nothing like the link.Thanks.

    – Paul Stanley
    Nov 15 '18 at 6:25











  • So, emm, the issue solved?

    – Nico Zhu - MSFT
    Nov 15 '18 at 6:27













0












0








0








Using the Visual Studio 2017 Universal Windows Templates I created a test UWP App using Prism. It all works fine untill I added a new blank page to the app. The view is called:



AbcPage


The XAML



<Page
x:Class="UI_Test_1.Views.AbcPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:UI_Test_1.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
Background="ThemeResource ApplicationPageBackgroundThemeBrush"
mc:Ignorable="d">

<Grid>
<Button Click="Button_Click" Content="test" />
</Grid>




I added the



 xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"


The code behind is thus:



 namespace UI_Test_1.Views

public sealed partial class AbcPage : Page

AbcPageViewModel viewModel => DataContext as AbcPageViewModel;
public AbcPage()

this.InitializeComponent();

private void Button_Click(object sender, RoutedEventArgs e)

var vm = viewModel;//this is null





And finally my ViewModel:



namespace UI_Test_1.ViewModels

public class AbcPageViewModel : ViewModelBase

public AbcPageViewModel()

//never called





The conventions appear to be correct or have I made a mistake? Why is the



 AbcViewModel


null? How do I debug this?










share|improve this question














Using the Visual Studio 2017 Universal Windows Templates I created a test UWP App using Prism. It all works fine untill I added a new blank page to the app. The view is called:



AbcPage


The XAML



<Page
x:Class="UI_Test_1.Views.AbcPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:UI_Test_1.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
Background="ThemeResource ApplicationPageBackgroundThemeBrush"
mc:Ignorable="d">

<Grid>
<Button Click="Button_Click" Content="test" />
</Grid>




I added the



 xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"


The code behind is thus:



 namespace UI_Test_1.Views

public sealed partial class AbcPage : Page

AbcPageViewModel viewModel => DataContext as AbcPageViewModel;
public AbcPage()

this.InitializeComponent();

private void Button_Click(object sender, RoutedEventArgs e)

var vm = viewModel;//this is null





And finally my ViewModel:



namespace UI_Test_1.ViewModels

public class AbcPageViewModel : ViewModelBase

public AbcPageViewModel()

//never called





The conventions appear to be correct or have I made a mistake? Why is the



 AbcViewModel


null? How do I debug this?







uwp prism






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 20:25









Paul StanleyPaul Stanley

642722




642722












  • You need to add your AbcPageViewModel in ViewModels folder, that's where prism will try to search by default.

    – Dishant
    Nov 15 '18 at 2:19











  • The AbcPageViewModel is in the ViewModels folder.

    – Paul Stanley
    Nov 15 '18 at 6:18











  • Which prism version you that you used?

    – Nico Zhu - MSFT
    Nov 15 '18 at 6:24











  • Alas there is no code such as that in my project. App.xaml.cs has initialisation routines and you can configure containers for Ioc but nothing like the link.Thanks.

    – Paul Stanley
    Nov 15 '18 at 6:25











  • So, emm, the issue solved?

    – Nico Zhu - MSFT
    Nov 15 '18 at 6:27

















  • You need to add your AbcPageViewModel in ViewModels folder, that's where prism will try to search by default.

    – Dishant
    Nov 15 '18 at 2:19











  • The AbcPageViewModel is in the ViewModels folder.

    – Paul Stanley
    Nov 15 '18 at 6:18











  • Which prism version you that you used?

    – Nico Zhu - MSFT
    Nov 15 '18 at 6:24











  • Alas there is no code such as that in my project. App.xaml.cs has initialisation routines and you can configure containers for Ioc but nothing like the link.Thanks.

    – Paul Stanley
    Nov 15 '18 at 6:25











  • So, emm, the issue solved?

    – Nico Zhu - MSFT
    Nov 15 '18 at 6:27
















You need to add your AbcPageViewModel in ViewModels folder, that's where prism will try to search by default.

– Dishant
Nov 15 '18 at 2:19





You need to add your AbcPageViewModel in ViewModels folder, that's where prism will try to search by default.

– Dishant
Nov 15 '18 at 2:19













The AbcPageViewModel is in the ViewModels folder.

– Paul Stanley
Nov 15 '18 at 6:18





The AbcPageViewModel is in the ViewModels folder.

– Paul Stanley
Nov 15 '18 at 6:18













Which prism version you that you used?

– Nico Zhu - MSFT
Nov 15 '18 at 6:24





Which prism version you that you used?

– Nico Zhu - MSFT
Nov 15 '18 at 6:24













Alas there is no code such as that in my project. App.xaml.cs has initialisation routines and you can configure containers for Ioc but nothing like the link.Thanks.

– Paul Stanley
Nov 15 '18 at 6:25





Alas there is no code such as that in my project. App.xaml.cs has initialisation routines and you can configure containers for Ioc but nothing like the link.Thanks.

– Paul Stanley
Nov 15 '18 at 6:25













So, emm, the issue solved?

– Nico Zhu - MSFT
Nov 15 '18 at 6:27





So, emm, the issue solved?

– Nico Zhu - MSFT
Nov 15 '18 at 6:27












2 Answers
2






active

oldest

votes


















1














For using prism early releases within uwp, you need config more ting base on native uwp project, such as App class and Page class. Certainly, the official has provide code sample you could refer.



public sealed partial class App : PrismUnityApplication

public App()

InitializeComponent();


protected override UIElement CreateShell(Frame rootFrame)

var shell = Container.Resolve<AppShell>();
shell.SetContentFrame(rootFrame);
return shell;


protected override Task OnInitializeAsync(IActivatedEventArgs args)

Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
return base.OnInitializeAsync(args);


protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)

NavigationService.Navigate(PageTokens.Main.ToString(), null);
return Task.FromResult(true);




For last 7.2 version that does not release has new usage mode. For more please check this link.



sealed partial class App : PrismApplication

public static IPlatformNavigationService NavigationService get; private set;

public App()

InitializeComponent();


public override void RegisterTypes(IContainerRegistry container)

container.RegisterForNavigation<MainPage, MainPageViewModel>(nameof(Views.MainPage));


public override void OnInitialized()

NavigationService = Prism.Navigation.NavigationService
.Create(new Frame(), Gestures.Back, Gestures.Forward, Gestures.Refresh);
NavigationService.SetAsWindowContent(Window.Current, true);


public override void OnStart(StartArgs args)

NavigationService.NavigateAsync(nameof(Views.MainPage));







share|improve this answer























  • Nico the nameing conventon was wrong. Thanks.

    – Paul Stanley
    Nov 15 '18 at 9:36






  • 1





    ok, get it, you could mark yourself.

    – Nico Zhu - MSFT
    Nov 15 '18 at 9:50


















1














I made a mistake with the naming conventions. If your page is AbcPage then the viewmodel should be AbcViewModel NOT AbcPageViewModel.






share|improve this answer






















    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%2f53308220%2fuwp-prism-viewmodel-is-null%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    For using prism early releases within uwp, you need config more ting base on native uwp project, such as App class and Page class. Certainly, the official has provide code sample you could refer.



    public sealed partial class App : PrismUnityApplication

    public App()

    InitializeComponent();


    protected override UIElement CreateShell(Frame rootFrame)

    var shell = Container.Resolve<AppShell>();
    shell.SetContentFrame(rootFrame);
    return shell;


    protected override Task OnInitializeAsync(IActivatedEventArgs args)

    Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
    return base.OnInitializeAsync(args);


    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)

    NavigationService.Navigate(PageTokens.Main.ToString(), null);
    return Task.FromResult(true);




    For last 7.2 version that does not release has new usage mode. For more please check this link.



    sealed partial class App : PrismApplication

    public static IPlatformNavigationService NavigationService get; private set;

    public App()

    InitializeComponent();


    public override void RegisterTypes(IContainerRegistry container)

    container.RegisterForNavigation<MainPage, MainPageViewModel>(nameof(Views.MainPage));


    public override void OnInitialized()

    NavigationService = Prism.Navigation.NavigationService
    .Create(new Frame(), Gestures.Back, Gestures.Forward, Gestures.Refresh);
    NavigationService.SetAsWindowContent(Window.Current, true);


    public override void OnStart(StartArgs args)

    NavigationService.NavigateAsync(nameof(Views.MainPage));







    share|improve this answer























    • Nico the nameing conventon was wrong. Thanks.

      – Paul Stanley
      Nov 15 '18 at 9:36






    • 1





      ok, get it, you could mark yourself.

      – Nico Zhu - MSFT
      Nov 15 '18 at 9:50















    1














    For using prism early releases within uwp, you need config more ting base on native uwp project, such as App class and Page class. Certainly, the official has provide code sample you could refer.



    public sealed partial class App : PrismUnityApplication

    public App()

    InitializeComponent();


    protected override UIElement CreateShell(Frame rootFrame)

    var shell = Container.Resolve<AppShell>();
    shell.SetContentFrame(rootFrame);
    return shell;


    protected override Task OnInitializeAsync(IActivatedEventArgs args)

    Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
    return base.OnInitializeAsync(args);


    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)

    NavigationService.Navigate(PageTokens.Main.ToString(), null);
    return Task.FromResult(true);




    For last 7.2 version that does not release has new usage mode. For more please check this link.



    sealed partial class App : PrismApplication

    public static IPlatformNavigationService NavigationService get; private set;

    public App()

    InitializeComponent();


    public override void RegisterTypes(IContainerRegistry container)

    container.RegisterForNavigation<MainPage, MainPageViewModel>(nameof(Views.MainPage));


    public override void OnInitialized()

    NavigationService = Prism.Navigation.NavigationService
    .Create(new Frame(), Gestures.Back, Gestures.Forward, Gestures.Refresh);
    NavigationService.SetAsWindowContent(Window.Current, true);


    public override void OnStart(StartArgs args)

    NavigationService.NavigateAsync(nameof(Views.MainPage));







    share|improve this answer























    • Nico the nameing conventon was wrong. Thanks.

      – Paul Stanley
      Nov 15 '18 at 9:36






    • 1





      ok, get it, you could mark yourself.

      – Nico Zhu - MSFT
      Nov 15 '18 at 9:50













    1












    1








    1







    For using prism early releases within uwp, you need config more ting base on native uwp project, such as App class and Page class. Certainly, the official has provide code sample you could refer.



    public sealed partial class App : PrismUnityApplication

    public App()

    InitializeComponent();


    protected override UIElement CreateShell(Frame rootFrame)

    var shell = Container.Resolve<AppShell>();
    shell.SetContentFrame(rootFrame);
    return shell;


    protected override Task OnInitializeAsync(IActivatedEventArgs args)

    Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
    return base.OnInitializeAsync(args);


    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)

    NavigationService.Navigate(PageTokens.Main.ToString(), null);
    return Task.FromResult(true);




    For last 7.2 version that does not release has new usage mode. For more please check this link.



    sealed partial class App : PrismApplication

    public static IPlatformNavigationService NavigationService get; private set;

    public App()

    InitializeComponent();


    public override void RegisterTypes(IContainerRegistry container)

    container.RegisterForNavigation<MainPage, MainPageViewModel>(nameof(Views.MainPage));


    public override void OnInitialized()

    NavigationService = Prism.Navigation.NavigationService
    .Create(new Frame(), Gestures.Back, Gestures.Forward, Gestures.Refresh);
    NavigationService.SetAsWindowContent(Window.Current, true);


    public override void OnStart(StartArgs args)

    NavigationService.NavigateAsync(nameof(Views.MainPage));







    share|improve this answer













    For using prism early releases within uwp, you need config more ting base on native uwp project, such as App class and Page class. Certainly, the official has provide code sample you could refer.



    public sealed partial class App : PrismUnityApplication

    public App()

    InitializeComponent();


    protected override UIElement CreateShell(Frame rootFrame)

    var shell = Container.Resolve<AppShell>();
    shell.SetContentFrame(rootFrame);
    return shell;


    protected override Task OnInitializeAsync(IActivatedEventArgs args)

    Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
    return base.OnInitializeAsync(args);


    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)

    NavigationService.Navigate(PageTokens.Main.ToString(), null);
    return Task.FromResult(true);




    For last 7.2 version that does not release has new usage mode. For more please check this link.



    sealed partial class App : PrismApplication

    public static IPlatformNavigationService NavigationService get; private set;

    public App()

    InitializeComponent();


    public override void RegisterTypes(IContainerRegistry container)

    container.RegisterForNavigation<MainPage, MainPageViewModel>(nameof(Views.MainPage));


    public override void OnInitialized()

    NavigationService = Prism.Navigation.NavigationService
    .Create(new Frame(), Gestures.Back, Gestures.Forward, Gestures.Refresh);
    NavigationService.SetAsWindowContent(Window.Current, true);


    public override void OnStart(StartArgs args)

    NavigationService.NavigateAsync(nameof(Views.MainPage));








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 6:48









    Nico Zhu - MSFTNico Zhu - MSFT

    10.3k1522




    10.3k1522












    • Nico the nameing conventon was wrong. Thanks.

      – Paul Stanley
      Nov 15 '18 at 9:36






    • 1





      ok, get it, you could mark yourself.

      – Nico Zhu - MSFT
      Nov 15 '18 at 9:50

















    • Nico the nameing conventon was wrong. Thanks.

      – Paul Stanley
      Nov 15 '18 at 9:36






    • 1





      ok, get it, you could mark yourself.

      – Nico Zhu - MSFT
      Nov 15 '18 at 9:50
















    Nico the nameing conventon was wrong. Thanks.

    – Paul Stanley
    Nov 15 '18 at 9:36





    Nico the nameing conventon was wrong. Thanks.

    – Paul Stanley
    Nov 15 '18 at 9:36




    1




    1





    ok, get it, you could mark yourself.

    – Nico Zhu - MSFT
    Nov 15 '18 at 9:50





    ok, get it, you could mark yourself.

    – Nico Zhu - MSFT
    Nov 15 '18 at 9:50













    1














    I made a mistake with the naming conventions. If your page is AbcPage then the viewmodel should be AbcViewModel NOT AbcPageViewModel.






    share|improve this answer



























      1














      I made a mistake with the naming conventions. If your page is AbcPage then the viewmodel should be AbcViewModel NOT AbcPageViewModel.






      share|improve this answer

























        1












        1








        1







        I made a mistake with the naming conventions. If your page is AbcPage then the viewmodel should be AbcViewModel NOT AbcPageViewModel.






        share|improve this answer













        I made a mistake with the naming conventions. If your page is AbcPage then the viewmodel should be AbcViewModel NOT AbcPageViewModel.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 9:35









        Paul StanleyPaul Stanley

        642722




        642722



























            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%2f53308220%2fuwp-prism-viewmodel-is-null%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

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20