【问题标题】:MvxSpinner not bindingMvxSpinner 未绑定
【发布时间】:2014-09-01 16:20:15
【问题描述】:

所以我需要更改 MvxSpinner 的文本颜色。我看到您无法从 xaml 代码更改颜色,因此我不得不为微调器使用模板。但是在我为微调器使用模板之前,一切都会与 viewModel 正确绑定,现在当我使用模板时,它似乎无法在 viewmodel 中找到我的属性。有没有办法将当前视图模型暴露给模板?

如果有帮助,下面是我的代码段

<?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" />

视图模型

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); }
        }
    }

我收到以下错误 -

MvxBind:Warning: 17.62 Unable to bind: source property source not 找到属性:字符串上的标题

【问题讨论】:

    标签: c# xamarin mvvmcross


    【解决方案1】:

    您无需执行任何额外操作即可将 VM 公开给模板。我面前有一些 Andorid 代码,它与您使用微调器所做的完全一样,一切都很好。

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

    MvxBind 警告表明您视图上的 ViewModel 有问题。检查视图的 ViewModel 设置为什么

    【讨论】:

    • 抱歉,我得到的错误是标题。我已经更新了原始帖子错误消息。我仍然在这方面遇到错误。
    • 看起来您正在绑定到一个字符串列表——而不是一个具有Caption 属性的对象列表。如果要绑定到整个 string 对象,请使用 . 句点来绑定整个对象。
    • @Stuart 我想要做的绑定是 ObservableCollection。这与本例中的 List 相同吗?我正在尝试避免使用 Lists 导致绑定问题。
    • 警告说绑定在寻找 Property:Caption on String 时遇到问题 - 所以这与列表无关。
    • @Stuart - 谢谢我使用您提到的对象列表解决了它。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    相关资源
    最近更新 更多