- やりたいこと
Xamarin.Formsでデザインをコードに記述した場合に、
そのオブジェクトにアクセスする方法がわかりません。
C#
1Page GetTestPage() 2{ 3 return new ContentPage 4 { 5 Content = new StackLayout 6 { 7 Padding = 10, 8 Children = 9 { 10 new StackLayout 11 { 12 BackgroundColor = Color.Yellow, 13 Children = 14 { 15 new Entry 16 { 17 ClassId = "entText1", 18 Placeholder = "first message", 19 }, 20 new Label 21 { 22 ClassId="labMessage1", 23 BackgroundColor = Color.Navy, 24 }, 25 } 26 }, 27 new StackLayout 28 { 29 BackgroundColor = Color.Maroon, 30 Children = 31 { 32 new Entry 33 { 34 ClassId = "entText2", 35 Placeholder = "second message", 36 }, 37 new Label 38 { 39 ClassId="labMessage2", 40 BackgroundColor = Color.Navy, 41 }, 42 } 43 }, 44 new Button 45 { 46 Command = new Command(()=> 47 { 48 //entText1の内容をlabMessage1に表示 49 //entText2の内容をlabMessage2に表示 50 }) 51 }, 52 53 } 54 } 55 }; 56}
Childrenに追加する前にオブジェクトを生成してその変数を追加していれば、
ボタンクリックの中でアクセスすれば当然可能なのですが、
ふと気になったのでやってみたら、なかなかいいメソッドが見つかりませんでした。
- やってみたこと
FindByNameというメソッドを見つけたので上記のようにClassIdプロパティを指定し使ってみましたが、使い方が悪いのかうまくいきませんでした。
C#
1Command = new Command(()=> 2{ 3 this.FindByName<Label>("labMessage1").Text = this.FindByName<Entry>("entText1").Text; 4})
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/20 21:57