首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定不会刷新lable的内容。

绑定不会刷新lable的内容。
EN

Stack Overflow用户
提问于 2022-01-01 18:53:08
回答 1查看 49关注 0票数 0

我有一个带有模拟器的小项目。最终胜利者应该站在标签上。反代码运行良好,但在我将"Winner“设置为新字符串后,绑定不会刷新。我说出了一些事情,因为它们对问题不重要。

这是我的xaml:

代码语言:javascript
复制
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Risiko.MainPage"
             xmlns:local="clr-namespace:Risiko"
             x:DataType="local:MainViewModel">

        <StackLayout>
            <Frame BackgroundColor="Red" Margin="10,10,5,10" CornerRadius="5">
                <StackLayout>
                    <Label Text="Angreifer:" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
                    <StackLayout>
                        <Entry Text="{Binding Enemie}" HorizontalOptions="FillAndExpand" Keyboard="Numeric" TextColor="White"/>
                        <Label Text="Soldaten" HorizontalOptions="EndAndExpand" TextColor="White"/>
                    </StackLayout>
                </StackLayout>
            </Frame>
            <Frame BackgroundColor="Blue" Margin="10,5,10,10" CornerRadius="5">
                <StackLayout>
                    <Label Text="Verteildiger:i" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
                    <StackLayout>
                        <Entry Text="{Binding Defender}" HorizontalOptions="FillAndExpand" Keyboard="Numeric" TextColor="White"/>
                        <Label Text="Soldaten" HorizontalOptions="EndAndExpand" TextColor="White"/>
                    </StackLayout>
                </StackLayout>
            </Frame>
            <Button Text="Start" Command="{Binding Start}" Background="green"/>
            <Frame BackgroundColor="Gray" Margin="10,5,10,10" CornerRadius="5">
                <Label Text="{Binding Winner, Mode=TwoWay}" HorizontalOptions="FillAndExpand" TextColor="White"/>
            </Frame>
        </StackLayout>
</ContentPage>

以下是我的视图模型:

代码语言:javascript
复制
  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using Xamarin.Forms;

namespace Risiko
{
    public  class MainViewModel
    {
        private string _defender;
        private string _winner;
        private string _enemie;
        private int[] enemieArray = new int[4];
        private int[] defenderArray = new int[3];
        Random random = new Random();
        private int max;
        private int max2;
        public Command Start { get; }

    public string Defender
    {
        get => _defender;
        set => SetProperty(ref _defender, value);
    }
    public string Enemie
    {
        get => _enemie;
        set => SetProperty(ref _enemie, value);
    }
    public string Winner
    {
        get => _winner;
        set => SetProperty(ref _winner, value);
    }

    public MainViewModel()
    {
        Start = new Command(OnStart);
    }

    private void OnStart()
    {            
        if(defenderArray[0] <= 0)
        {
            Winner  = "Gewonnen hat der Angreifer mit nur noch " + enemieArray[0] + " übrigen Truppen.";
        }
        else
        {
            Winner = "Gewonnen hat der Verteildiger mit nur noch " + defenderArray[0] + " übrigen Truppen.";
        }
    }

    
    #region INotifyPropertyChanged
    protected bool SetProperty<T>(ref T backingStore, T value,
        [CallerMemberName] string propertyName = "",
        Action onChanged = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;

        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;

  

      changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }

}

还有我的反码cs

代码语言:javascript
复制
  namespace Risiko
    {
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
                BindingContext  = new MainViewModel();
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-01 22:02:56

绑定机制检查分配给BindingContext的类是否实现了INotifyPropertyChanged。这就是使绑定工作的原因。光是打电话给PropertyChanged就什么也做不了。你需要添加

代码语言:javascript
复制
public class MainViewModel : INotifyPropertyChanged
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70551109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档