Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Xamarin: Binding property not found

$
0
0

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.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>