Skip to content

Instantly share code, notes, and snippets.

View peyangu's full-sized avatar

peyangu peyangu

View GitHub Profile
@peyangu
peyangu / Program.cs
Last active March 14, 2017 15:54
FizzBuzz問題(引数渡したバージョン)
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
FizzBuzz(4, 7, 120);
}
@peyangu
peyangu / Program.cs
Last active March 14, 2017 15:55
FizzBuzz問題(単純)
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
FizzBuzz();
}
@peyangu
peyangu / OnClosing.cs
Created March 15, 2017 06:35
既存のOnClosingメソッド
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
// 何か処理追加
}
@peyangu
peyangu / MainWindow.xaml.cs
Created March 15, 2017 06:36
CloseActionプロパティにthis.Close()の処理を渡す
public MainWindow()
{
InitializeComponent();
var mView = new ViewModel.MainViewModel();
this.All.DataContext = mView;
// OnClose時に処理を行いたいので、Model側で閉じられるようthis.CloseをVMに渡す。
if(mView.Model.CloseAction == null)
mView.Model.CloseAction = new Action(() => this.Close());
}
@peyangu
peyangu / MainModel.cs
Created March 15, 2017 06:37
追加したい処理とCloseActionの呼び出し
// 閉じる処理プロパティ
public Action CloseAction { get; set; }
public void OnClosing()
{
// 追加したい処理
CloseAction();
}
@peyangu
peyangu / MainViewModel.cs
Created March 15, 2017 06:38
ICommandのCloseにModelクラスのOnClosingメソッドを紐づける
public ICommand Close { get; private set; }
public MainViewModel()
{
Close = new RelayCommand(_Model.OnClosing);
}
@peyangu
peyangu / MainWindow.xaml
Created March 15, 2017 06:39
ボタンのCommandにCloseをバインドする
<Button x:Name="CloseButton" Content="r" Style="{DynamicResource CaptionButtonStyleKey}" Command="{Binding Close}">
@peyangu
peyangu / Person.cs
Created March 15, 2017 07:26
Personクラス
publi class Person
{
public string Name { get; set; }
public string Age { get; set; }
}
@peyangu
peyangu / XmlController.cs
Created March 15, 2017 07:26
XMLに書き込む処理
public void SaveXml(List<Person> personList)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Person[]));
// Data.xmlは保存するファイル名
using (System.IO.FileStream fs = new System.IO.FileStream("Data.xml", System.IO.FileMode.Create))
{
serializer.Serialize(fs, personList.ToArray());
}
@peyangu
peyangu / XmlController.cs
Created March 15, 2017 07:27
XMLから読み込む処理
public List<Person> LoadXml()
{
Person[] loadAry;
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Person[]));
using (System.IO.StreamReader sr = new System.IO.StreamReader("Data.xml", new System.Text.UTF8Encoding(false)))
{
loadAry = (Person[])serializer.Deserialize(sr);
}