【问题标题】:Adding content on a custom content page在自定义内容页面上添加内容
【发布时间】:2020-02-04 18:42:38
【问题描述】:

好的,所以我正在尝试在我的页面的工具栏中添加一个搜索栏。

  • 搜索栏正确显示在工具栏中,我可以捕捉到 on text changed 事件。

  • 我创建了一个新的 Xaml 和 cs 页面并将内容页面更改为“MySearchContentPage”

  • 我尝试在创建的新页面上添加网格和标签,但除了搜索栏外什么都不会显示。我添加这个只是为了看看是否可以显示任何内容。

我是否在正确的位置添加它?或者您如何向此页面添加内容?

我通过以下方式做到了这一点:

MySearchContentPage 类:

public class MySearchContentPage : ContentPage, ISearchPage
{

    public MySearchContentPage()
    {

        SearchBarTextChanged += HandleSearchBarTextChanged;

    }





    public event EventHandler<string> SearchBarTextChanged;

    public void OnSearchBarTextChanged(string text) => SearchBarTextChanged?.Invoke(this, text);

    void HandleSearchBarTextChanged(object sender, string searchBarText)
    {
        //Logic to handle updated search bar text

    }
}

ISearchPage:

public interface ISearchPage
{
    void OnSearchBarTextChanged(string text);
    event EventHandler<string> SearchBarTextChanged;


}

iOS 渲染器页面:

public class MySearchContentPageRenderer : PageRenderer, IUISearchResultsUpdating
{
    readonly UISearchController searchController;


    bool _isFirstAppearing = true;

    public override void WillMoveToParentViewController(UIViewController parent)
    {
        base.WillMoveToParentViewController(parent);

        var searchController = new UISearchController(searchResultsController: null)
        {
            SearchResultsUpdater = this,
            DimsBackgroundDuringPresentation = false,
            HidesNavigationBarDuringPresentation = true,
            HidesBottomBarWhenPushed = true



        };
        searchController.SearchBar.Placeholder = "Search Symptoms";



        parent.NavigationItem.SearchController = searchController;

        DefinesPresentationContext = true;
    }

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);

        //Work-around to ensure the SearchController appears when the page first appears https://stackoverflow.com/a/46313164/5953643
        if (_isFirstAppearing)
        {
            ParentViewController.NavigationItem.SearchController.Active = true;
            ParentViewController.NavigationItem.SearchController.Active = false;

            _isFirstAppearing = false;
        }
    }

    public void UpdateSearchResultsForSearchController(UISearchController searchController)
    {
        if (Element is ISearchPage searchPage)
            searchPage.OnSearchBarTextChanged(searchController.SearchBar.Text);
    }


public MySearchContentPageRenderer()
    {
       var searchControllerr = new UISearchController(searchResultsController: null)
        {
            SearchResultsUpdater = this,
            DimsBackgroundDuringPresentation = false,
            HidesNavigationBarDuringPresentation = false,
            HidesBottomBarWhenPushed = true
        };
        searchControllerr.SearchBar.Placeholder = string.Empty;
    }


    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        UINavigationBar.Appearance.TitleTextAttributes = new UIStringAttributes
        {
            ForegroundColor = UIColor.Red
        };



    }

    public override void ViewDidLoad()
    {
       //  base.ViewDidLoad();

      //  NavigationController.NavigationBar.PrefersLargeTitles = true;
      //  NavigationController.NavigationBar.BackgroundColor = UIColor.Red;

        //  var searchController = new UISearchController(searchResultsController: null);
        //  searchController.SearchBar.SearchBarStyle = UISearchBarStyle.Default;
        // searchController.SearchBar.BackgroundColor = UIColor.Green;
        // NavigationItem.SearchController = searchController;
        // NavigationItem.HidesSearchBarWhenScrolling = false;



        //searchController.SearchBar.SizeToFit();
        //searchController.SearchBar.SearchBarStyle = UISearchBarStyle.Prominent;
        ////NavigationController.TabBarController
        //this.sea
        //NavigationController.TabBarController.NavigationItem.HidesSearchBarWhenScrolling = true;
        //NavigationController.TabBarController.NavigationItem.SearchController = searchController;

        //this.Title = "Search";
    }








}

目前的结果是这样的:

我似乎无法将任何其他内容添加到此页面。谁能解释一下为什么?

AddSymptomNew.xaml 页面:

<?xml version="1.0" encoding="UTF-8"?>
<visiblegyapp:MySearchContentPage
xmlns:visiblegyapp="clr-namespace:VisiblegyApp"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="VisiblegyApp.AddSymptomNew"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.LargeTitleDisplay="Always"
Title="Search Symptoms"
BackgroundColor="{DynamicResource BasePageColor}"

>
<ScrollView 
    x:Name="outerScrollView"
    Padding="0"
    >

    <Grid 
            x:Name="layeringGrid" 
            RowSpacing="0"

            VerticalOptions="FillAndExpand">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Text="test label" TextColor="Red"  Grid.Row="1"/>



    </Grid>






    </ScrollView>

【问题讨论】:

  • 首先,为什么需要为这个页面定制一个渲染器?其次,当你说你不能添加任何其他元素时,你在哪里尝试这样做?您的 XAML 仅包含一个网格和一个标签。您尝试添加的 ListView 在哪里?
  • @Jason 我为此页面使用了自定义渲染,因此我可以在导航栏中使用搜索栏,我尝试使用 titleview 添加它,但它似乎不是那样工作的,是有更好的方法吗??我尝试在 XAML 页面中添加其他内容,抱歉,这是一个错字,我只是添加了网格和标签以查看它是否会显示,但我无法让它显示。

标签: xamarin xamarin.forms


【解决方案1】:

原因是 ContentPage 是可继承的,而 XAML is not inheritable

我建议您使用自定义 contentview 并将此 contentView 添加到 MySearchContentPage 。

例如,在此处创建自定义contentView

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }
}

在 Xaml 中:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App132.AddSymptomNewView">
  <ContentView.Content>

        <ScrollView 
            x:Name="outerScrollView"
            Padding="0">

            <Grid 
                x:Name="layeringGrid" 
                RowSpacing="0"

                VerticalOptions="FillAndExpand">

                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Label Text="test label" TextColor="Red"  Grid.Row="1"/>

            </Grid>

        </ScrollView>
    </ContentView.Content>
</ContentView>

并在MySearchContentPage 中使用它:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:app132="clr-namespace:App132"
             mc:Ignorable="d"
             x:Class="App132.MainPage">

    <app132:AddSymptomNewView/>

</ContentPage>

【讨论】:

  • 嗨杰克,所以我按照你的建议创建了一个新的内容视图并将其添加到一个新页面中,所以目前我的页面 xaml 看起来像这样 'xamarin.com/schemas/2014/forms" xmlns:x="schemas.microsoft.com/winfx/2009/xaml" x:Class="myapp.AddSym"> '。但它似乎仍然没有显示除搜索栏以外的任何内容。
  • @MarkPW 我认为标签与搜索栏重叠。尝试添加 Horizo​​ntalOptions 和 VerticalOptions:&lt;app132:AddSymptomNewView VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/&gt;.
  • 谢谢你,问题是导航栏一直保持白色,直到你点击搜索栏然后它变成应该的颜色,你知道为什么会这样吗?
  • 红色?您应该检查设置颜色的代码,看看代码是否在页面创建时运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 2018-04-30
相关资源
最近更新 更多