我试图基于从UWP的本地设置缓存(不是OnContent
属性)检索的值来更新三个ToggleSwitches的OffContent
和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
值只是在类的开头在公共构造函数之前声明并进行设置。这也不起作用。请让我知道是否需要其他代码。
我究竟做错了什么?
编辑
我通过简单地调用它来在某处调用
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"]
值。如果value为null,则Convert.ToBoolean
方法返回false。所有toggleSwitchTest OnConten
将被设置为:关。
我发现您使用toggleSwitchTest1.OffContent = toggleSwitchTest1.OnContent;
它将导致OffContent
和OnConten
变为相同的值。
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";
});
尽管您可以使用上述代码更改
off
和on
内容,但这将导致逻辑混乱。我建议您在xaml之前的内容中进行切换,然后将IsOn
属性与localsetting值绑定。toggleSwitchTest1.IsOn = Convert.ToBoolean(localSettings.Values["Test1"]);
编辑1
由于
AppServiceConnection
方法无法访问UI,因此尝试从这样的方法更改UI(即使使用Dispatcher
时)也无效。而是从可以通过
ApplicationData.Current.DataChanged
调用的方法或事件(如ApplicationData.Current.SignalDataChange()
)中调用它。