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
81 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
I might actually be able to help with this one, as I ran into this this week. There are 2 really serious bugs in WPF. The first is that if you set
<AssemblyName>
anywhere in your cproj (or in any other Directory.Build.props or anything), the build will fail to bind any XAML elements that are referencing the current assembly. To fix this, you need to make that property depended on "_TargetAssemblyProjectName" not being set.
The second is that if you have exactly one resource in your App.xaml file, it will just... not compile your App.xaml at all. To fix this you need to add a second resource to your App.xaml resource dictionary. I went with a super passive agressive "<system:String>" resource, but any resource will do.
Both of these can cause the constructor of your main window to throw an exception that the WPF framework tends to swallow up, which would explain why code in your OnStartup appears never to run.I have no
<AssemblyName>
tag in my .csproj
file and I have two <ResourceDictionary>
in my App.xaml. It was like that before as well, so it still does not run.
.csproj
App.xaml
I told you the application was missing namespaces
You never fixed this
FusedQyou
Anyway the issue is your App isn't wrapped in a namespace
Quoted by
<@191282187016863744> from #Manually Showing MainWindow.xaml In App.xaml.cs (Dependency Injection) (click here)
React with ❌ to remove this embed.
I made a pull request that fixes this. If you merge it the application will work again (in the
development
branch)
https://github.com/wildREA/computerComponentsTracker/pull/4
Additionally, I fixed an issue with your DI setup that causes an additional exception because IAppLanguageServices
relies on Application
and it was not regisered. Please reconsider the way you have set up your services because passing the whole application in it is very bad practice due to excessive coupling.GitHub
Fixes regarding the startup of the application by RoyDefined · Pull...
This pull request fixes two issues that cause the application to have issues during startup.
Due to a missing namespace in App.xaml.cs the application could not find the main entrypoint.
Due to mi...
@wildREA
Tested locally and after a small delay the window shows
I had already added a namespace. Anyways, it still does not show MainWindow.
If you added the namespace then how is it possible for my pull request to make these changes? It would not have been required.
None of your commits have ever fixed it. The last commit was from 4 days ago.
I never pushed my changes since.
I can't help you based on guesswork. You have to commit these things.
Also, for me the application starts when using a fresh repo. Maybe try deleting your
bin
and obj
folder and try again. Note that there's a substantial delay during startup, possible from all the IO operations happening
Does the language feature work?
I don't know what that is. I only fixed the whole issue with the startup
I can give you a bit of additional help, but you do need to explain what must be looked at then
You have to move your mouse over the settings button and click it, then you can select a language under textblock a comboboxitem.
You gotta click apply for it to change.
Well you see, when setting a breakpoint in the application you can see it finds the new language when I change the dropdown value. Try it yourself

That said, I don't see any visual changes when I do this. I assume you have to update the UI somehow if you expect the strings to display Russian
I'll push the new changes. By the way, it works. I basically had nothing to do with the actual coding or anything like that, I had to CREATE A NEW FOLDER for the project.
The namespace might have surely worked, but after that, it only showed up after I made the new folder.
I don't see how a new folder fixes anything
I think you have a bunch of changes still lying around without you noticing them. No pending commits ready to be pushed?
Are you using a GUI like Github Desktop for changes or just the command line?
Visual Studio Enterprise has a built-in Git feature. I had no lying commits.
Not sure then
I feel like you confuse yourself on this and it's more an XY problem thing
Like, it wasn't the folder but rather something you did around it
A fresh repo should work, assuming you have my fixes
It works now, I didn't get to mention it and the
ResourceDictionary
work now as well.Nice
Well, I have everything set up locally now so if there's anything else I can take a look if needed
Are you native or proficient in a language other than English?
I'm Dutch myself, I can somewhat read German but I definitely prefer just English
Could you perhaps help with translation for Dutch? I can do the German myself since I speak it as well at a comprehensible level.
Sure, I can contribute with translations but it also depends on the size. I don't have a lot of time apart from these small bits whilst at work, and the rest I'd rather spend on personal learning
Is it just the strings in the Resources folder?
Strings.nl.xaml
Yes, just this.
Foreign words like Wi-Fi and Ethernet does not have to be translated if the language does not necessarily have a translation for it.
Words like Wi-Fi and Ethernet are words we use ourselves
Same here. For Russian, Danish, German, Lithuanian, Polish, and some other languages that aren't English-based mostly.
I'll make a
String.nl.xaml
file and make sure it becomes an option in the drop downAlright, then I'll do the
de
, lt
, pl
, da
, and tr
. Thanks in advance.No problem
It's nice to add some points to the account in terms of outside contribution
For sure. I even had my friend next to me right now help me beforehand, that's the other guy in the contribution list. He'll do the
tr
(Turkish) translation.
@FusedQyou I translated Strings.xaml. Is my nl
translation correct?
Strings.nl.xaml
Yes, that looks exactly like how I would pronounce everything
I'll push it then to development.