ToggleSwitch.On & OffContent 未更新

ToggleSwitch.On & OffContent Not Updating

我正在尝试根据从 UWP 的本地设置缓存(不是 ToggleSwitch.IsOn 属性 ).请注意,我是在外部线程上执行此操作的。

这是我当前的代码:

ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
public async void updateUI()
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        // set Test1 state in UI
        toggleSwitchTest1.OnContent = (Convert.ToBoolean(localSettings.Values["Test1"])) ? "It is: On" : "It is: Off";
        toggleSwitchTest1.OffContent = toggleSwitchTest1.OnContent;

        // set Test2 state in UI
        toggleSwitchTest2.OnContent = (Convert.ToBoolean(localSettings.Values["Test2"])) ? "It is: On" : "It is: Off";
        toggleSwitchTest2.OffContent = toggleSwitchTest2.OnContent;

        // set Test3 state in UI
        toggleSwitchTest3.OnContent = (Convert.ToBoolean(localSettings.Values["Test3"])) ? "It is: On" : "It is: Off";
        toggleSwitchTest3.OffContent = toggleSwitchTest3.OnContent;
     });
}

我也尝试过使用正常的 Boolean 值作为参数传递给方法。或者 Boolean 值只是在 class 构造函数之前的 class 开头声明并设置它。这也没有用。

如果需要任何其他代码,请告诉我。

我做错了什么?

编辑

我通过像这样简单地调用它来在某处调用 updateUI 方法:updateUI()

编辑 2

ToggleSwitches'XAML构造函数代码:

<ToggleSwitch x:Name="toggleSwitchTest1" OffContent="It is" OnContent="It is" HorizontalAlignment="Left" Margin="19,84,0,0" VerticalAlignment="Top" IsOn="False"/>
<ToggleSwitch x:Name="toggleSwitchTest2" OffContent="It is" OnContent="It is" HorizontalAlignment="Left" Margin="19,116,0,0" VerticalAlignment="Top" IsOn="False"/>
<ToggleSwitch x:Name="toggleSwitchTest3" OffContent="It is" OnContent="It is" HorizontalAlignment="Left" Margin="19,148,0,0" VerticalAlignment="Top" IsOn="False"/>

它将获得 null 值,如果您从未将值应用到 localSettings.Values["xxx"]。如果值为 null,则 Convert.ToBoolean 方法 returns 为假。所有的 toggleSwitchTest OnConten 都会被设置 它是:Off

而且我发现你使用toggleSwitchTest1.OffContent = toggleSwitchTest1.OnContent;会导致OffContentOnConten变成相同的值

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
    var value = localSettings.Values["Test1"];
    // set Test1 state in UI
    toggleSwitchTest1.OnContent = (Convert.ToBoolean(localSettings.Values["Test1"])) ? "It is: On" : "It is: Off";
    // toggleSwitchTest1.IsOn = Convert.ToBoolean(localSettings.Values["Test1"]);
    toggleSwitchTest1.OffContent = !(Convert.ToBoolean(localSettings.Values["Test1"])) ? "It is: On" : "It is: Off";

    // set Test2 state in UI
    toggleSwitchTest2.OnContent = (Convert.ToBoolean(localSettings.Values["Test2"])) ? "It is: On" : "It is: Off";
    //  toggleSwitchTest2.IsOn= Convert.ToBoolean(localSettings.Values["Test2"]);
    toggleSwitchTest2.OffContent = !(Convert.ToBoolean(localSettings.Values["Test2"])) ? "It is: On" : "It is: Off";

    // set Test3 state in UI
    toggleSwitchTest3.OnContent = (Convert.ToBoolean(localSettings.Values["Test3"])) ? "It is: On" : "It is: Off";
    //   toggleSwitchTest3.IsOn= Convert.ToBoolean(localSettings.Values["Test3"]);
    toggleSwitchTest3.OffContent = !(Convert.ToBoolean(localSettings.Values["Test3"])) ? "It is: On" : "It is: Off";
});

虽然可以用上面的代码改变offon的内容,但是会导致逻辑混乱。我建议你在 xaml 之前开始和打开内容,并绑定 IsOn 属性 和 localsetting 值。

toggleSwitchTest1.IsOn = Convert.ToBoolean(localSettings.Values["Test1"]);

编辑 1

由于 AppServiceConnection 方法无法访问 UI,试图从这样的方法更改 UI(即使使用 Dispatcher) 不会有影响。

而是从类似 ApplicationData.Current.DataChanged 的方法或事件中调用它,您可以通过 ApplicationData.Current.SignalDataChange().

调用它