locked
Xamarin: Binding property not found RRS feed

  • Question

  • User368514 posted

    This app works just fine in UWP. I have ripped out everything except one of the more basic properties that is failing on Android. It looks like this:

    MyPage.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:ViewModels="clr-namespace:MyApp.ViewModels"
             x:Class="MyApp.Views.MyApp">
        <ContentPage.BindingContext>
            <ViewModels:MyViewModel />
        </ContentPage.BindingContext>
        <ContentPage.Content>
            <ScrollView>
    
                <StackLayout Style="{StaticResource PageForm}">
    
                    <Picker ItemsSource="{Binding Modes}"
                        ItemDisplayBinding="{Binding Value}"
                        SelectedItem="{Binding SelectedMode}" />
    
                </StackLayout>
    
            </ScrollView>
    
       </ContentPage.Content>
    </ContentPage>
    

    MyPage.cs

    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace MyApp.Views
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class MyApp : ContentPage
        {
            public MyApp ()
            {
                InitializeComponent ();
            }
        }
    }
    

    MyViewModel.cs

    using MyApp.Models;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    namespace MyApp.ViewModels
    {
        public class MyViewModel: INotifyPropertyChanged
        {
            List<Mode> _modes;
            Mode _selectedMode;
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public MyViewModel()
            {
                Modes = new List<Mode>()
                {
                    new Mode() { Key=ModeType.Mode1, Value="Mode1" },
                    new Mode() { Key=ModeType.Mode2, Value="Mode2" }
                };
                SelectedMode = Modes.Single(m => m.Key == ModeType.Mode1);
            }
    
            public List<Mode> Modes {
                get { return _modes; }
                set {
                    _modes = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Modes"));
                }
            }
    
            public Mode SelectedMode {
                get {
                    return _selectedMode;
                }
                set {
                    _selectedMode = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedMode"));
                }
            }
    
        }
    }
    

    Mode.cs

    namespace MyApp.Models
    {
        public enum ModeType { Mode1, Mode2 };
        public class Mode
        {
            public ModeType _key;
            public string _value;
            public ModeType Key {
                get
                {
                    return _key;
                }
                set
                {
                    _key = value;
                }
            }
            public string Value {
                get
                {
                    return _value;
                }
                set
                {
                    _value = value;
                }
            }
        }
    }
    

    and what I see in the Debug console is

    [0:] Binding: 'Value' property not found on 'MyApp.Models.Mode', target property: 'Xamarin.Forms.Picker.Display'
    
    [0:] Binding: 'Value' property not found on 'MyApp.Models.Mode', target property: 'Xamarin.Forms.Picker.Display'
    
    [0:] Binding: 'SelectedMode' property not found on 'MyApp.ViewModels.'MyApp', target property: 'Xamarin.Forms.Picker.SelectedItem'
    

    Like I said this works if I run it as a UWP app but when I try it on Android it just doesn't work. That's about all I can say since it doesn't really say what the problem is other than the errors above which don't make sense.

    The rest of the view model actually works. The main part of the app works, I can even run the code on this view model. If I create a simple string binding that will work, even on Android.

    Any help is appreciated.

    Wednesday, June 13, 2018 3:00 AM

All replies

  • User368514 posted

    The answer is total magic to me. If someone can please explain this I will mark your answer as the accepted one.

    Anroid Project File > Properties > Linking > Set to None.

    It still didn't work so I closed Visual Studio and deleted the bin and obj directories in the PCL and Android projects. Finally it worked.

    One other thing is this seems like I've now lost the ability to have linking be set to sdk and user assemblies. What if I need that at some point?

    Thursday, June 14, 2018 2:22 AM
  • User363749 posted

    @smchughinfo Hi, I know it is an old issue, but have you found a better solution for this? I have the same issue, while the same property binding is working on iOS but throwing that error on Android. I tried setting the Linking option to none, but still no luck.

    Monday, July 13, 2020 2:02 AM