[UWP] UWP保存RadioButton用户选择状态 [英] [UWP]UWP Saving RadioButton user selection states

查看:229
本文介绍了[UWP] UWP保存RadioButton用户选择状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个UWP应用程序,用户将通过RadioButton选择一些设置。如何保存这些设置,以便即使在重新启动应用程序后也可以看到它们。目前我使用LocalSettings来存储状态。但每次打开Settings
页面时,都会根据LocalSettings中的值自动触发Checked事件。我不希望这种情况发生。


我希望Checked事件仅在用户进行选择时触发。请注意这是正确的方法。


您可以引用Windows 10的设置中的一些示例。它们如何保留并显示用户选择?

解决方案

Hi Satish12321,


我的建议是你可以创建一个名为"IsFirstIn"的全局变量。  它是一个bool值,默认值为"IsFirstIn"为false。输入Checked事件时,您可以先
检查此值。如果为false,则将其更改为true并返回。所以接下来的步骤将不会执行。下次单击其他单选按钮时,"IsFirstIn"为真,它可以在Checked事件中执行其他代码。


您可以参考以下代码作为参考:


< p style ="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none">
  

 public sealed partial class MainPage:Page 
{
bool IsFirstIn;
public MainPage()
{
this.InitializeComponent();

ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
string value = localSettings.Values [" myTest"] as string;
bool myValue = Convert.ToBoolean(value);
myRadioButton.IsChecked = myValue;
Debug.WriteLine(" init");
}
private void MyButton_Checked(object sender,RoutedEventArgs e)
{
if(!IsFirstIn)
{
Debug.WriteLine(" Success" );
IsFirstIn = true;
返回;
}
Debug.WriteLine(" Checked");
}
}



最好的问候


Roy Li


I am designing an UWP app where user will select some settings through RadioButton. How to save those settings so that it will be visible even after restarting the application. Currently I am using LocalSettings to store the states. But every time the Settings page is opened, the Checked event is fired automatically based upon the values in LocalSettings. I don't want this to happen.

I want the Checked event to fire only when users makes a selection. Kindly throw some light on this as what is the right way of doing this.

You can cite some examples from the Settings of Windows 10. How do they retain and show the user selections?

解决方案

Hi Satish12321,

My suggestion is that you could create a global variable called "IsFirstIn".  It is a bool value and the default value of "IsFirstIn" is false. You could check this value first when enter the Checked event. If it is false, change it to true and return. So the next steps won’t be executed. Next time when you click other radiobuttons, the "IsFirstIn" is true and it can execute other codes in the Checked event.

You could refer the following code as reference:

  

  public sealed partial class MainPage : Page
    {
        bool IsFirstIn;
        public MainPage()
        {
            this.InitializeComponent();
            
            ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
            string value = localSettings.Values["myTest"] as string;
            bool myValue = Convert.ToBoolean(value);
            myRadioButton.IsChecked = myValue;
            Debug.WriteLine("init");
        }
        private void MyButton_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsFirstIn)
            {
                Debug.WriteLine("Success");
                IsFirstIn = true;
                return;
            }
            Debug.WriteLine("Checked");
        }
    }

Best Regards

Roy Li


这篇关于[UWP] UWP保存RadioButton用户选择状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆