Created
May 26, 2011 11:31
-
-
Save kimoto/992961 to your computer and use it in GitHub Desktop.
SQCN - MainWindow.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Windows; | |
| using System.Windows.Controls; | |
| using System.Windows.Data; | |
| using System.Windows.Documents; | |
| using System.Windows.Input; | |
| using System.Windows.Media; | |
| using System.Windows.Media.Imaging; | |
| using System.Windows.Navigation; | |
| using System.Windows.Shapes; | |
| using Steam4NET; | |
| namespace SQCN | |
| { | |
| /// <summary> | |
| /// MainWindow.xaml の相互作用ロジック | |
| /// </summary> | |
| public partial class MainWindow : Window | |
| { | |
| private string dataFileName = "data.txt"; | |
| private List<Person> textFile2PersonList(string fileName, string encoding="UTF-8") | |
| { | |
| StreamReader reader = null; | |
| try | |
| { | |
| List<Person> personList = new List<Person>(); | |
| reader = new StreamReader(fileName, Encoding.GetEncoding(encoding)); | |
| while (true) | |
| { | |
| string line = reader.ReadLine(); | |
| if (line == null) | |
| { | |
| break; | |
| } | |
| Person person = new Person(); | |
| person.Name = line; | |
| personList.Add(person); | |
| } | |
| reader.Close(); | |
| return personList; | |
| } | |
| catch | |
| { | |
| return null; | |
| } | |
| finally | |
| { | |
| if (reader != null) | |
| reader.Close(); | |
| } | |
| } | |
| private bool personList2TextFile(List<Person> personList, string fileName) | |
| { | |
| StreamWriter writer = null; | |
| try | |
| { | |
| // 入力データ保存処理 | |
| writer = new StreamWriter(fileName); | |
| personList.ForEach(delegate(Person p) | |
| { | |
| writer.WriteLine(p.Name); | |
| }); | |
| writer.Close(); | |
| return true; | |
| } | |
| catch | |
| { | |
| return false; | |
| } | |
| finally | |
| { | |
| if (writer != null) | |
| writer.Close(); | |
| } | |
| } | |
| public MainWindow() | |
| { | |
| InitializeComponent(); | |
| SteamContext.Initialize(); | |
| List<Person> personList = new List<Person>(); | |
| if (personList == null) | |
| MessageBox.Show("設定ファイル読み込みエラー: "); | |
| else | |
| DataContext = textFile2PersonList(dataFileName); | |
| } | |
| private void Window_Loaded(object sender, RoutedEventArgs e) | |
| { | |
| } | |
| private void cancelButton_Click(object sender, RoutedEventArgs e) | |
| { | |
| List<Person> personList = (List<Person>)DataContext; | |
| if (!personList2TextFile(personList, dataFileName)) | |
| MessageBox.Show("設定ファイル書き込みエラー"); | |
| Close(); | |
| } | |
| private void okButton_Click(object sender, RoutedEventArgs e) | |
| { | |
| if (dataGrid.SelectedItem == null || dataGrid.SelectedItem == CollectionView.NewItemPlaceholder) | |
| { | |
| MessageBox.Show("変更後の名前を選択してください"); | |
| return; | |
| } | |
| Person person = (Person)dataGrid.SelectedItem; | |
| SteamContext.ClientFriends.SetPersonaName(person.Name); | |
| System.Media.SystemSounds.Asterisk.Play(); | |
| } | |
| } | |
| public class Person | |
| { | |
| public string Name { get; set; } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment