绑定不刷新标签内容
Binding doesn't refresh the content of the lable
我有一个带有模拟器的小项目。最后获胜者应该站在标签中。反向代码工作正常,但在我将“Winner”设置为新字符串后绑定不会刷新。我泄露了一些东西,因为它们对问题并不重要。
这是我的 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>
这是我的视图模型:
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
namespace Risiko
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
}
}
绑定机制检查分配为 BindingContext
的 class 是否实现了 INotifyPropertyChanged
。这就是使绑定起作用的原因。仅仅调用 PropertyChanged
不会做任何事情。您需要添加
public class MainViewModel : INotifyPropertyChanged
我有一个带有模拟器的小项目。最后获胜者应该站在标签中。反向代码工作正常,但在我将“Winner”设置为新字符串后绑定不会刷新。我泄露了一些东西,因为它们对问题并不重要。
这是我的 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>
这是我的视图模型:
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
namespace Risiko
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
}
}
绑定机制检查分配为 BindingContext
的 class 是否实现了 INotifyPropertyChanged
。这就是使绑定起作用的原因。仅仅调用 PropertyChanged
不会做任何事情。您需要添加
public class MainViewModel : INotifyPropertyChanged