Manually Showing MainWindow.xaml In App.xaml.cs (Dependency Injection)
Hey! My code is supposed to manually show MainWindow.xaml, but it doesn't if I remove
StartupUri="MainWindow.xaml"
and I get the error that is listed with details below with it. Without it, I get no error, so it must be my logic. The way I wrote it is because of dependency injection for AppLanguageServices.
App.xaml
App.xaml.cs
46 Replies
This is WPF?
Yes, it is WPF.
You have to set
MainWindow
in the app
Just making and showing the window doesn't work, you have to explicitly replace the MainWindow property for it to work
Otherwise it assumes it has to use StartupUri
which would be requiredWhat about the ServiceProvider in my code line?
Doesn't matter, you don't set
MainWindow
MainWindow
is a property in Application
, mainWindow
is your local variable
So set MainWindow
, not mainWindow
You can do whatever you want, you just have to set MainWindow
inside the startup which you're not doing. You can use DI like you do now.
@wildREA works?
This is my current line of code. It does not work and I need
ServiceProvider
as a required parameter, otherwise I assume I'd get an error due to a missing argument.
This is all you need to do
I already tried that, but I can try again and see if I maybe have had done something wrong.
Worked for me 🤷♂️
In a minimal application this is all that's required for manual creation of the window
@wildREA ?
Sorry for forgetting to respond. I have a senior colleague try help me out, but we couldn't initially figure it out. He found me a YouTube video I should try and watch and see if it helps. The thing you gave me did not work.
What does it say though
Like, what's the error now
The
OnStartup
method has 0 references, so it does not ever run at all.OnStartup
is an overridden method which is called on app load. You can verify it by placing a breakpoint inside of it
You don't have to call it
WPF does it for youThe problem is that it never runs since it, as I said, has 0 references, so something is mysteriously incorrect. I have interfaces setup for
IServiceProvider
, but since OnStartup
doesn't run even with correct syntax for registering services, it cannot run since, well, it has no references as I mention again.How did you verify this? Did you place a breakpoint in it?
I did it by using a breakpoint and generally reading that there aren't any references.
0 references
at the top is something you should not be reading here
This is an internal method you override and internal code calls it
So this is not an issue with main window at this point, your method is not calledOh right, but it still never runs.
So what does this setting say in your csproj properties?
Also, are you starting the correct project? Make sure you have the project as the startup project
Something is either blocking this code from running because something else is run, or you didn't compile the changes at all
If that doesn't solve it I suggest you delete the
bin
and obj
folder from your project, recompile, and check again
It would also help if you shared all the code and the exception more properly
Also, this exception generally suggests you still use parameters somewhere where you should not use them
I have only seen 0.01% of the code so I can only assume somewhere you have an Application/Window/Control constructor that takes parameters
So you should verify that's not the case
Would be nice if we could continue the conversation in a steady manner rather than having huge pauses by the way
Unless your issue is resolvedIs it okay with you if I enter a voice channel with you tomorrow after college?
By the way, I had break for 30 minutes, so that's why there was such a huge pause.
I see
I don't want to join a voice channel because generally it won't improve the conversation
I just want to have more context, like the current error and the code that is actually used here
Also considering I asked you to check various things, such as the constructors, and I have no clue what the answer to those would be
FusedQyou
I have only seen 0.01% of the code so I can only assume somewhere you have an Application/Window/Control constructor that takes parameters
Quoted by
<@191282187016863744> from #Manually Showing MainWindow.xaml In App.xaml.cs (Dependency Injection) (click here)
React with ❌ to remove this embed.
See how that changes things
You'll have to add an explicit
Initialize
method in here and keep an empty constructor
Generally you make factories because of this reason. Also something mentioned in the documentation
@wildREA
This is how it looks now, but it does not open the window.
Does anything happen at all? Any errors?
I can't imagine it just doesn't do anything unless you literally don't tell it to do anything, such as not showing the window at all
I've manually coded it to show MainWindow, but it doesn't. It runs without errors, but no window nonetheless.
I'm referring to your suggested changes.
Next thing to do would be to verify it still gets to
MainWindow?.Show()
and see that it is non-null and calling it
Also put a breakpoint inside the MainWindow's constructor to see if it's called
Actually, I'll just clone your repo and check myselfThe breakpoint tells me that
public MainWindow
does not run and MainWindow?.Show();
is not null.
Just fork it.It doesn't even run for me
Like, doesn't even call the startup object
Now we're in the same boat.
You say it gets to the Show method though
No, I have figured out that the
OnStartup
method does not run.I don't understand this then
How can it not be null if it doesn't run
Anyway the issue is your App isn't wrapped in a namespace
You removed it by accident 7 days ago
Well, there is some progression. At least I have different errors now, of which are missing assembly references for
IAppLanguageServices
.
This is just embarassing.
Just for your information, I have break until 12 (XX:00).All good
If you want I can add a pull request that fixes this
I'm just confused why the conversation started off with a whole different issue that seemed to suggest it worked for you initially
Or was this an old version by any chance?
The error I had personally was something about settings not being registered by DI, which is a matter of specifying it in the service collection
To be fair, WPF is dumb as hell for not properly erroring because it can't find the entry point. I would expect something to break here but instead it just ignores it
Sure, go ahead.
It was probably an older version. I have registered my services, and they should work, but yes, WPF is a work of deterred art.
Sorry, I got quite busy so I'm not sure if I can contribute to the change. I hope the fix is clear so maybe you can do it
It's fine, I've already implemented the changes. I've defined
IAppLanguageServices
and added the namespace, of which caused the language service assembly references to be missing. The problem is, the issue still persists with not receiving any errors, while not having the MainWindow show. Listen, you can contribute further if you'd like, but I'm done for today at 15:30 (XX:30), so I'll check on this either later today if I have time or tomorrow.I'm quite busy at work currently but I can take a look at it later