環境
Xamarin.Forms 3.2.0.839982
前提・実現したいこと
Xamarin.Formsで画面オープン時にコードビハインドを
使用せず特定のコントロールにフォーカスを当てたい
試した方法
とりあえずビヘイビアでなんとかしてみようとしたのが以下ソースですが
エラーはでなかったのですが、フォーカスは設定されませんでした
XAML
1<?xml version="1.0" encoding="utf-8" ?> 2<ContentPage 3 x:Class="BlankApp1.Views.LoginPage" 4 xmlns="http://xamarin.com/schemas/2014/forms" 5 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 6 xmlns:b="clr-namespace:BlankApp1.Behavior" 7 xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" 8 prism:ViewModelLocator.AutowireViewModel="True"> 9 <ContentPage.Behaviors> 10 <b:OpenForcusBehavior FocusControl="{Binding Source={x:Reference User}}" /> 11 </ContentPage.Behaviors> 12 13 <StackLayout Style="{StaticResource StackLayoutStyle}"> 14 <Entry x:Name="User" /> 15 </StackLayout> 16</ContentPage>
C#
1 public class OpenForcusBehavior : Behavior<ContentPage> 2 { 3 public object FocusControl { get; set; } 4 5 protected override void OnAttachedTo(ContentPage bindable) 6 { 7 bindable.Appearing += SetFocus; 8 } 9 10 private void SetFocus(object sender, EventArgs e) 11 { 12 var f = (Binding)FocusControl; 13 14 var s = f.Source as Entry; 15 16 s.Focus(); 17 } 18 }
ご教示よろしくおねがいします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/05/26 21:31
2019/05/27 16:29
退会済みユーザー
2019/05/28 13:36