Добавление контента на пользовательскую страницу контента - PullRequest
0 голосов
/ 04 февраля 2020

Хорошо, поэтому я пытаюсь добавить панель поиска на панели инструментов моей страницы.

  • Панель поиска отображается правильно на панели инструментов, и я могу отследить событие изменения текста.

  • Я создал новую страницу Xaml и cs и изменил страницу содержимого на «MySearchContentPage»

  • Я пытался добавить сетку и метку на мою новую созданную страницу но ничего не покажет, кроме панели поиска. Я добавил это только для того, чтобы посмотреть, смогу ли я что-нибудь отобразить.

Я добавляю это в нужном месте? Или как добавить контент на эту страницу?

Я сделал это, выполнив следующее:

MySearchContentPage Class:

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>

enter image description here

1 Ответ

1 голос
/ 05 февраля 2020

Причина в том, что ContentPage наследуется, а XAML не наследуется .

Я бы порекомендовал вам использовать пользовательский contentview и добавить this contentView to 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...