やってみる

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

Raspbian stretch MonoDevelop Eto.Forms XAML TextBox.Text と WebView.Url のデータ・バインディングができない2

 IValueConverterはEto.Forms 2.4.0以降で存在した。が、他でつまづく。

対象環境

前回

参考

手順

  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

 xmlns:local, <Window.Resources>が問題の箇所。そもそもWPFの記法なのだが、これはEto.Formsでも使えるのか?

MainForm.xeto

<?xml version="1.0" encoding="UTF-8"?>
<Form xmlns="http://schema.picoe.ca/eto.forms" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MainForm" Title="My Eto Form" ClientSize="800, 600" Padding="10">
    <Window.Resources>
        <local:StringUriConverter x:Key="StringUriConverter" />
    </Window.Resources>
    <StackLayout>
        <TextBox x:Name="textBox1" Width="800" />
        <WebView x:Name="webView1" Width="800" Height="600" Url="{Binding ElementName=textBox1, Path=Text, Converter={StaticResource StringUriConverter}}" />
    </StackLayout>>
</Form>

MainForm.xeto.cs

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

namespace HelloEtoXamlBindingTextBoxWebView
{
    public class MainForm : Form
    {
        public MainForm()
        {
            XamlReader.Load(this);
            DataContext = new UrlViewModel();
        }
    }
}

StringUriConverter.cs

 IValueConverter。よく調べたらEto.Formsにも存在した。

using System;
using Eto.Forms;
namespace HelloEtoXamlBindingTextBoxWebView
{
    public class StringUriConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //return ((string?)value == true) ? new Uri(value as string) : value;
            //var v = value as string;
            return new Uri(value as string);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //throw new NotImplementedException();
            return (value as string).ToString();
        }
    }
}

UrlViewModel.cs

 モデル。

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

namespace HelloEtoXamlBindingTextBoxWebView
{
    public class UrlViewModel : 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;
    }
}

3. 実行

  1. Ctrl+F5で実行
  2. 怒られた
    f:id:ytyaru:20181202084218p:plain
  3. ファイルパスを辿ってexeファイルを直接叩くと強制終了する…… f:id:ytyaru:20181204080023p:plain

所感

 WPFの情報ならあるが、Eto.Formsにコードをコピーしても動かない……。