访问公共设置类中的变量的语法

huangapple go评论79阅读模式
英文:

Syntax for accessing Variables in public Settings Class

问题

以下是您要翻译的内容:

我找到了以下的讨论线程:

> 在寻找正确答案的过程中经历了很多挣扎,我能够使用
> [Preferences][1] 来非常轻松地获取/设置用户设置,而不必担心自己保存和加载。
>
> // 获取器
> var value = Preferences.Get("nameOfSetting", "defaultValueForSetting");
>
> // 设置器
> Preferences.Set("nameOfSetting", value);
>
>
> 我将我的代码包装在属性中,以便更容易使用:
>
> public string FilePath
> {
> get { return Preferences.Get(nameof(FilePath), ""); }
> set { Preferences.Set(nameof(FilePath), value); }
> }

我尝试了很多次,但无法在属性中使用这些变量。

例如,如果我想将Labeltext设置为上述讨论线程中的Filepath设置:

Text = StandardSettings.get(Filepath);

Text = StandardSettings.Filepath{get};

StandardSettings类是公共的,并且在我的xaml中通过 "using" 连接。
获取/设置Filepath的语法是什么?
获取/设置Filepath的语法,无论是在xaml还是在C#中,都将非常有帮助。

英文:

I found following thread:

> After a lot of struggling to find the right answer, I was able to use
> [Preferences][1] to really easily get/set user settings without having
> to worry about saving and loading myself.
>
> // getter
> var value = Preferences.Get("nameOfSetting", "defaultValueForSetting");
>
> // setter
> Preferences.Set("nameOfSetting", value);
>
>
> I wrapped mine in a property so it's easier to use:
>
> public string FilePath
> {
> get { return Preferences.Get(nameof(FilePath), ""); }
> set { Preferences.Set(nameof(FilePath), value); }
> }

I tried a lot but cant manage to use the variables when wrapped in a property.

e.g. if I want to set the Labeltext to the Filepath setting from thread above:

Text = StandardSettings.get(Filepath);

or

Text = StandardSettings.Filepath{get};

The class StandardSettings is public and connected by "using" in my xaml.
What would be the syntax for getting/setting the Filepath?
Would be great to get the syntax for both xaml and c#.

答案1

得分: 1

I created a new project and everything worked correctly. Here is my code:

public partial class NewPage1 : ContentPage
{
    public string FileName
    {
        get { return Preferences.Default.Get(nameof(FileName), "NoName"); }
        set { Preferences.Default.Set(nameof(FileName), value); }
    }

    public NewPage1()
    {
        InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        this.FileName = "Hahaha";
        (sender as Button).Text = this.FileName;
    }
}

When I clicked the button, the button's text will change as "Hahaha." And if I remove the this.FileName = "Hahaha";, it will be the default value "NoName."

Note: this.FileName = "Hahaha"; will call the FileName's set method. And (sender as Button).Text = this.FileName; will call its get method.

英文:

I created a new project and everything worked correctly. Here is my code:

public partial class NewPage1 : ContentPage
{
      public string FileName
      {
            get { return Preferences.Default.Get(nameof(FileName),"NoName"); }
            set { Preferences.Default.Set(nameof(FileName), value); }
      }

      public NewPage1()
      {
            InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
            this.FileName = "Hahaha";
            (sender as Button).Text = this.FileName;
    }
}

When I clicked the button, the button's text will change as Hahaha. And if I remove the this.FileName = "Hahaha";, it will be the default value NoName.

Note: this.FileName = "Hahaha"; will call the FileName's set method. And (sender as Button).Text = this.FileName; will call its get method.

huangapple
  • 本文由 发表于 2023年6月15日 14:20:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定