やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Raspbian stretch MonoDevelop Eto.Forms XAML TextBox 同士でデータ・バインディングできた(実用性なし)

 型が同じならできた。

成果物

対象環境

前回

手順

  1. プロジェクト作成
  2. ソースコード作成
  3. 実行

1. プロジェクト作成

  1. メニュー→ファイル新しいソリューション
    f:id:ytyaru:20181201194454p:plain
  2. マルチプラットフォームアプリEto Application f:id:ytyaru:20181202083840p:plain
  3. 名前などを適当に入力し、Xamlを選択する
    f:id:ytyaru:20181202083922p:plain
    f:id:ytyaru:20181202083950p:plain
  4. 場所を入力する
    f:id:ytyaru:20181201194509p:plain
  5. プロジェクトが作成される
    f:id:ytyaru:20181202084143p:plain

2. ソースコード作成

MainForm.xeto

 ポイントはText="{Binding Url}"。このUrlは後述するモデルクラスの公開プロパティ名。

<feff><?xml version="1.0" encoding="UTF-8"?>
<Form xmlns="http://schema.picoe.ca/eto.forms" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="My Eto Form" ClientSize="400, 350" Padding="10">
    <StackLayout>
        <TextBox x:Name="textBox1" Text="{Binding Url}" />
        <TextBox x:Name="textBox2" Text="{Binding Url}" />
    </StackLayout>
</Form>``````

#### UrlModel.cs

 TextBoxのモデルクラス。ただ文字列をそのままget, setするだけ。

using System; using System.ComponentModel; // INotifyPropertyChanged using System.Runtime.CompilerServices; // CallerMemberName

namespace HelloEtoXamlBindingTextBox { public class UrlModel : INotifyPropertyChanged { String url; public String Url { get { return url; } set { this.url = value; OnPropertyChanged(); } }
void OnPropertyChanged([CallerMemberName] string memberName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(memberName)); } public event PropertyChangedEventHandler PropertyChanged; } }

#### MainForm.xeto.cs

 モデルクラスをWindowのDataContextにセットしただけ。

using System; using System.Collections.Generic; using Eto.Forms; using Eto.Drawing; using Eto.Serialization.Xaml;

namespace HelloEtoXamlBindingTextBox { public class MainForm : Form { public MainForm() { XamlReader.Load(this); var model = new UrlModel(); DataContext = model; } } }

## 3. 実行

1. Ctrl+F5で実行
1. 怒られた  
[f:id:ytyaru:20181202084218p:plain]  
1. ファイルパスを辿ってexeファイルを直接叩くと実行できた  
[f:id:ytyaru:20181204080023p:plain]

 2つのテキストボックスのうち一方が変更されると、他方も変更される。バインディングできている。

# 所感

 異なる型の場合、どうやったらいいの? [公式](https://github.com/picoe/Eto/wiki/Data-Binding)を見てもうまくできない。`.Convert()`のところだとは思うけど。