【问题标题】:Xaramin form -calling variable from other .cs fileXamarin 形式 - 从另一个 .cs 文件调用变量
【发布时间】:2021-03-28 06:14:31
【问题描述】:

我在 Xaramin 中做一个问答游戏。形式。和得分函数。如果用户得到正确答案,我希望分数会加 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}"/>

【问题讨论】:

  • 您的submit_Clicked 正在创建bindingModel新实例,而不是页面上已存在的实例
  • @Jason 感谢您的回复,请问使用现有实例的正确方法是什么。我是编程新手

标签: xamarin.forms data-binding


【解决方案1】:

在您的页面构造函数中,保留对您的 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++;

【讨论】:

    【解决方案2】:

    回答

    这里有两个问题:

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

    1。引用视图模型

    您每次都通过调用 bindingModel b = new bindingModel(); 重新初始化 ViewModel

    让我们初始化一次 ViewModel,将其存储为字段,将其设置为 ContentPageBindingContext,并在 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();
            }
        }
    } 
    

    【讨论】:

    • 感谢您的回复。我想问如何将值保存到变量中?因为我有大约 10 个问题,每个问题,如果用户给出正确答案,我需要增加变量。
    • @JUSTINCHUA 每次用户给出正确答案时,分数的值都会增加,该值确实保存在分数中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2020-10-09
    • 2017-11-29
    相关资源
    最近更新 更多