-
Notifications
You must be signed in to change notification settings - Fork 667
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
[BUG] XamlTypeInfo.g.cs generates invalid code for types with required or init properties #8723
Comments
Same problem. So we can't use types with the required keyword in WinUI? |
FWIW I haven't found any workarounds that let me use it so I've had to jump some hoops to avoid using both required and init properties anywhere WinUI sees. |
Is there some attribute one can use on the class or property so that it is not included in Trying to forward the property so it's not used in the XAML didn't solve the problem, i.e.:
Ended up declaring all properties that causes the issue like this:
Because if I let them be |
Generated XamlTypeInfo.g.cs contained invalid code for @ref. It's not my bug but I can fix it by avoiding @ref. Related (may be): microsoft/microsoft-ui-xaml#8723
@karmeye What I've done in these situations is usually something like: private MyClass? _viewModel = null;
public MyClass ViewModel
{
get => ViewModelHelper.Get(_viewModel);
set => ViewModelHelper.Set(ref _viewModel, value);
}
internal static class ViewModelHelper
{
public static T Get<T>(T? field) where T : class
{
return field ?? throw new InvalidOperationException("View model was not initialized.");
}
public static void Set<T>([NotNull] ref T? field, T value) where T : class
{
ArgumentNullException.ThrowIfNull(value);
if (field is not null)
throw new InvalidOperationException("View model has already been set.");
field = value;
}
} |
Is there any update on this issue? This is becoming a large problem for us. It affects our ability to use modern C# features (if you can even call |
Describe the bug
If a type with
required
properties is used as apublic
property of a user control / page / dialog / etc (or aninternal
property that is actually used in XAML), the build fails with an error inXamlTypeInfo.g.cs
because the generated code attempts to naively new it up for some reason.Steps to reproduce the bug
This is enough for the build to fail when the property is public, like above. If the property is internal then the control builds, but then compilation fails when the property is actually used from XAML, i.e:
In either case, you get:
Occurs on this line:
I am also having a similar problem with
{ get; init; }
properties whereXamlTypeInfo.g.cs
sometimes tries to generate a method that sets init props on view models.Expected behavior
Should just work the same as types that don't have parameterless constructors and not attempt to new it up in
XamlTypeInfo.g.cs
.Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.4 Preview 1: 1.4.230628000-preview1
Windows version
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: