如何在 XAML 中使用 Xam.plugins.settings 存储的设置?
How can I use settings stored with Xam.plugins.settings in my XAML?
感谢 James Montemagno 提供此插件。[https://jamesmontemagno.github.io/SettingsPlugin ]
我已经在我的 c# 代码中轻松使用了它:CrossSettings.Current.GetValueOrDefault("abc", "")
但我也想在 XAML.
中使用这些设置
在此插件之前,我使用的是我自己的基本(低效)数组设置并使用:...Text="{x:Static local:Settings.abc}"
...现在我已完全转换到此插件。它在 c# 中运行良好,但正在努力使其在 XAML.
中运行
我尝试了 {x:Static local:Helpers.Settings.GeneralSettings.abc}
和 {x:Static helps:Settings.GeneralSettings.abc}
(创建 xmlns:help)。
如何在我的 XAML 代码中使用这些设置?
执行此操作的最佳方法是设置一个 属性 访问器 (get/set) - 并将控件绑定到该方法。
作为一个快速(单向)示例..
页面代码隐藏
public int MyNumber
{
//This may be different, depending on what your Settings class has been named and where its reference has been stored - but it is the same plugin.
return App.Settings.GetValueOrDefault("myNumber",0);
}
XAML
<Label Text="{Binding MyNumber}"/>
您还需要确保已设置 BindingContext
。我发现在页面构造函数中 InitializeComponent();
之后最容易做到这一点,只需使用 BindingContext = this;
.
可以在 Settings Plugin Documentation 中找到更复杂的实现(使用双向数据绑定)。
感谢 James Montemagno 提供此插件。[https://jamesmontemagno.github.io/SettingsPlugin ]
我已经在我的 c# 代码中轻松使用了它:CrossSettings.Current.GetValueOrDefault("abc", "")
但我也想在 XAML.
在此插件之前,我使用的是我自己的基本(低效)数组设置并使用:...Text="{x:Static local:Settings.abc}"
...现在我已完全转换到此插件。它在 c# 中运行良好,但正在努力使其在 XAML.
我尝试了 {x:Static local:Helpers.Settings.GeneralSettings.abc}
和 {x:Static helps:Settings.GeneralSettings.abc}
(创建 xmlns:help)。
如何在我的 XAML 代码中使用这些设置?
执行此操作的最佳方法是设置一个 属性 访问器 (get/set) - 并将控件绑定到该方法。
作为一个快速(单向)示例..
页面代码隐藏
public int MyNumber
{
//This may be different, depending on what your Settings class has been named and where its reference has been stored - but it is the same plugin.
return App.Settings.GetValueOrDefault("myNumber",0);
}
XAML
<Label Text="{Binding MyNumber}"/>
您还需要确保已设置 BindingContext
。我发现在页面构造函数中 InitializeComponent();
之后最容易做到这一点,只需使用 BindingContext = this;
.
可以在 Settings Plugin Documentation 中找到更复杂的实现(使用双向数据绑定)。