首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FindByName控件在ContentPage的另一个控件中的应用

FindByName控件在ContentPage的另一个控件中的应用
EN

Stack Overflow用户
提问于 2022-03-19 19:28:01
回答 2查看 160关注 0票数 0

我想把注意力集中在SearchBar出现的时候。问题是SearchBar位于弹出视图中,我需要从ViewModel访问他。

我会用标准的方式

代码语言:javascript
运行
复制
Xamarin.Forms.SearchBar tmp_SearchBar = this.Page.FindByName("fro_SearchBar_NewItem") as Xamarin.Forms.SearchBar;

但这已经不管用了。

这是XAML

代码语言:javascript
运行
复制
<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也不起作用。

代码语言:javascript
运行
复制
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;

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-23 17:05:22

似乎这离正确的方向并不遥远,实际上,这两个评论都没有。

代码语言:javascript
运行
复制
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;

但是我找到了一个可以工作的,我不需要为弹出窗口创建单独的XAML。

代码语言:javascript
运行
复制
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);
        }
    }

谢谢大家的帮助。

票数 0
EN

Stack Overflow用户

发布于 2022-03-21 07:13:46

您可以使用任务打开一个新线程,以便在代码隐藏中完成此操作。

Xaml:

代码语言:javascript
运行
复制
  <SearchBar  x:Name="fro_SearchBar_NewItem" Placeholder="...." BackgroundColor="White"/>

代码背后:

代码语言:javascript
运行
复制
   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();
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71541371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档