SpaceApple
SpaceApple

Reputation: 1327

MvxSpinner not binding

So I needed to change the text color of my MvxSpinner. I see you can't change the color from the xaml code so I had to do use templates for the spinner. But before I used templates for the spinner everything would bind correctly with the viewModel, now it seems it can't find my properties in the viewmodel when I use templates. Is there a way to expose the current viewmodel to the templates?

Below is my code segments if it helps

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">
    <ImageView
        android:src="@drawable/synchramed_trans_300"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1" />
    <TextView
        android:text="Select Practice"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:textColor="#000000" />
    <MvxSpinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:textColor="#000000"
        local:MvxItemTemplate="@layout/item_spinner"
        local:MvxDropDownItemTemplate="@layout/item_spinnerdropdown"
        local:MvxBind="ItemsSource PracticeItems; SelectedItem SelectedPracticeItem" />
    <Button
        android:text="Generate Report"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        local:MvxBind="Click ReportCommand"
        style="@style/DefaultButtonText"
        android:background="@drawable/button_default_bg" />
</LinearLayout>

Item_Spinner

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:background="#fff000"
    android:foreground="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    local:MvxBind="Text Caption" />

Item_SpinnerDropDown

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:background="#fff000"
    android:foreground="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    local:MvxBind="Text Caption" />

ViewModel

public class HomeViewModel
        : MvxViewModel
    {
        string PracticeName = string.Empty;

        private readonly IMvxMessenger _messenger;
        private readonly IHomeService _homeService;
        public HomeViewModel(IHomeService homeService, IMvxMessenger messenger)
        {
            _homeService = homeService;
            _messenger = messenger;
            _homeService.GetReportList(this);
        }

        public HomeViewModel()
        {

        }

        public async Task InitializeViewModel()
        {
            await GetPractice ();
        }

        private async Task GetPractice()
        {
            try 
            {
                PracticeItems = new ObservableCollection<string>(await _homeService.GetPracticeList(this));
            } 
            catch (Exception ex) 
            {
                //return null;
            }
        }

        private string _selectedItem;
        public string SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; RaisePropertyChanged(() => SelectedItem); }
        }

        private string _caption = "sjdfsfkldj";
        public string Caption
        {
            get { return _caption; }
            set { _caption = value; RaisePropertyChanged(() => Caption); }
        }


        public ICommand ReportCommand
        {
            get { return new MvxCommand(() => ShowViewModel<OverviewViewModel>(new { param = SelectedPracticeItem })); }
        }

        public class Practices
        {
            public string ErrorMessage { get; set; }

            public List<string> Practice { get; set; }
        }

        #region Report List Properties

        private List<string> _reportItems;
        public List<string> ReportItems
        {
            get { return _reportItems; }
            set { _reportItems = value; RaisePropertyChanged(() => ReportItems); }
        }

        private string _selectedReportItem;
        public string SelectedReportItem
        {
            get { return _selectedReportItem; }
            set { _selectedReportItem = value; RaisePropertyChanged(() => SelectedReportItem); }
        }

        private ObservableCollection<string> _practiceItems;
        public ObservableCollection<string> PracticeItems
        {
            get { return _practiceItems; }
            set { _practiceItems = value; RaisePropertyChanged(() => PracticeItems); }
        }

        private string _selectedPracticeItem;
        public string SelectedPracticeItem
        {
            get { return _selectedPracticeItem; }
            set { _selectedPracticeItem = value; RaisePropertyChanged(() => SelectedPracticeItem); }
        }
    }

I got the following error -

MvxBind:Warning: 17.62 Unable to bind: source property source not found Property:Caption on String

Upvotes: 2

Views: 1041

Answers (1)

Pat Long - Munkii Yebee
Pat Long - Munkii Yebee

Reputation: 3629

You should not need to do anything extra to expose the VM to the templates. I have some Andorid code in front of me that is doing exactly what you are doing with a spinner and it is all fine.

MvxBind:Warning: 17.62 Unable to bind: source property source not found Property:PracticeItems on String

That MvxBind warning is saying something is wrong with the ViewModel on your view. Check what the view's ViewModel is set to

Upvotes: 2

Related Questions