从主页绑定到 Template10 设置
Binding to Template10 Settings from Main Page
到目前为止,我很喜欢 Template10,非常好。
我对如何绑定到主页上的设置值有点困惑。
我添加了一个正确存储的新 bool 设置。
在我的主页上,我有一个绑定到设置的可见性:
Visibility="{Binding UseAmbientLightSensor, Converter={StaticResource CollapsedWhenFalseConverter}}"
这会按预期在应用程序启动时运行,MainPageViewModel 从“设置”中读取值,并根据该设置显示或折叠网格。
但是我似乎无法将此绑定到 'listen' 设置,如果我转到设置页面并更改该值,当我返回主页时,可见性不会改变。它只有在我重新启动应用程序时才有效。
在 vanilla Template10 安装中,这类似于将 MainPage 上的一个小徽标绑定到“设置”页面中的 'UseLightThemeButton' 设置,该设置会根据该设置发生变化..
有两种可能不会发生的情况:
- 当您的 bool 值更新时引发 属性 更改事件。
- 将绑定设置为双向模式。
为此,请更改 Visibility
属性
的绑定模式
Visibility="{Binding UseAmbientLightSensor, Mode=TwoWay, Converter={StaticResource CollapsedWhenFalseConverter}}"
这将告诉 xaml 监听视图模型中 属性 的任何变化。
然后你需要告诉View模型什么时候让XAML视图知道它的变化,如果你使用的是Template10,那么就可以这样做:
private bool useAmbientLightSensor;
public TodoListControlViewModel UseAmbientLightSensor
{
get
{
return this.useAmbientLightSensor;
}
set
{
this.Set(ref this.useAmbientLightSensor, value);
}
}
视图模型需要从 ViewModelBase
class 扩展,它提供引发 OnPropertyChanged
事件的 Set
方法,允许视图知道任何更改在视图模型中。
有关详细信息,请查看 INotifyPropertyChanged
interface and its implementation。
好的,我想这就是 "official" 的答案。但是很多方法都是有效的。这个与模板最匹配。我会这样做:
public class MainPageViewModel : ViewModelBase
{
Services.SettingService.SettingService _SettingService;
public MainPageViewModel()
{
_SettingService = Services.SettingService.SettingService.Instance;
}
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
{
Windows.Storage.ApplicationData.Current.DataChanged += SettingsChanged;
await Task.CompletedTask;
}
public override async Task OnNavigatedFromAsync(IDictionary<string, object> pageState, bool suspending)
{
Windows.Storage.ApplicationData.Current.DataChanged -= SettingsChanged;
await Task.CompletedTask;
}
private void SettingsChanged(Windows.Storage.ApplicationData sender, object args)
{
RaisePropertyChanged(nameof(FontSize));
}
public double FontSize { get { return _SettingService.FontSize; } }
}
使用该视图模型,您可以轻松绑定到设置(在本例中为 FontSize)。
祝你好运。
到目前为止,我很喜欢 Template10,非常好。 我对如何绑定到主页上的设置值有点困惑。 我添加了一个正确存储的新 bool 设置。 在我的主页上,我有一个绑定到设置的可见性:
Visibility="{Binding UseAmbientLightSensor, Converter={StaticResource CollapsedWhenFalseConverter}}"
这会按预期在应用程序启动时运行,MainPageViewModel 从“设置”中读取值,并根据该设置显示或折叠网格。
但是我似乎无法将此绑定到 'listen' 设置,如果我转到设置页面并更改该值,当我返回主页时,可见性不会改变。它只有在我重新启动应用程序时才有效。
在 vanilla Template10 安装中,这类似于将 MainPage 上的一个小徽标绑定到“设置”页面中的 'UseLightThemeButton' 设置,该设置会根据该设置发生变化..
有两种可能不会发生的情况:
- 当您的 bool 值更新时引发 属性 更改事件。
- 将绑定设置为双向模式。
为此,请更改 Visibility
属性
Visibility="{Binding UseAmbientLightSensor, Mode=TwoWay, Converter={StaticResource CollapsedWhenFalseConverter}}"
这将告诉 xaml 监听视图模型中 属性 的任何变化。
然后你需要告诉View模型什么时候让XAML视图知道它的变化,如果你使用的是Template10,那么就可以这样做:
private bool useAmbientLightSensor;
public TodoListControlViewModel UseAmbientLightSensor
{
get
{
return this.useAmbientLightSensor;
}
set
{
this.Set(ref this.useAmbientLightSensor, value);
}
}
视图模型需要从 ViewModelBase
class 扩展,它提供引发 OnPropertyChanged
事件的 Set
方法,允许视图知道任何更改在视图模型中。
有关详细信息,请查看 INotifyPropertyChanged
interface and its implementation。
好的,我想这就是 "official" 的答案。但是很多方法都是有效的。这个与模板最匹配。我会这样做:
public class MainPageViewModel : ViewModelBase
{
Services.SettingService.SettingService _SettingService;
public MainPageViewModel()
{
_SettingService = Services.SettingService.SettingService.Instance;
}
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
{
Windows.Storage.ApplicationData.Current.DataChanged += SettingsChanged;
await Task.CompletedTask;
}
public override async Task OnNavigatedFromAsync(IDictionary<string, object> pageState, bool suspending)
{
Windows.Storage.ApplicationData.Current.DataChanged -= SettingsChanged;
await Task.CompletedTask;
}
private void SettingsChanged(Windows.Storage.ApplicationData sender, object args)
{
RaisePropertyChanged(nameof(FontSize));
}
public double FontSize { get { return _SettingService.FontSize; } }
}
使用该视图模型,您可以轻松绑定到设置(在本例中为 FontSize)。
祝你好运。