gpt4 book ai didi

c# - Xamarin:找不到绑定(bind)属性

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:39 24 4
gpt4 key购买 nike

此应用在 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;
}
}
}
}

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

[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 上尝试它时,它就不起作用了。这就是我能说的全部,因为除了上面没有意义的错误之外,它并没有真正说明问题所在。

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

感谢任何帮助。

最佳答案

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

Anroid 项目文件 > 属性 > 链接 > 设置为无。

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com