为什么它不记得在Xamarin中输入的最后一个文本? [英] Why Can't It remember last text entered in entry in Xamarin?

查看:68
本文介绍了为什么它不记得在Xamarin中输入的最后一个文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记住最后输入的内容,然后将其输出到Label上以检查其是否有效.我正在使用Montemagno的Xam.Plugins.settings.

我尝试了设置.

using Plugin.Settings;
using Plugin.Settings.Abstractions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App424
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public static string LastPickName
{
    get => AppSettings.GetValueOrDefault(nameof(LastPickName), string.Empty);
    set => AppSettings.AddOrUpdateValue(nameof(LastPickName), value);
}

string name;
public MainPage()
{
    InitializeComponent();


/*works if i force a text
nameEntry.Text = "You";
LastPickName = nameEntry.Text;

nameLabel.Text = LastPickName;*/

name = nameEntry.Text;
LastPickName = name

nameLabel.Text = LastPickName;
}


}
}

标签为空.它没有显示应该保存的数据.

解决方案

由于Label组件未绑定到变量,因此仅当您执行nameEntry.Text = "You"; e时,它才会获得其值.

要使其正常工作,您基本上有两种选择:

1.在每次更改时将值设置为标签;

只需设置Argon在其答案中建议的值或变量

2.将页面(视图)绑定到可观察对象,然后视图将侦听可观察对象(通常是视图模型)上的所有更改,并对此做出反应(例如,更改其自身Text值).

我想您打算做的是第二个.因此,您可以在页面代码后面创建一个公共字符串full属性,并将页面实例绑定到自身.像这样:

   <Label Text="{Binding MyStringProperty}"
  .../>

和绑定:

 private string myStringProperty;
    public string MyStringProperty
    {
        get { return myStringProperty; }
        set 
        {
            myStringProperty = value;
            OnPropertyChanged(nameof(MyStringProperty)); // Notify that there was a change on this property
        }
    }

如果这对您仍然没有意义/不可行,我建议您花一些时间来掌握詹姆斯的视频.

I want to remember what was last entry entered and outputting it on Label to check if it works. I'm using Xam.Plugins.settings by Montemagno.

I tried Settings.

using Plugin.Settings;
using Plugin.Settings.Abstractions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App424
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public static string LastPickName
{
    get => AppSettings.GetValueOrDefault(nameof(LastPickName), string.Empty);
    set => AppSettings.AddOrUpdateValue(nameof(LastPickName), value);
}

string name;
public MainPage()
{
    InitializeComponent();


/*works if i force a text
nameEntry.Text = "You";
LastPickName = nameEntry.Text;

nameLabel.Text = LastPickName;*/

name = nameEntry.Text;
LastPickName = name

nameLabel.Text = LastPickName;
}


}
}

Label is empty.It's not showing the data supposed to be saved .

解决方案

Because the Label component is not bounded to the variable, it just gets it value when you did nameEntry.Text = "You"; e no further more.

To make it works you have basically two choices:

1. Set the value to the label on every change;

Just set the values or variable like Argon suggests in his answer

2. Bind the page (View) to an Observable object, then the view will listen to every change on your observable object (often a View Model) and react to this (changing it's self Text value, for example).

I guess what you're intending to do is the second one. So you can create a public string full property on your page's code behind and bind the instance of your page to itself. Like this:

   <Label Text="{Binding MyStringProperty}"
  .../>

and binding:

 private string myStringProperty;
    public string MyStringProperty
    {
        get { return myStringProperty; }
        set 
        {
            myStringProperty = value;
            OnPropertyChanged(nameof(MyStringProperty)); // Notify that there was a change on this property
        }
    }

If this still does not make sense/work for you, I would suggest you spend sometime to master Data Binding, it won't take long to learn and it is crucial in Xamarin Forms. Alternatively watch James's video on DataBinding.

这篇关于为什么它不记得在Xamarin中输入的最后一个文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆