Xamarin:找不到绑定属性

时间:2018-06-13 02:22:12

标签: c# xamarin xamarin.forms xamarin.android

此应用程序在UWP中运行良好。除了在Android上失败的一个更基本的属性之外,我已经删除了所有内容。它看起来像这样:

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

我在Debug控制台中看到的是

[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'

就像我说的那样,如果我将它作为UWP应用程序运行,但是当我在Android上试用它时它就不起作用了。这就是我所能说的全部,因为除了上面没有意义的错误之外,它并没有真正说明问题是什么。

视图模型的其余部分实际上有效。该应用程序的主要部分工作,我甚至可以在此视图模型上运行代码。如果我创建一个简单的字符串绑定,即使在Android上也可以。

感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

答案对我来说是完全神奇的。如果有人可以解释一下,我会将你的答案标记为已接受的答案。

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

它仍然没有用,所以我关闭了Visual Studio并删除了PCL和Android项目中的bin和obj目录。最后它奏效了。

另一件事是,我现在已经失去了将链接设置为sdk和用户程序集的能力。如果我在某个时候需要它会怎么样?

答案 1 :(得分:0)

您的模式模型类还需要实现INotifyPropertyChanged

答案 2 :(得分:0)

使用一种方法绑定以避免调试控制台中出现这些绑定错误。

Text="{Binding [Name], Source={x:Static i18n:Translator.Instance}, Mode=OneWay}"

如果您需要TwoWay绑定,请确保绑定的模型对象实现Markus Michel所示的INotifyPropertyChanged