Page 1 of 1

MainWindow.xaml not loading using Application Framework on .NET Core

Posted: 17 May 2019, 16:47
by skyflashde
Hi, I am trying to get a console .NET Core app working that starts up Noesis.

I followed your samples and this post: viewtopic.php?f=3&t=1668

I got the following files now:

Program.cs:
namespace Dashboard
{
    class Program
    {
        static App app;

        public static void Main(string[] args)
        {
            app = new App() { Uri = "App.xaml", StartupUri = "MainWindow.xaml" };
            app.Run();
        }
    }
}
App.xaml.cs:
using NoesisApp;

namespace Dashboard
{
    public partial class App : Application
    {
        public App()
        {
        }

        protected override Display CreateDisplay()
        {
            return new Win32Display();
        }

        protected override RenderContext CreateRenderContext()
        {
            return new RenderContextD3D11();
        }
    }
}
MainWindow.xaml.cs:
namespace Dashboard
{
    public class MainWindow : NoesisApp.Window
    {
        public MainWindow()
        {
        }
}
My problem is that the MainWindow constructor never gets hits. A window opens, but the screen is completely black.

The last thing that works is the app.Run(); after that my app is dead. No more breakpoints get hit.

All my XAML are on embedded now.
The App.xaml and the MainWindow.xaml are the empty stubs from the sample, the namespaces and class names have been checked like 100 times.
I am using the newest NuGets, no problems with dependencies as far as I can tell.

Does anyone have any clue why this could be happening?

Thank you!

Re: MainWindow.xaml not loading using Application Framework on .NET Core

Posted: 17 May 2019, 20:33
by dstroup
Your MainWindow.xaml and MainWindow.xaml.cs files probably aren't being linked up properly at build time (and the same is likely true for App.xaml/App.xaml.cs depending on how you created them).

xaml.cs files are usually partial classes to allow combining them with the xaml, so your MainWindows.xaml.cs should have
public partial class MainWindow : NoesisApp.Window
instead of
public class MainWindow : NoesisApp.Window

You'll also need to specify that the xaml.cs files are dependent on the xaml files in your project file, if they aren't already. If you use Visual Studio, this is normally handled for you when adding new WPF classes. .NET Core doesn't do that since it doesn't support WPF yet. You can do this by adding the following to your project file:
<ItemGroup>
    <!-- You may not need this first line -->
    <Compile Include="**\*.cs" Exclude="obj\**" />
    <Compile Update="**\*.xaml.cs">
        <DependentUpon>%(Filename)</DependentUpon>      
        <SubType>Code</SubType>
    </Compile>
  </ItemGroup>

Re: MainWindow.xaml not loading using Application Framework on .NET Core

Posted: 17 May 2019, 21:51
by sfernandez
Hi skyflashde,

As I can't see the xamls my guess is that you don't have the x:Class in the MainWindow root, so it is creating a default Window instead, could that be the problem?
I created an empty .NET Core console project myself that works so you can compare it with your own. Please take a look at the attached zip and let me know.

Hope this helps.

Re: MainWindow.xaml not loading using Application Framework on .NET Core

Posted: 19 May 2019, 18:18
by wyvern010
have you tried setting all .xaml files to: Embeded Resource inside the file properties tab?
That was the thingy that made it work for me...


Cheers!

Re: MainWindow.xaml not loading using Application Framework on .NET Core

Posted: 20 May 2019, 08:38
by skyflashde
Good morning....

I have implemented all of your suggestions and it is still not working.

The sample project is working, however.

I have tried to modify my project according to the sample, but so far that doesn't work. The only difference between my code (that isnt working) and the sample (which is working) are now the additional dependencies (which I need), the folder path and the namespace name.

So next I will try to put my code into the working sample until it stops working.

No clue. Really.

Thanks though. ;)

Re: MainWindow.xaml not loading using Application Framework on .NET Core

Posted: 20 May 2019, 08:52
by skyflashde
Ok, I found the problem.

It is the namespace name.

If I take the sample application and change the namespace from ConsoleApp1 to ConsoleApp2 it stops working.

Anyone have a clue why that could be happening? :)

This is my problem, just that its not the solution, its the .csproj filename:

https://github.com/aspnet/AspNetCore/issues/5524

Solution to my problem:

Insert this into .csproj -> <RootNamespace>MyNamespace</RootNamespace>

If it is missing, all the generated code is compiled into some namespace that is constructed by the folder structure or whatever and the Application framework doesn't work.

Re: MainWindow.xaml not loading using Application Framework on .NET Core

Posted: 21 May 2019, 11:54
by sfernandez
After some investigation I found that Visual Studio uses <RootNamespace> project property to generate the name of the Embedded Resources, and unfortunately that value is not available during runtime because it is a Visual Studio thing. So if you have "ConsoleApp1" as RootNamespace, then manifest resource names will be in the form "ConsoleApp1.Folder1.Folder2.File.ext".

Our default embedded resource providers use the current Application object namespace to build the manifest resource name and perform the resource lookup, so in order to work RootNamespace project property must match the Application namespace.