Xamarin:未找到绑定属性

2024-04-11

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

我的页面.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>

我的页面.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;
            }
        }
    }
}

我在调试控制台中看到的是

[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 上也可以工作。

任何帮助表示赞赏。


答案对我来说简直就是神奇。如果有人可以解释这一点,我会将您的答案标记为已接受的答案。

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

还是不行,所以我关闭了Visual Studio,并删除了PCL和Android项目中的bin和obj目录。终于成功了。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Xamarin:未找到绑定属性 的相关文章

随机推荐