我有一個帶模擬器的小專案。最后獲勝者應該站在標簽上。反向代碼作業正常,但在我將“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();
}
}
}
uj5u.com熱心網友回復:
系結機制檢查是否分配為BindingContext實作的類INotifyPropertyChanged。這就是系結作業的原因。只是一個人打電話PropertyChanged不會做任何事情。你需要添加
public class MainViewModel : INotifyPropertyChanged
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402244.html
