private void ReadPortSetup(SharedPreferences sp) { //读取选中的端口 if (_hasPorts) { //判断当前端口的数量,小于则表示是OK。 if (sp.GetInt32("selectedPort", 0) < comboBox_port.Items.Count) { comboBox_port.SelectedIndex = sp.GetInt32("selectedPort", 0); } else { comboBox_port.SelectedIndex = 0; } } //读取设置的波特率 comboBox_baudrate.SelectedIndex = sp.GetInt32("baudRate", 0); //读取设置的校验方式 comboBox_parity.SelectedIndex = sp.GetInt32("parity", 0); //读取设置的数据位 comboBox_databit.SelectedIndex = sp.GetInt32("dataBits", 0); //读取设置的停止位 comboBox_stopbit.SelectedIndex = sp.GetInt32("stopBits", 0); //读取自动发送数据时间间隔 textBox_sendPeriod.Text = sp.GetInt32("intervalTime", 500).ToString(); //读取接收方式 checkBox_receiveHex.Checked = sp.GetBoolean("acceptChar", true); //读取标志变量,即是否在接收框中显示信息。 showInfo = sp.GetBoolean("showInfo", true); button_receivepause.Text = showInfo ? "暂停接收显示" : "继续接收显示"; }
/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); switch (typeCode) { case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = SharedPreferences.GetLong(key, Convert.ToInt64(defaultValue)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue)); break; case TypeCode.UInt16: value = SharedPreferences.GetInt(key, Convert.ToUInt16(defaultValue)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue)); break; case TypeCode.Double: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; } return((null != value) ? (T)value : defaultValue); } }
// =========================================================== // Methods // =========================================================== public bool IsFirstTime(/* final */ String pKey) { /* final */ SharedPreferences prefs = this.GetPreferences(FileCreationMode.Private); if (prefs.GetBoolean(pKey, true)) { prefs.Edit().PutBoolean(pKey, false).Commit(); return(true); } return(false); }
private void ReadPIDSetup(SharedPreferences sp) { //保存的车型 radioButton_FourWheel.Checked = sp.GetBoolean("radioButton_carType", true); if (!radioButton_FourWheel.Checked) radioButton_BalanceCar.Checked = true; if (radioButton_FourWheel.Checked) //四轮车PID ReadPIDFourWheel(sp); else if (radioButton_BalanceCar.Checked) //直立车PID参数的获取 ReadPIDBalanceCar(sp); }
private void ReadPIDSetup(SharedPreferences sp) { //保存的车型 radioButton_FourWheel.Checked = sp.GetBoolean("radioButton_carType", true); if (!radioButton_FourWheel.Checked) { radioButton_BalanceCar.Checked = true; } if (radioButton_FourWheel.Checked) //四轮车PID { ReadPIDFourWheel(sp); } else if (radioButton_BalanceCar.Checked) //直立车PID参数的获取 { ReadPIDBalanceCar(sp); } }
/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); bool resave = false; switch (typeCode) { case TypeCode.Decimal: //Android doesn't have decimal in shared prefs so get string and convert var savedDecimal = string.Empty; try { savedDecimal = SharedPreferences.GetString(key, string.Empty); } catch (Java.Lang.ClassCastException cce) { Console.WriteLine("Settings 1.5 change, have to remove key."); try { Console.WriteLine("Attempting to get old value."); savedDecimal = SharedPreferences.GetLong(key, (long)Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture)).ToString(); Console.WriteLine("Old value has been parsed and will be updated and saved."); } catch (Java.Lang.ClassCastException cce2) { Console.WriteLine("Could not parse old value, will be lost."); } Remove("key"); resave = true; } if (string.IsNullOrWhiteSpace(savedDecimal)) { value = Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture); } else { value = Convert.ToDecimal(savedDecimal, System.Globalization.CultureInfo.InvariantCulture); } if (resave) { AddOrUpdateValue(key, value); } break; case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = (Int64)SharedPreferences.GetLong(key, (long)Convert.ToInt64(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Double: //Android doesn't have double, so must get as string and parse. var savedDouble = string.Empty; try { savedDouble = SharedPreferences.GetString(key, string.Empty); } catch (Java.Lang.ClassCastException cce) { Console.WriteLine("Settings 1.5 change, have to remove key."); try { Console.WriteLine("Attempting to get old value."); savedDouble = SharedPreferences.GetLong(key, (long)Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture)).ToString(); Console.WriteLine("Old value has been parsed and will be updated and saved."); } catch (Java.Lang.ClassCastException cce2) { Console.WriteLine("Could not parse old value, will be lost."); } Remove(key); resave = true; } if (string.IsNullOrWhiteSpace(savedDouble)) { value = Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture); } else { value = Convert.ToDouble(savedDouble, System.Globalization.CultureInfo.InvariantCulture); } if (resave) { AddOrUpdateValue(key, value); } break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; default: if (defaultValue is Guid) { var outGuid = Guid.Empty; Guid.TryParse(SharedPreferences.GetString(key, Guid.Empty.ToString()), out outGuid); value = outGuid; } else { throw new ArgumentException(string.Format("Value of type {0} is not supported.", value.GetType().Name)); } break; } return(null != value ? (T)value : defaultValue); } }
private void Init_pane_Scope() { var sp = new SharedPreferences(SavefileName); panel_Scope.AutoScroll = true; var total = ScopeNumber; for (var i = 0; i < total; i++) { var checkDrawing = new CheckBox(); checkDrawing.Size = new Size(15, 15); checkDrawing.Location = new Point(20 + i%8*77, 10 + i/8*60); checkDrawing.Name = "checkBox_Def" + Convert.ToString(i + 1); checkDrawing.Text = ""; var checkState = string.Format("SCOPE_CheckState{0}", i + 1); checkDrawing.Checked = sp.GetBoolean(checkState, false); checkDrawing.CheckedChanged += CheckDrawingOnCheckedChanged; var txtBoxName = new TextBox(); txtBoxName.Size = new Size(50, 20); //textbox大小 txtBoxName.Location = new Point(41 + i%8*77, 7 + i/8*60); txtBoxName.Name = "txtName" + Convert.ToString(i + 1); txtBoxName.TextAlign = HorizontalAlignment.Center; var txtName_CustomPara = string.Format("SCOPE_TextName{0}", i + 1); var getName = sp.GetString(txtName_CustomPara, @"波形" + Convert.ToString(i + 1)); txtBoxName.Text = getName.Trim() != "" ? getName : string.Format("波形{0}", i + 1); txtBoxName.BackColor = _colorLine[i%8]; txtBoxName.TextChanged += ScopeTextNameChanged; txtBoxName.Enabled = checkDrawing.Checked; var buttonNewForm = new Button(); buttonNewForm.Size = new Size(66, 23); buttonNewForm.Location = new Point(25 + i%8*77, 34 + i/8*60); buttonNewForm.Name = "buttonDraw" + Convert.ToString(i + 1); buttonNewForm.Text = @"独立图像"; buttonNewForm.Click += ButtonNewFormOnClick; buttonNewForm.Enabled = checkDrawing.Checked; panel_Scope.Controls.Add(checkDrawing); panel_Scope.Controls.Add(txtBoxName); panel_Scope.Controls.Add(buttonNewForm); } }
private void AddControlsToPannel(SharedPreferences sp, int start, int end, bool byInit) { for (var i = start; i < end; i++) { var checkboxSelect = new CheckBox(); checkboxSelect.Size = new Size(50, 20); checkboxSelect.Location = new Point(30 + 70*0, 30*(i + 1)); checkboxSelect.Name = "checkBox_Def" + Convert.ToString(i + 1); checkboxSelect.Text = (i + 1).ToString(); if (byInit) { var checkState = string.Format("DIY_CheckState{0}", i + 1); checkboxSelect.Checked = sp.GetBoolean(checkState, true); } checkboxSelect.CheckedChanged += CheckboxSelectOnCheckedChanged; var txtBoxName = new TextBox(); txtBoxName.Size = new Size(50, 50); //textbox大小 txtBoxName.Location = new Point(30 + 70*1, 30*(i + 1)); txtBoxName.Name = @"txtName" + Convert.ToString(i + 1); txtBoxName.Text = @"Name" + Convert.ToString(i + 1); txtBoxName.TextAlign = HorizontalAlignment.Center; if (byInit) { var txtName_CustomPara = string.Format("DIY_TextName{0}", i + 1); txtBoxName.Text = sp.GetString(txtName_CustomPara, "数据"); } else { txtBoxName.Enabled = false; } var txtBoxValue = new TextBox(); txtBoxValue.Size = new Size(50, 50); //textbox大小 txtBoxValue.Location = new Point(30 + 70*2, 30*(i + 1)); txtBoxValue.Name = "txtValue" + Convert.ToString(i + 1); txtBoxValue.Text = @"1.0"; txtBoxValue.TextAlign = HorizontalAlignment.Center; if (byInit) { var txtValue_CustomPara = string.Format("DIY_TextValue{0}", i + 1); txtBoxValue.Text = sp.GetString(txtValue_CustomPara, "1.0"); } else { txtBoxValue.Enabled = false; } txtBoxValue.TextChanged += DIYTextboxValueChanged; var submitButton = new Button(); submitButton.Size = new Size(50, 20); //textbox大小 submitButton.Location = new Point(30 + 70*3, 30*(i + 1)); submitButton.Name = "buttonSubmit" + Convert.ToString(i + 1); submitButton.Text = @"修改"; submitButton.Click += SubmitButtonOnClick; if (!byInit) { submitButton.Enabled = false; } if (!checkboxSelect.Checked) { txtBoxName.Enabled = false; txtBoxValue.Enabled = false; submitButton.Enabled = false; } panel_add_DIYControls.Controls.Add(checkboxSelect); panel_add_DIYControls.Controls.Add(txtBoxName); panel_add_DIYControls.Controls.Add(txtBoxValue); panel_add_DIYControls.Controls.Add(submitButton); } }
/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); switch (typeCode) { case TypeCode.Decimal: value = (decimal)SharedPreferences.GetLong(key, (long)Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = (Int64)SharedPreferences.GetLong(key, (long)Convert.ToInt64(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Double: value = (double)SharedPreferences.GetLong(key, (long)Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; default: if (defaultValue is Guid) { var outGuid = Guid.Empty; Guid.TryParse(SharedPreferences.GetString(key, Guid.Empty.ToString()), out outGuid); value = outGuid; } else { throw new ArgumentException(string.Format("Value of type {0} is not supported.", value.GetType().Name)); } break; } return(null != value ? (T)value : defaultValue); } }
/// <summary> /// 载入应用程序保存的配置信息。并设置相关控件的属性等。 /// </summary> private void LoadApplicationSettings() { if (File.Exists(@"配置文件夹\Application.config")) { try { //读取应用程序配置信息。 SharedPreferences sp = new SharedPreferences(@"配置文件夹\Application.config", true); //读取并设置是否保存配置 saveConfig = sp.GetBoolean("saveConfig", true); saveSongList = sp.GetBoolean("saveSongList", true); isFirstTimeRunning = sp.GetBoolean("isFirstTimeRunning", true); userName = sp.GetString("userName", ""); if (File.Exists(@"配置文件夹\songlist.xml") && saveSongList) { LoadSongListPath(); } if (saveConfig) { //读取并设置窗口透明度 BorderImage.Opacity = sp.GetDouble("windowOpacity", 1d); borderOpacity = BorderImage.Opacity; //读取并设置是否显示频谱图 showSpectrum = sp.GetBoolean("showSpectrum", true); //读取并设置是否自动加载歌词文件 autoLoadLyricFile = sp.GetBoolean("autoLoadLyricFile", true); //读取并设置歌词文件的编码方式 string str = sp.GetString("lyricFileEncoding", "null"); if (str != "null") { encoding = Encoding.GetEncoding(Int32.Parse(str)); } //读取并设置桌面歌词字体大小 dskLrcFontSize = sp.GetInt32("dskLyricFontSize", 40); string tempStr = string.Empty; //读取并设置桌面歌词字体 tempStr = sp.GetString("dskLyricFontFamily", "null"); if (tempStr != "null" && tempStr != "") { dskLrcFontFamily = tempStr; } // tempStr = sp.GetString("dskLyricFontStyle", "null"); if (tempStr != "null" && tempStr != "") { dskLrcFontStyle = tempStr; } //读取并设置已播放歌词颜色 tempStr = sp.GetString("playedLyricForeColor", "null"); if (tempStr != "null") { playedForecolor = (Color)(ColorConverter.ConvertFromString(tempStr)); } //读取并设置未播放歌词的颜色 tempStr = sp.GetString("unplayedLyricForeColor", "null"); if (tempStr != "null") { unplayedForecolor = (Color)(ColorConverter.ConvertFromString(tempStr)); } //读取并设置歌名前景色 tempStr = sp.GetString("songNameForeColor", "null"); if (tempStr != "null") { nowPlayingSong.Foreground = GetBrushFromString(tempStr); } //读取并设置标题前景色 tempStr = sp.GetString("windowTitleForeColor", "null"); if (tempStr != "null") { WindowTitle.Foreground = GetBrushFromString(tempStr); } //读取并设置桌面歌词未播放颜色 tempStr = sp.GetString("dskLrcUnplayedForeColor", "null"); if (tempStr != "null") { dskLrcUnplayedForecolor = (Color)(ColorConverter.ConvertFromString(tempStr)); } //读取并设置桌面歌词已播放的 tempStr = sp.GetString("dskLrcPlayedForeColor", "null"); if (tempStr != "null") { dskLrcPlayedForecolor = (Color)(ColorConverter.ConvertFromString(tempStr)); } //读取并设置标题颜色 tempStr = sp.GetString("titleForeColor", "null"); if (tempStr != "null") { titleTB.Foreground = GetBrushFromString(tempStr); } //读取并设置列表项目颜色 tempStr = sp.GetString("unselectedItemForeColor", "null"); if (tempStr != "null") { songListLB.Foreground = GetBrushFromString(tempStr); } //读取并更换背景图片。 borderImagePath = sp.GetString("borderBackImagePath", "null"); if (borderImagePath != "null") { ChangeBorderBackImage(borderImagePath); } //读取并设置为上次关闭时的音量。 this.volumeValuePrb.Value = sp.GetDouble("volume", 50); //读取并设置是否显示桌面歌词 showDesktopLyricItem.IsChecked = sp.GetBoolean("showDskLyric", true); //读取并设置为上次关闭时设置的播放模式 playmodeIndex = sp.GetInt32("playmodeIndex", -1) - 1; PlayerButtonClickDown(playmodeBt); #region 播放器状态还原代码。有点烦。。。 rememberExitPosition = sp.GetBoolean("remeberExitPosition", true); if (songListLB.Items.Count != 0) { if (rememberExitPosition) { if (saveSongList && saveConfig) { if (sp.GetInt32("nowplayingIndex", -2) != -2) { //读取并设置为上次关闭时播放器状态 songListLB.SelectedIndex = sp.GetInt32("nowplayingIndex", -1); //进行播放 PlayerButtonClickDown(mediaControlBt); bassPlayer.ChannelPosition = TimeSpan.FromMilliseconds(sp.GetDouble("pausePosition", 0)); GetLyricIndex(bassPlayer.ChannelPosition.TotalMilliseconds); //再暂停,等待用户操作。 PlayerButtonClickDown(mediaControlBt); } } } } #endregion } } catch (Exception) { try { File.Delete(@"配置文件夹\Application.config"); MessageBox.Show("Sorry, 检测到配置文件损坏,因为格式有误,无法读取。已强制删除配置文件。"); } catch (Exception) { } } } if (isFirstTimeRunning) { //表示是第一次运行程序。 //WelcomeToUse(string.Format("", userName)); isFirstTimeRunning = false; //TestFunc(); } }
private void ReadPortSetup(SharedPreferences sp) { //读取选中的端口 if (_hasPorts) { //判断当前端口的数量,小于则表示是OK。 if (sp.GetInt32("selectedPort", 0) < comboBox_port.Items.Count) comboBox_port.SelectedIndex = sp.GetInt32("selectedPort", 0); else { comboBox_port.SelectedIndex = 0; } } //读取设置的波特率 comboBox_baudrate.SelectedIndex = sp.GetInt32("baudRate", 0); //读取设置的校验方式 comboBox_parity.SelectedIndex = sp.GetInt32("parity", 0); //读取设置的数据位 comboBox_databit.SelectedIndex = sp.GetInt32("dataBits", 0); //读取设置的停止位 comboBox_stopbit.SelectedIndex = sp.GetInt32("stopBits", 0); //读取自动发送数据时间间隔 textBox_sendPeriod.Text = sp.GetInt32("intervalTime", 500).ToString(); //读取接收方式 checkBox_receiveHex.Checked = sp.GetBoolean("acceptChar", true); //读取标志变量,即是否在接收框中显示信息。 showInfo = sp.GetBoolean("showInfo", true); button_receivepause.Text = showInfo ? "暂停接收显示" : "继续接收显示"; }