0
votes

i've a big Problem trying to bind a user Input to an object instanciated via a ResourceDictionary.

<ContentPage.Resources>
    <ResourceDictionary>
        <model:BusinessObjectsGraphDatasource x:Key="Testdata" 
                                              Query="query {
                                                          test {
                                                            id
                                                            description
                                                            foo
                                                          }
                                                        }"

                                              Parameter="{Binding Source={x:Reference QueryParameterEntry}, Path=Text}"/>

    </ResourceDictionary>
</ContentPage.Resources>

<StackLayout HorizontalOptions="Center" VerticalOptions="Center" Orientation="Vertical">
    <Entry x:Name="QueryParameterEntry"/>

    <ListView ItemsSource="{Binding Source={x:StaticResource Testdata}, Path=Result}"/>

</StackLayout>

As you can see, i define an custom Datasource (BusinessObjectsGraphDatasource)object within the ContentPage resources. But this Datasource should be able to handle a parameter given by a Databinding from an Entry.

BusinessObjectGraphDatasource is a class that derives from BindableObject and publishs some BindableProperties (e.g. Parameter)

Can someone give me a hint, how to handle this scenario?

Special scenario notes within this app: i don't have access to a viewmodel or to a code-behind file. the only things i can do is writing controls and resources and use them within xaml.


The Problem here, is that i retrieve a "Sequence contains no matching Elements" exception as soon as i add the Binding to the datasource.

The stacktrace Looks like that:

       at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.TryAddToProperty(Object element, String localName, Object value, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, Exception& exception)
   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, Object rootElement, INode node, HydratationContext context, IXmlLineInfo lineInfo)
   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit(ElementNode node, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.FillResourceDictionariesVisitor.Visit(ElementNode node, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.XamlLoader.Visit(RootNode rootnode, HydratationContext visitorContext)
   at Xamarin.Forms.Xaml.XamlLoader.Load(Object view, String xaml)
   at Xamarin.Forms.Xaml.XamlLoader.Load(Object view, Type callingType)
   at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml](TXaml view, Type callingType)
   at App1.MainPage.InitializeComponent()
   at App1.MainPage..ctor()
   at App1.App..ctor()
   at App1.UWP.MainPage..ctor()
   at App1.UWP.App1_UWP_XamlTypeInfo.XamlTypeInfoProvider.Activate_4_MainPage()
   at App1.UWP.App1_UWP_XamlTypeInfo.XamlUserType.ActivateInstance()

The Bindable Property is defined like that:

public BindableProperty ParameterProperty = BindableProperty.Create("Parameter", typeof(string),
            typeof(BusinessObjectsGraphDatasource), string.Empty, BindingMode.TwoWay, null, ParameterPropertyChanged);

        public string Parameter
        {
            get => (string)GetValue(ParameterProperty);
            set => SetValue(ParameterProperty, value);
        }

        private static void ParameterPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
        {
        }

cheers,

chris

1
To recreate the problem I need more code behind including BusinessObjectsGraphDatasource, etc. If you can share simple project I might be able to look - Yuri S
If you can't here is the hint. Check all your LINQ queries. One of them is throwing an exception when xaml is loaded. Check where you use First() or similar. Use debugger to find which line throws an exception - Yuri S

1 Answers

0
votes

You might have to specify explicitly that it should be a two-way binding:

Parameter="{Binding Source={x:Reference QueryParameterEntry}, Path=Text, BindingMode=TwoWay}"