UWP - Saving Settings does not work all the time

246 Views Asked by At

I've got the following code just copied from here. I want to bind a double value to a xaml slider, get this value from the localsetting every time I navigate to the SettingsPage and everytime the slidervalue gets changed by the user I want it to be saved to localsettings. Here is my code so far:

SettingsPage.xaml.cpp:

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);
}

SettingsPage.xaml:

<Slider x:Name="sldQ" Margin="15,5,15,0" Value="{Binding SliderQValue}" ValueChanged="Slider_ValueChanged" MaxWidth="300" HorizontalContentAlignment="Left" ></Slider>

SettingsViewModel.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;
}

My Problem is that this works exactly once! If I navigate from other pages to SettingsPage, I get slidervalue=99. Then I set it by dragging to e.g. 50. Then I navigat back to other page. From the other page I navigate again to SettingsPage and get slidervalue=50. But doing it once again I get 99 again. So it only works for 1 page navigation-cycle but it should work even if the app is rebooted. What is the problem in my code? Am I understanding something wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

I actually solved the problem with the help of this. In my code above I was initializing a new 'ApplicationDateCompositeValue' each time I wanted to write/read it. So with the new method it works like it was planned to do:

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)));

Property::get():

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;