c# - MvxSpinner 未绑定(bind)

标签 c# xamarin mvvmcross

所以我需要更改 MvxSpinner 的文本颜色。我发现您无法更改 xaml 代码的颜色,因此我必须使用微调器的模板。但在我使用微调器模板之前,所有内容都会与 viewModel 正确绑定(bind),现在当我使用模板时,它似乎无法在 viewmodel 中找到我的属性。有没有办法将当前 View 模型公开给模板?

下面是我的代码段(如果有帮助的话)

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

View 模型

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 found Property:Caption on String

最佳答案

您不需要执行任何额外操作即可将虚拟机公开给模板。我面前有一些 Andorid 代码,它们所做的正是您使用旋转器所做的事情,一切都很好。

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

MvxBind 警告表明您 View 中的 ViewModel 出现问题。检查 View 的 ViewModel 设置为什么

关于c# - MvxSpinner 未绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25601160/

相关文章:

android - 我可以在 xamarin android 应用程序中设置设备语言/文化吗

android - MvvmCross 和 Xamarin Forms,Android 应用程序在获取强调色时因空引用错误而崩溃

c# - MVVMCross iOS 支持的方向

c# - "Could not find a part of the path"Azure应用程序中文件读取操作错误

c# - 不能将 Enumerable.Count 与 List 一起使用,编译器假定 List.Count

c# - 在 itemTemplate 中查找标签并从后面的代码绑定(bind)数据

mvvm-light - 用 MvvmCross 替换 MVVM Light 或同时使用两者

c# - Picturebox 中的图像 C#

ios - UISearchBar 底线

ios - 当前位置纬度和经度在 iOS 7 到 IOS 8 中的不同行为