-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
[MAUI] Specified cast is not valid #38
Comments
Can you provide a sample project? Because Pin class is not a view, you can set it XAML only to some property. I have a custom View ContentControl with DataContent property of type object. When I set it like this: <local:ContentControl>
<local:ContentControl.DataContent>
<local:Pin Address="{x:Bind SomeProperty}" />
</local:ContentControl.DataContent>
</local:ContentControl>
it works. |
I'm just using Maui.GoogleMaps. and it does have <googlemaps:Map ItemsSource="{x:Bind Pins}">
<googlemaps:Map.ItemTemplate>
<DataTemplate x:DataType="googlemaps:Pin">
<googlemaps:Pin Position="{x:Bind Position}" />
</DataTemplate>
</googlemaps:Map.ItemTemplate>
</googlemaps:Map> As you can see, the DataType for |
Data Templates should only be used to specify appearance of a data item. Look at documentation.
Because of this, generated code expects, that content of a DataTemplate is derivied from Element: static void BindingsChanged(global::Microsoft.Maui.Controls.BindableObject bindable, object oldValue, object newValue)
{
if (oldValue != null)
{
((IGeneratedDataTemplate)oldValue).Cleanup((global::Microsoft.Maui.Controls.Element)bindable);
}
if (newValue != null)
{
((IGeneratedDataTemplate)newValue).Initialize((global::Microsoft.Maui.Controls.Element)bindable);
}
} Also Visual Studio displays a warning because of misused DataTemplate: I've looked at the code of the Map control, which is also developed by you. I don't quite understand the need of ItemsSource and ItemTemplate properties. The pins can be created and added directly to Pins property. Or it can be settable. |
The code wasn't developed by me. I just did PR with another changes to it. But the original implementation is from Xamarin.Forms.GoogleMaps. The package Maui.GoogleMaps is migrated version for XF one by @themronion. Why it was made so I don't know. but it is working even with that warning you showed in the picture. But. maybe you can change the implementation of generated code to expect BindableObject instead of Element? |
It involves many changes. The code, generated for a DataTemplate, needs that the root object is an Element. Maybe I will do it in the future. |
If I try to add x:Bind to an object which is not
Element
but onlyBindableObject
I'm getting the exception specified in the title. If I use regular Binding, then it works as expected.For example I have
And if I do in XAML smth like this
I'm getting 'Specified cast is not valid'
The text was updated successfully, but these errors were encountered: