Xamarin 表单 - 从其他 .cs 文件调用变量

问题描述 投票:0回答:2

我在 Xamarin Forms 中做一个问答游戏,对于评分功能,如果用户得到正确答案,我想将 1 添加到分数中。但就我而言,即使答案正确,分数也没有增加。

我也在尝试将“分数”变量绑定到标签。我想知道我是否使用了正确的代码。

按钮

private void submit_Clicked(object sender, EventArgs e)
{
    string answer = this.answer.Text;
    string canswer = "correct";

    if (answer != null)
    {
        string ranswer = answer.Replace(" ", string.Empty);

        if (ranswer.ToLower() == canswer)
        {
            DisplayAlert("GoodJob", "You got the correct answer", "OK");
            bindingModel b = new bindingModel();
            b.score++;
          
            (sender as Button).IsEnabled = false;
        }
        else 
        {
            DisplayAlert("Unfortunately", "Your answer is wrong", "OK");
            (sender as Button).IsEnabled = false;
        }
    }
}

视图模型


public class bindingModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public int displayScore => Score;
    public int score = 0;

    void OnPropertyChanged(int score)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(score.ToString()));
    }

    public int Score
    {
        get => score;
        set
        {
            if (score != value)
            {
                score = value;
                OnPropertyChanged(score);
            }
        }
    } 
}

型号

<Label Text="{Binding Score}"/>
xamarin.forms data-binding
2个回答
0
投票

在您的页面构造函数中,保留对您的 VM 的引用

bindingModel VM;

// this is your constructor, the name will match your page name
public MyPage()
{
  InitializeComponent();

  this.BindingContext = VM = new bindingModel();

  ...
}

然后在您的事件处理程序中,您不需要创建一个新的

bindingModel

// update the Count on the VM
VM.Count++;

0
投票

回答

这里有两个问题:

  1. 你正在重新初始化你的 ViewModel 而不是引用同一个实例
  2. 您将错误的值传递给
    PropertyChangedEventArgs

1。引用视图模型

您每次都通过调用

bindingModel b = new bindingModel();

重新初始化 ViewModel

让我们初始化 ViewModel 一次,将其存储为一个字段,将其设置为我们的

BindingContext
ContentPage
,并在
submit_Clicked

中引用该字段

public partial class QuizPage : ContentPage
{
    readonly bindingModel _bindingModel;

    public QuizPage()
    {
        _bindingModel = new bindingModel();
        BindingContext = _bindingModel;
    }

    private async void submit_Clicked(object sender, EventArgs e)
    {
        string answer = this.answer.Text;
        string canswer = "correct";

        Button button = (Button)sender;
    
        if (answer != null)
        {
            string ranswer = answer.Replace(" ", string.Empty);
    
            if (ranswer.ToLower() == canswer)
            {
                await DisplayAlert("GoodJob", "You got the correct answer", "OK");
                _bindingModel.score++;
    
                button.IsEnabled = false;
            }
            else 
            {
                await DisplayAlert("Unfortunately", "Your answer is wrong", "OK");
                button.IsEnabled = false;
            }
        }
    }
}

2。 PropertyChangedEventArgs

您需要将房产名称传递给

PropertyChangedEventArgs
.

他们

PropertyChanged
的工作方式是它宣布已更改的属性的名称。在这种情况下,它需要广播
Score
属性已更改。

让我们使用

nameof(Score)
将字符串
"Score"
传递给
PropertyChangedEventArgs

void OnScorePropertyChanged()
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(displayScore)));
}

public int Score
{
    get => score;
    set
    {
        if (score != value)
        {
            score = value;
            OnScorePropertyChanged();
        }
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.