XAML does not have a DataContext

Scottintexas

Well-known member
Joined
Jun 21, 2018
Messages
47
Location
Texas
Programming Experience
5-10
I am being told by the error list that MainWindowView does not have a data context. And another error saying it does not have a Show method. It has in the past and I am stumped on how to locate the real issue.

C#:
        protected override async void OnStartup(StartupEventArgs e)
        {
            await AppHost!.StartAsync();
            var startupForm = AppHost.Services.GetRequiredService<MainWindowView>();
//Two offending lines
            startupForm.DataContext = AppHost.Services.GetRequiredService<MainWindowViewModel>();
            startupForm.Show();

            base.OnStartup(e);
        }

        public App()
        {
            AppHost = Host.CreateDefaultBuilder()
                .ConfigureServices((HostBuilderContext, services) =>
                {
                    //register services.
                    services.AddSingleton<MainWindowView>();
                    services.AddSingleton<MyLogger>();
                    services.AddSingleton<MainWindowViewModel>(serviceProvider =>
                    {
                        var paramVal = serviceProvider.GetRequiredService<MyLogger>();
                        return new MainWindowViewModel(paramVal);
                    });
                })
                .Build();
        }

Thanks for any help you can give me.
 

Latest posts

Back
Top Bottom