简体   繁体   English

绑定不会刷新标签的内容

[英]Binding doesn't refresh the content of the lable

I have a little project with a simulator.我有一个带模拟器的小项目。 In the end the winner should stand in the label.最终获胜者应该是 label。 The backcode works fine but the binding doesn't refresh after I set "Winner" to the new string.回码工作正常,但在我将“Winner”设置为新字符串后绑定不会刷新。 I let somethings out because they aren't important for the problem.我放了一些东西,因为它们对问题并不重要。

Here's my xaml:这是我的 xaml:

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

Here's my Viewmodel:这是我的视图模型:

  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
    }

}

And my backcode cs还有我的回码 cs

  namespace Risiko
    {
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
                BindingContext  = new MainViewModel();
            }
        }
    }

the binding mechanism checks to see if the class assigned as the BindingContext implements INotifyPropertyChanged .绑定机制检查分配为BindingContext的 class 是否实现了INotifyPropertyChanged This is what makes binding work.这就是使绑定起作用的原因。 Just calling PropertyChanged alone won't do anything.仅单独调用PropertyChanged不会做任何事情。 You need to add您需要添加

public class MainViewModel : INotifyPropertyChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM