型が同じならできた。
成果物
対象環境
- Raspbierry pi 3 Model B+
- Raspbian stretch(9.0) 2018-06-27
- Mono 5.16.0
- MonoDevelop 7.6 build 711
- Eto.Forms 2.4.1 拡張機能, NuGetパッケージ
前回
- http://ytyaru.hatenablog.com/entry/2020/01/30/000000
- http://ytyaru.hatenablog.com/entry/2020/01/29/000000
- http://ytyaru.hatenablog.com/entry/2020/01/28/000000
- http://ytyaru.hatenablog.com/entry/2020/01/27/000000
- http://ytyaru.hatenablog.com/entry/2020/01/26/000000
- http://ytyaru.hatenablog.com/entry/2020/01/25/000000
- http://ytyaru.hatenablog.com/entry/2020/01/24/000000
手順
- プロジェクト作成
- ソースコード作成
- 実行
1. プロジェクト作成
- メニュー→
ファイル
→新しいソリューション
マルチプラットフォーム
→アプリ
→Eto Application
- 名前などを適当に入力し、
Xaml
を選択する
場所
を入力する
- プロジェクトが作成される
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()`のところだとは思うけど。