maui
maui copied to clipboard
[MAUI] Runtime Error When Using the Same x:Name Property in Inherited MAUI Components
Description
When creating custom MAUI components, if one component inherits from another and both use the same x:Name property, an exception is thrown at runtime.
Steps to Reproduce
1. Create a custom MAUI component with an x:Name property.
2. Create another component that inherits from the first one and uses the same x:Name property.
3. Run the application.
Xaml example
<?xml version="1.0" encoding="utf-8"?>
<Grid xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Controls.InputLayout"
x:DataType="controls:InputLayout"
x:Name="_self"
RowDefinitions="Auto, Auto"
ColumnDefinitions="*, *">
<!--- stufff -->
</Grid>
public partial class InputLayout : Grid
<?xml version="1.0" encoding="utf-8"?>
<controls:InputLayout xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:MyApp.Controls"
x:Class="MyApp.Controls.InputView"
x:DataType="controls:InputView"
x:Name="_self">
</controls:InputLayout>
public partial class InputView : InputLayout
Version with bug
8.0.10 SR3
Is this a regression from previous behavior?
I think yes it used to work in Xamarin.Forms, but anyway if not the error should be more relevant.
Last version that worked well
Unknown, I just come back to see how .Net MAUI is doing.
Affected platforms
iOS, Android, Windows, macOS, Other (Tizen, Linux, etc. not supported by Microsoft directly)
Affected platform versions
.Net 8 - All platforms
Did you find any workaround?
You should use a different x:Name. But at least the error should be more relevant.
Relevant log output
As I already say, the error should be more relevant. took me an hour to find out...
A System.Reflection.TargetInvocationException exception is thrown at runtime.
Exception has occurred: System.Reflection.TargetInvocationException
Exception has been thrown by the target of an invocation.
at at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
at at System.RuntimeType.CreateInstanceMono(Boolean nonPublic, Boolean wrapExceptions)
at at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at at System.Activator.CreateInstance(Type type)
at at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass19_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()
at at Microsoft.Maui.Controls.ElementTemplate.CreateContent()
at at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)
at at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()
at at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()
at at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()
at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:58
at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:94
at MyApp.Program.Main(String[] args) in /Users/me/Documents/Code/MyApp/MyApp-current/app/Platforms/iOS/Program.cs:13