Xaml UWP-保存设置并非始终有效
我刚从中复制了以下代码。我想将一个双值绑定到xaml滑块,每次导航到SettingsPage时从localsetting获取该值,每次用户更改slidervalue时,我都希望将其保存到localsettings。以下是我目前的代码: 设置spage.xaml.cpp:Xaml UWP-保存设置并非始终有效,xaml,uwp,settings,c++-cx,Xaml,Uwp,Settings,C++ Cx,我刚从中复制了以下代码。我想将一个双值绑定到xaml滑块,每次导航到SettingsPage时从localsetting获取该值,每次用户更改slidervalue时,我都希望将其保存到localsettings。以下是我目前的代码: 设置spage.xaml.cpp: Windows::Storage::ApplicationDataContainer^ localSettings = Windows::Storage::ApplicationData::Current->LocalSe
Windows::Storage::ApplicationDataContainer^ localSettings = Windows::Storage::ApplicationData::Current->LocalSettings;
SettingsPage::SettingsPage()
{
InitializeComponent();
this->viewModel = ref new SettingsViewModel();
this->DataContext = this->viewModel;
}
void SettingsPage::QSlider_ValueChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::Primitives::RangeBaseValueChangedEventArgs^ e)
{
Windows::Storage::ApplicationDataCompositeValue^ composite =ref new Windows::Storage::ApplicationDataCompositeValue();
bool a = composite->Insert(SETTINGS_TAG_SLIDER_Q, dynamic_cast<PropertyValue^>(PropertyValue::CreateDouble((double)sldQ->Value)));
auto values = localSettings->Values;
bool b = values->Insert(SETTINGS_TAG_SETTINGS_PAGE, composite);
}
Windows::Storage::ApplicationDataContainer^localSettings=Windows::Storage::ApplicationData::Current->localSettings;
SettingsPage::SettingsPage()
{
初始化组件();
此->视图模型=参考新设置视图模型();
this->DataContext=this->viewModel;
}
void SettingsPage::QSlider\u ValueChanged(平台::对象^sender,Windows::UI::Xaml::控件::原语::RangeBaseValueChangedEventArgs ^e)
{
Windows::Storage::ApplicationDataCompositeValue^composite=ref新建Windows::Storage::ApplicationDataCompositeValue();
bool a=composite->Insert(设置、标签、滑块、动态转换(PropertyValue::CreateDouble((double)sldQ->Value));
自动值=本地设置->值;
bool b=值->插入(设置\标签\设置\页面,复合);
}
setingspage.xaml:
<Slider x:Name="sldQ" Margin="15,5,15,0" Value="{Binding SliderQValue}" ValueChanged="Slider_ValueChanged" MaxWidth="300" HorizontalContentAlignment="Left" ></Slider>
设置ViewModel.cpp:
double SettingsViewModel::SliderQValue::get()
{
Windows::Storage::ApplicationDataContainer^ localSettings = Windows::Storage::ApplicationData::Current->LocalSettings;
ApplicationDataCompositeValue^ composite = safe_cast<ApplicationDataCompositeValue^>(localSettings->Values->Lookup(SETTINGS_TAG_SETTINGS_PAGE));
if (composite != nullptr)
{
if (composite->HasKey(SETTINGS_TAG_SLIDER_Q)) {
double value = safe_cast<IPropertyValue^>(composite->Lookup(SETTINGS_TAG_SLIDER_Q))->GetDouble();
return value;
}
}
return 99;
}
双重设置viewmodel::SliderQValue::get()
{
Windows::Storage::ApplicationDataContainer^localSettings=Windows::Storage::ApplicationData::Current->localSettings;
ApplicationDataCompositeValue^composite=safe_cast(localSettings->Values->Lookup(SETTINGS_TAG_SETTINGS_PAGE));
if(复合!=nullptr)
{
如果(复合->HasKey(设置\u标记\u滑块\u Q)){
double value=safe_cast(复合->查找(设置_标记_滑块_Q))->GetDouble();
返回值;
}
}
返回99;
}
我的问题是,这只工作一次!如果我从其他页面导航到SettingsPage,我会得到slidervalue=99。然后我通过拖动到例如50来设置它。然后我导航回另一页。在另一个页面中,我再次导航到SettingsPage并获得slidervalue=50。但是再做一次,我又得到了99分。因此,它只适用于1个页面导航周期,但即使应用程序重新启动,它也应该可以工作。我的代码中有什么问题?我理解错了什么吗 事实上,我在老师的帮助下解决了这个问题。在上面的代码中,每当我想写/读时,我都会初始化一个新的“ApplicationDateCompositeValue”。因此,使用新方法,它的工作原理与计划的一样: OnValueChanged:
Windows::Storage::ApplicationDataContainer^ localSettings = Windows::Storage::ApplicationData::Current->LocalSettings;
auto values = localSettings->Values;
values->Insert(TAG_SLIDER, dynamic_cast<PropertyValue^>(PropertyValue::CreateDouble((double)sldQuality->Value)));
Windows::Storage::ApplicationDataContainer^localSettings=Windows::Storage::ApplicationData::Current->localSettings;
自动值=本地设置->值;
值->插入(标记滑块,动态转换(PropertyValue::CreateDouble((double)sldQuality->Value));
属性::get():
ApplicationDataContainer^localSettings=ApplicationData::Current->localSettings;
自动值=本地设置->值;
if(localSettings->Values->HasKey(标记滑块)){
双值=safe_cast(localSettings->Values->Lookup(TAG_滑块));
返回值;
}
其他的
返回默认值;
ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings;
auto values = localSettings->Values;
if (localSettings->Values->HasKey(TAG_SLIDER)) {
double value = safe_cast<double>(localSettings->Values->Lookup(TAG_SLIDER));
return value;
}
else
return default_value;