asp.net-mvc - 如何在导航页面中包含搜索栏
问题描述
所以,我需要在导航页面或导航栏中包含搜索栏,但我无法理解如何以编程方式执行此操作。我可以在 App.cs 中创建搜索栏,但不能将其包含在导航栏中(我在 ASp.NetCore 中使用 Xamarin.Forms 和 RestApi。请帮助我!
protected async override void OnStart(){
HttpResponseMessage response = await client.GetAsync("http://192.168.1.6:5000/api/Events");
if (response.StatusCode == HttpStatusCode.OK)
{
HttpContent responseContent = response.Content;
var json = await responseContent.ReadAsStringAsync();
List<Event> Events = JsonConvert.DeserializeObject<List<Event>>(json);
MainPage page = new MainPage();
StackLayout MyButtons = new StackLayout()
{
Orientation = StackOrientation.Vertical,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
SearchBar search = new SearchBar
{
Placeholder = "Search items...",
PlaceholderColor = Color.Gray,
TextColor = Color.Gray,
HorizontalTextAlignment = TextAlignment.End,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Start,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(SearchBar)),
FontAttributes = FontAttributes.Italic,
};
search.SearchButtonPressed += OnSearchButtonPressed;
Frame FrameSearch = new Frame()
{
BorderColor = Color.Gray,
CornerRadius = 10,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Start,
HeightRequest = 40,
WidthRequest = 180,
HasShadow = true,
Margin = new Thickness(0,10,0,0),
Padding = new Thickness(0),
Content = new StackLayout
{
Padding = 0,
Children =
{
search
}
}
};
MyButtons.Children.Add(FrameSearch);
foreach (var item in Events)
{
var btn = new Button()
{
Text = item.Name, //Whatever prop you wonna put as title;
StyleId = item.EventId.ToString(), //use a property from event as id to be passed to handler
BorderRadius = 50,
FontSize = 30,
BorderColor = Color.Black,
FontAttributes = FontAttributes.Bold,
Margin = new Thickness(2, 5, 2, 0),
Padding = new Thickness(0),
HorizontalOptions = LayoutOptions.FillAndExpand
};
btn.Clicked += OnDynamicBtnClicked;
//Content = new ContentPage()
MyButtons.Children.Add(btn);
}
ScrollView scrollView = new ScrollView();
scrollView.Content = MyButtons;
page.Content = scrollView;
Application.Current.MainPage = new NavigationPage(page);
NavigationPage _Page = Application.Current.MainPage as NavigationPage;
_Page.BarBackgroundColor = Color.FromHex("009688");
解决方案
您可以设置NavigationPage.TitleView来实现效果。
就像在 c# 代码中一样:
public TitleViewPage()
{
InitializeComponent();
var titleView = new SearchBar { HeightRequest = 44, WidthRequest = 300 };
NavigationPage.SetTitleView(this, titleView);
}
或在 xaml 中:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NavigationPageTitleView.TitleViewPage">
<NavigationPage.TitleView>
<SearchBar HeightRequest="44" WidthRequest="300" />
</NavigationPage.TitleView>
...
</ContentPage>
推荐阅读
- java - Spring批处理NoClassDefFoundError:oracle/xdb/XMLType
- gitlab-ci - 在带有巧克力的 Windows 上安装 gitlab-runner
- python - 在firestore中设置python http函数
- css - 如何在 html 和 css 中制作始终位于底部的项目
- node.js - HTML 5 音频元素是否在除 Firefox 之外的所有浏览器中都损坏了?
- python - 当鼠标悬停在 QGraphicsProxyWidget 上时,QGraphicsView 的行为不同
- javascript - 错误:预期的模拟函数已被调用 - onclick Jest 酶
- regex - replaceAllMapped 与 span 元素匹配
- swift - Kotlin 中 Swift 的 fatalError() 相当于什么
- javascript - 输入内的字符间距在其整个宽度上相等