【问题标题】:ContentView label binding Cant find property in viewmodelContentView 标签绑定无法在视图模型中找到属性
【发布时间】:2020-03-10 10:58:23
【问题描述】:

我有一个标签要绑定到我的 viewModel 中的一个属性。但是在运行我的代码时,它会返回后备值,即使还有另一个值。在输出中它写道: 绑定:在 ContentViews.OverviewView 上找不到“名称”属性,目标属性:“Xamarin.Forms.Label.Text”

绑定:在 ContentViews.OverviewView 上找不到“匹配”属性,目标属性:“Xamarin.Forms.Label.Text” 我的代码 XAML

<ContentView.Content>
        <StackLayout BackgroundColor="#37474F">
            <Label Text="{Binding Match.Result.ScoreInfo.Score[1}.Name, FallbackValue=0-0}" />
            <Label Text="{Binding Match.Result.ScoreInfo.Score[0].Name, FallbackValue=0-0}" />
      </StackLayout>
  </ContentView.Content>

代码隐藏

  public OverviewViewModel OVM { get; set; }

        public OverviewView(Models.Match match)
        {
            OVM = new OverviewViewModel(match);
            BindingContext = OVM;
            InitializeComponent();            
        }

视图模型

public Match Match { get; set; }
        public OverviewViewModel(Match match)
        {
            Match = match;
        }

如果我在后面的代码中设置标签文本没有任何问题并且返回正确的结果。 可能是什么问题?

【问题讨论】:

  • 这里有一个错字:Score[1}.Name
  • 谢谢!我更正后它仍然返回回退值
  • 尝试使用私有匹配变量:private Match _match;然后在公有的Getter和Setter中,获取并设置私有的:public Match Match { get => _match;设置 { _match = 值; } }
  • 如果您的视图模型实现了 INotifyPropertyChanged(它应该),这样做应该调用 propertychanged 方法,如果没有,您可以使用 OnPropertyChanged();在setter中设置_match值后
  • 如果这些都不起作用,则可能是 xaml 中的另一个语法错误。有了给定的信息,我无能为力了

标签: c# xamarin.forms


【解决方案1】:

这里的语法有错误。

应该是 Score[1].Name:-

 <Label Text="{Binding Match.Result.ScoreInfo.Score[1].Name, FallbackValue=0-0}" />

【讨论】:

  • 谢谢!但是它仍然返回后备值
【解决方案2】:

在调用 InitializeComponent 后尝试设置绑定上下文。

【讨论】:

  • 仍然返回回退值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 2013-03-22
  • 2010-12-02
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多