Xamarin 中 ContentPage 上另一个控件内的 FindByName 控件

FindByName control inside another control on ContentPage in Xamarin

我想在他出现时将焦点设置在 SearchBar 上。问题是 SearchBar 位于弹出视图内,我需要从 ViewModel 访问他。

以标准方式我会使用

Xamarin.Forms.SearchBar tmp_SearchBar = this.Page.FindByName("fro_SearchBar_NewItem") as Xamarin.Forms.SearchBar;

但这不再有效了。

这里是XAML

<sfPopup:SfPopupLayout x:Name="fro_Popup_NewItem" Opened="fro_Popup_NewItem_Opened" Grid.Row="1" HorizontalOptions="Center" VerticalOptions="Center" BackgroundColor="Black">
    <sfPopup:SfPopupLayout.PopupView>
        <sfPopup:PopupView  BackgroundColor="Black" WidthRequest ="400" HeightRequest ="100" ShowFooter="False" ShowHeader="False">
            <sfPopup:PopupView.ContentTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <!--Search bar-->
                        <Grid Grid.Row="0" HorizontalOptions="Center" VerticalOptions="Center">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                                <SearchBar  x:Name="fro_SearchBar_NewItem"
                                            Grid.Column="0"
                                            Text="{Binding SearchText_Popup, Mode=TwoWay}"
                                            SearchCommand="{Binding SearchCommand}"
                                            Placeholder="Find" 
                                            CancelButtonColor="White" 
                                            TextColor="White" 
                                            PlaceholderColor="Gray"/>

                        </Grid>
                  </Grid>
                 </DataTemplate>
            </sfPopup:PopupView.ContentTemplate>
        </sfPopup:PopupView>
    </sfPopup:SfPopupLayout.PopupView>
</sfPopup:SfPopupLayout>

嵌套的 FindByName 也不起作用。

Syncfusion.XForms.PopupLayout.SfPopupLayout tmp_Popup = _Page.FindByName("fro_Popup_NewItem") as Syncfusion.XForms.PopupLayout.SfPopupLayout;
Xamarin.Forms.SearchBar tmp_SearchBar = tmp_Popup.FindByName("fro_SearchBar_NewItem") as Xamarin.Forms.SearchBar;

谢谢

您可以在code-behind.

中使用Task开一个新线程来完成这个操作

Xaml:

  <SearchBar  x:Name="fro_SearchBar_NewItem" Placeholder="...." BackgroundColor="White"/>

后面的代码:

   public partial class PagePop : Popup
   {
        public PagePop()
        {
             InitializeComponent();
             Task.Run(() => myTask());//Create and start the thread
        }
        private void myTask()
        {
            Thread.Sleep(300);
            fro_SearchBar_NewItem.Focus();
        }
    }

这似乎与正确的方向相去不远,实际上评论都没有。

Syncfusion.XForms.PopupLayout.SfPopupLayout tmp_Popup = _Page.FindByName("fro_Popup_NewItem") as Syncfusion.XForms.PopupLayout.SfPopupLayout;
Xamarin.Forms.SearchBar tmp_SearchBar = tmp_Popup.FindByName("fro_SearchBar_NewItem") as Xamarin.Forms.SearchBar;

但我找到了有效的方法,我不必为 Popup 创建单独的 XAML。

private void fro_Popup_NewItem_Opened(object sender, EventArgs e)
    {
        try
        {
            var nativeObject = (object)fro_Popup_NewItem.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name.Equals("NativeObject")).GetValue(fro_Popup_NewItem);
            var formsPopupviewContentTemplate = nativeObject.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("formsPopupViewContentTemplate")).GetValue(nativeObject);
            var SearchBar = (formsPopupviewContentTemplate as Grid).FindByName<SearchBar>("fro_SearchBar_NewItem");
            SearchBar?.Focus();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

谢谢大家的帮助。