I have a question for people familiar with the Community Toolkit MVVM regarding best practices for building up the Views part of MVVM. I have started a new project to try to get things figured out before trying to implement this into my actual project. My main question is, if you have multiple views/windows for different purposes in a WPF application, is it not a good idea to place them into a View or Views folder? If so, I'm having a hard time getting the application to launch the main window.
System.IO.IOException: 'Cannot locate resource 'wpf_mvvm_test.views.mainwindow.xaml'.'
The first thing I tried was just moving the MainWindow.xaml file to the views folder, which caused the runtime error as soon as it compiled. I started changing the namespace references for the MainWindow.xaml, MainWindow.xaml.cs, App.xaml, App.xaml.cs, as well as the startup URI to reflect the namespace for the folder, WPF_MVVM_Test.Views, and none of the changes I have made have prevented the error. I did leave the App.xaml file in the main project, not in the folder. If I try to move it into the Views folder, even if I change its x:Class and the App.xaml.cs namespace to WPF_MVVM_Test, when I try to run it, it gives me an error that there is no static Main entrypoint.
Is there no way to place the WPF window files into a folder? If it's not possible, it's not possible, but if it is possible, I would love to figure out how, as it would make it easier to keep the solution organized.
MainWindow.xaml:
<Window x:Class="WPF_MVVM_Test.Views.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_MVVM_Test.Views"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
using WPF_MVVM_Test.ViewModels;
using WPF_MVVM_Test.Views;
namespace WPF_MVVM_Test.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
}
App.xaml:
<Application x:Class="WPF_MVVM_Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_MVVM_Test"
StartupUri="WPF_MVVM_Test.Views.MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs:
using System.Windows;
using WPF_MVVM_Test.Views;
namespace WPF_MVVM_Test
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}