0

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
    {
    }
}
NateLFO
  • 9
  • 2
  • StartupUri should have remained the same even after changing namespace `StartupUri="MainWindow.xaml"`. And moving files between folders doesn't require namespace change – ASh May 24 '22 at 18:53
  • I tried changing that back to `StartupUri="MainWindow.xaml"`, no change, it still cannot find the MainWIndow.xaml. I also tried changing the App.xaml `xmlns:local="clr-namespace:WPF_MVVM_Test.Views"` back to `xmlns:local="clr-namespace:WPF_MVVM_Test"` with no change. – NateLFO May 24 '22 at 19:03
  • 1
    if this issue is blocking development, use a different approach - init start window from code: https://stackoverflow.com/a/47901337/1506454, https://stackoverflow.com/questions/13425425/how-to-customize-startup-of-wpf-application – ASh May 24 '22 at 19:09
  • @ASh - Thank you. Removing the StartupUri from App.xaml and adding the following to the App.xaml.cs file worked. `protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); new MainWindow().Show(); }` – NateLFO May 24 '22 at 20:12

1 Answers1

0

Thanks to ASh for the very helpful reply. I'm not sure what is preventing the application from compiling by moving the MainWindow.xaml file in to a folder, but the workaround references ASh provided definitely works.

First, I removed the StartupUri statement from the App.xaml file.

Second, in the App.xaml.cs file, I added an override for an OnStartup event as shown below, ensuring the folder that the MainWindow files are located in is in the using statements.

App.xaml.cs

using System.Windows;
using WPF_MVVM_Test.Views;

namespace WPF_MVVM_Test
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            new MainWindow().Show();
        }
    }
}
NateLFO
  • 9
  • 2