【问题标题】:UWP - Saving Settings does not work all the timeUWP - 保存设置并非一直有效
【发布时间】:2018-02-14 22:59:42
【问题描述】:

我刚刚从here 复制了以下代码。我想将双精度值绑定到 xaml 滑块,每次导航到 SettingsPage 时从 localsetting 获取此值,并且每次滑块值被用户更改时,我希望将其保存到 localsettings。到目前为止,这是我的代码:

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

我的问题是这只能工作一次!如果我从其他页面导航到 SettingsPage,我会得到 slidervalue=99。然后我通过拖动到例如设置它50. 然后我导航回到其他页面。从另一个页面我再次导航到 SettingsPage 并获得 slidervalue=50。但是再做一次,我又得到了 99。因此它仅适用于 1 页导航周期,但即使重新启动应用程序也应该有效。我的代码有什么问题?我理解错了吗?

【问题讨论】:

    标签: xaml uwp settings c++-cx


    【解决方案1】:

    我实际上在this 的帮助下解决了这个问题。在我上面的代码中,每次我想写/读它时,我都会初始化一个新的“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)));
    

    属性::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;
    

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多