Create AssemblyInfo.cs for .NET MAUI Project Template #9358
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a file names AssemblyInfo.cs to the Properties folder. The AssemblyInfo.cs file has a single, simple responsibility; enable XAML Compile by default.
Description of Change
This brings the .NET MAUI project template up to the same XamlC standard that Xamarin.Forms used. This was/is a known headache for developers who are not aware about the topic.
If you're not familiar with XamlC (XAML Compilation), this assembly attribute allows developers to use externally defined XAML components without requiring them to define an x:Name or create a dummy instance in C#.
Without those precautions, an exception will be thrown at runtime stating that there is an unknown type.
Example
For example, if a developer has a library with a control named "MyButton". The developer references that library and attempts to use MyButton in XAML like this:
You would expect it to work... but it will not. There's a runtime exception stating that MyButton is an unknown type.
The solution is to choose one of the following options
Solutions
Option 1- Enable XamlC (using a class level or assembly level attribute)**
This is the most reliable and dev-friendly option because we can do it in the default template without dev-user intervention. We also provide the developer with some other advantages of XamlC by default!
We could add it to the MauiProgram.cs file like this:
However, that's not best practice. The standard for this is to have an AssemblyInfo.cs file in the Properties folder. This lets the developer know it is an assembly wide attribute.
Option 2 - Give every control in all the XAML places, an
x:Name
property value**This is unreasonable and easily forgettable.
Option 3 - Create a dummy instance of that component in somewhere in the project's C#**
This is also clumsy and unreasonable to ask the dev-user to do this.
Conclusion
The most reliable and dev-friendly option is to just enable XamlC at the assembly level so that the custom components will work in every XAML file.
That is why in this Pull Request, I have made the small change to the default project template
maui-mobile
, which contains Option 1 implemented in anAssemblyInfo.cs
file.