c# - ToggleSwitch.On & Off内容不更新

标签 c# multithreading uwp uwp-xaml toggleswitch

我正在尝试根据从 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;
     });
}

我还尝试使用普通的 bool 值作为传递到方法中的参数来执行此操作。或者只是在类的开头在公共(public)构造函数之前声明并设置它的 bool 值。这也不起作用。

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

我做错了什么?

编辑

我在某处调用 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"/>

最佳答案

如果您从未将值应用于localSettings.Values["xxx"],它将获得null值。如果值为 null,则 Convert.ToBoolean方法返回 false。所有的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 值绑定(bind)。

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

编辑 1

由于 AppServiceConnection 方法无法访问 UI,因此尝试通过此类方法更改 UI(即使使用 Dispatcher)也不会成功有效果。

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

关于c# - ToggleSwitch.On & Off内容不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49730690/

相关文章:

c# - "Catastrophic failure"on MapControl tap with Windows Phone 10 SDK

c# - TextHighlighter RichTextBlock UWP

c# - C# 中的接口(interface)实现语法选项

c# - Richtextbox.invoke,C#,窗体仍挂

c# - 在vb.net或c#中将curl转换为webrequest

c# - UWP - 比较 JSON 和数据库上的数据

java - javax.swing.SwingWorker 中的问题

android - Android上下文线程检查

c# - 在 C# 控制台应用程序中使用线程概念并行执行超过 2 个 Dtsx 包

c# - 当 UWP TextBlock 移出其容器时对其进行剪辑