Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In.NET 5.0 WPF, a window in a container cannot call a style resource in app.xaml #4538

Open
lifuhua123 opened this issue May 19, 2021 · 12 comments · May be fixed by #8853
Open

In.NET 5.0 WPF, a window in a container cannot call a style resource in app.xaml #4538

lifuhua123 opened this issue May 19, 2021 · 12 comments · May be fixed by #8853
Labels
Bug Product bug (most likely) 🚧 work in progress
Milestone

Comments

@lifuhua123
Copy link

In.NET 5.0 WPF, a window in a container cannot call a style resource in app.xaml ,
as shown in the figure :
1
2
3
4

@miloush
Copy link
Contributor

miloush commented May 24, 2021

This reproduces in 4.8.

This seems to be a bug in the compiler actually. When Application.StartupUri is not in App.xaml, the compiler does not generate and include app.baml in the assembly resources.

@miloush
Copy link
Contributor

miloush commented May 24, 2021

...which seems to be an intentional optimization:

if (memberInfo != null &&
memberInfo.Name.Equals(STARTUPURI) &&
KnownTypes.Types[(int)KnownElements.Application].IsAssignableFrom(memberInfo.DeclaringType))
{
// if Application.StartupUri property then don't bamlize, but gen code since
// this is better for perf as Application is not a DO.
if (!_pass2)
{
_compiler.AddApplicationProperty(memberInfo,
xamlPropertyNode.Value,
xamlPropertyNode.LineNumber);
}
}
else
{
_compiler.IsBamlNeeded = true;
base.WriteProperty(xamlPropertyNode);
}

@miloush
Copy link
Contributor

miloush commented May 24, 2021

@lifuhua123 and this code also hints a workaround: set any property on the Application element, for example explicitly include ShutdownMode="OnLastWindowClose".

@ryalanms ryalanms added Question General question, not a problem in source code or documentation (yet) Community Discussion and removed Requires WPF team triage Untriaged labels May 24, 2021
@ThomasGoulet73
Copy link
Contributor

I think this issue is a duplicate of this one: #4457. As I said in this comment: #4457 (comment), the issue is caused by PresentationBuildTasks.dll so it will only apply to SDK-style csproj (.Net Framework or .Net Core) but this issue does not reproduce when using the old csproj style of .Net Framework.

@miloush
Copy link
Contributor

miloush commented May 24, 2021

This one reproduces for me with the old project file. That said, I agree this looks caused by the same underlying issue.

@ThomasGoulet73
Copy link
Contributor

@miloush You're right, just tested it again and I was able to reproduce this issue and #4457 using the old project style. I think I just forgot to remove StartupUri in my test.

@miloush
Copy link
Contributor

miloush commented May 24, 2021

@ryalanms I think this is meant to be a bug report rather than a question, but perhaps can be closed as duplicate.

@lifuhua123
Copy link
Author

@ThomasGoulet73 This problem is different from # 4457. Please look at the sample
WpfApp2.zip

@lifuhua123
Copy link
Author

@miloush You're right, I added shutdownMode =" onLastWindowClose "and the program starts, but why?

@ThomasGoulet73
Copy link
Contributor

ThomasGoulet73 commented May 25, 2021

@lifuhua123 The problem is different but the cause is the same. You can also use the same workaround in #4457 of adding another style to your resource dictionary in App.xaml. The bug is that it should generate the InitializeComponent method even if there is only one Style in the resource dictionary. By adding a property, you trigger the generation of InitializeComponent that was already suppose to occur when there is on style.

@lifuhua123
Copy link
Author

Okay, so is this a problem or a bug? I tried another method, the program can also start. Set shutdownMode =" onLastWindowClose ", but don't know why? @ThomasGoulet73

@ThomasGoulet73
Copy link
Contributor

This bug should be fixed by #8853. For now, you can workaround this issue by adding a dummy item in <Application.Resources>.

@ThomasGoulet73 ThomasGoulet73 added Bug Product bug (most likely) and removed Question General question, not a problem in source code or documentation (yet) Community Discussion labels Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Product bug (most likely) 🚧 work in progress
Projects
None yet
4 participants