DI and Views

I am not sure where I'm going wrong but I am trying to open a new view (window) from the main window but I am stuck with some unresolved issues in code. I have the services configured and everything. I have uploaded the source to Github. https://github.com/coredreamstudios/TitanStudioWPF Preview of the issue: Thank you in advice.
GitHub
GitHub - coredreamstudios/TitanStudioWPF
Contribute to coredreamstudios/TitanStudioWPF development by creating an account on GitHub.
No description
10 Replies
Keswiik
Keswiik•4w ago
that is missing because it exists in TitanStudio.Wpf while your VM is in TitanStudio.Core, so it does not know MatchRulesEditorView is a type i don't work much with WPF but the way you've organized this feels very sus TitanStudio.Core - depends on nothing, tries to use references from TitanStudio.Wpf and contains a couple VMs TitanStudio.Logic - depends on nothing, also has a VM TitanStudio.Wpf - has all of your views, references the core project so the only way for both your VMs and Views to know of each other is by creating circular references which seems like bad design
Temporal Nightmare
Temporal NightmareOP•4w ago
so what can I do @Keswiik ? sorry, fell asleep 😦
Temporal Nightmare
Temporal NightmareOP•4w ago
No description
Temporal Nightmare
Temporal NightmareOP•4w ago
I got rid of the "DataLogic" breaking shit down int 504354305430530 projects is a crazy confusing thing for ADHD'ers like myself
Keswiik
Keswiik•4w ago
i don't think you really need to worry about breaking everything into super specific projects i'd only be splitting them out if you have a reason to
Temporal Nightmare
Temporal NightmareOP•4w ago
I'm only gonna keep Core and Wpf then Wpf is Views/Controls only
leowest
leowest•4w ago
yes for now its best if u just keep Core and Wpf and dont overcomplicate things as u go is MainView even supposed to be used or your main window is MatchRulesEditorView? because as far as I can see ur not using MainView if so you would have
protected override void OnStartup(StartupEventArgs e)
{
var serviceCollection = new ServiceCollection();

ConfigureServices(serviceCollection);
_serviceProvider = serviceCollection.BuildServiceProvider();

MainWindow = _serviceProvider.GetRequiredService<MatchRulesEditorView>();
MainWindow.Show();
base.OnStartup(e);
}
protected override void OnStartup(StartupEventArgs e)
{
var serviceCollection = new ServiceCollection();

ConfigureServices(serviceCollection);
_serviceProvider = serviceCollection.BuildServiceProvider();

MainWindow = _serviceProvider.GetRequiredService<MatchRulesEditorView>();
MainWindow.Show();
base.OnStartup(e);
}
Make use of the default MainWindow Property. I would also advise u drop using Interface for now to keep things simpler. rather then trying to open a new window, are u sure u dont just want to navigate to another control? You could for example in a "MainWindow" we usually call this the shell, have something like say
<Grid>
<ContentControl Content="{Binding Page}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:LoginViewModel}">
<feature:Login />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:DashboardViewModel}">
<feature:Dashboard />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</Grid>
<Grid>
<ContentControl Content="{Binding Page}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:LoginViewModel}">
<feature:Login />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:DashboardViewModel}">
<feature:Dashboard />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</Grid>
and in your LoginViewModel for example assuming your using CommunityToolkit.Mvvm you can use the messenger available there to communicate back to the ShellViewModel to move to another page. So page is just:
[ObservableProperty]
private BaseViewModel _page;
[ObservableProperty]
private BaseViewModel _page;
Could be just ObservableObject. This would be using UserControl as the pages. you can just change Page to whatever page u want to display and it will swap. if you look at the WinUI3 Gallery for example you can have a good idea of how the pages,menu etc work https://apps.microsoft.com/detail/9p3jfpwwdzrc?hl=en-US&gl=US
Temporal Nightmare
Temporal NightmareOP•2w ago
lol just saw this Hello friends I am stuck on a small issue which I did check everything but missing something.
IMainViewModel:
namespace Fusion.Library.Interfaces;

public interface IMainViewModel
{
void ShowHashGenerator();
}
IMainViewModel:
namespace Fusion.Library.Interfaces;

public interface IMainViewModel
{
void ShowHashGenerator();
}
App.xaml.cs

using Fusion.App.ViewModels;
using Fusion.App.Views;
using Fusion.Library.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace Fusion.App;

public partial class App : Application
{
private IServiceProvider serviceProvider = null!;

public App()
{
InitializeComponent();
}

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);

serviceProvider = serviceCollection.BuildServiceProvider();

var mainWindow = serviceProvider.GetRequiredService<MainWindow>();;

mainWindow.Show();
}

private void ConfigureServices(IServiceCollection services)
{
// Configure Logging
services.AddLogging();

// Register ViewModels
services.AddSingleton<IMainViewModel, MainViewModel>();

// Register Views
services.AddSingleton<MainWindow>();
services.AddSingleton<HashGeneratorView>();
}

private void onExit(object sender, ExitEventArgs e)
{
// Dispose of services if needed
if (serviceProvider is IDisposable)
{
((IDisposable)serviceProvider).Dispose();
}
}
}
App.xaml.cs

using Fusion.App.ViewModels;
using Fusion.App.Views;
using Fusion.Library.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace Fusion.App;

public partial class App : Application
{
private IServiceProvider serviceProvider = null!;

public App()
{
InitializeComponent();
}

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);

serviceProvider = serviceCollection.BuildServiceProvider();

var mainWindow = serviceProvider.GetRequiredService<MainWindow>();;

mainWindow.Show();
}

private void ConfigureServices(IServiceCollection services)
{
// Configure Logging
services.AddLogging();

// Register ViewModels
services.AddSingleton<IMainViewModel, MainViewModel>();

// Register Views
services.AddSingleton<MainWindow>();
services.AddSingleton<HashGeneratorView>();
}

private void onExit(object sender, ExitEventArgs e)
{
// Dispose of services if needed
if (serviceProvider is IDisposable)
{
((IDisposable)serviceProvider).Dispose();
}
}
}
MainWindow.xaml.cs

using Fusion.Library.Interfaces;
using System.Diagnostics;
using System.Windows;

namespace Fusion.App;

public partial class MainWindow : Window
{
public MainWindow(IMainViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
// Debug: Make sure DataContext is correct
Debug.WriteLine($"DataContext: {DataContext?.GetType().Name}");
}
}
MainWindow.xaml.cs

using Fusion.Library.Interfaces;
using System.Diagnostics;
using System.Windows;

namespace Fusion.App;

public partial class MainWindow : Window
{
public MainWindow(IMainViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
// Debug: Make sure DataContext is correct
Debug.WriteLine($"DataContext: {DataContext?.GetType().Name}");
}
}
and last
MainViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Fusion.App.Views;
using Fusion.Library.Interfaces;

namespace Fusion.App.ViewModels;

public partial class MainViewModel : ObservableObject, IMainViewModel
{
[RelayCommand]
public void ShowHashGenerator()
{
// Show the Hash Generator Window
var view = new HashGeneratorView();
view.Show();
}
}
MainViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Fusion.App.Views;
using Fusion.Library.Interfaces;

namespace Fusion.App.ViewModels;

public partial class MainViewModel : ObservableObject, IMainViewModel
{
[RelayCommand]
public void ShowHashGenerator()
{
// Show the Hash Generator Window
var view = new HashGeneratorView();
view.Show();
}
}
Temporal Nightmare
Temporal NightmareOP•2w ago
No description
Temporal Nightmare
Temporal NightmareOP•2w ago
But this still won't bind but the DC looks fine, what did I miss? disgard It had to be ShowHashGeneratorCommand Forgot thats how MVVM is XD

Did you find this page helpful?