英文:
Xamarin Forms Image in NavigationBar not visible
问题
我有一个问题。我创建了一个类似这样的TabbedPage:
TabbedBuilder = new TabbedPage
{
BarBackgroundColor = Color.FromHex("#212121"),
BarTextColor = Color.White
};
TabbedBuilder.Children.Add(new ImageBuilder());
TabbedBuilder.Children.Add(new ImageTemplates());
然后我这样设置了TabbedPage:
Navigation.PushAsync(TabbedBuilder);
但现在我想在导航栏的中央添加一张图片,所以在ImageBuilder页面中我添加了以下代码:
<NavigationPage.TitleView>
<StackLayout VerticalOptions="CenterAndExpand" Orientation="Horizontal">
<Image Source="Title_Dark.png" HeightRequest="25" />
</StackLayout>
</NavigationPage.TitleView>
但是当我进入那个页面时,导航栏中没有图片...
我做错了什么?
英文:
I have a problem. I created a TabbedPage like this:
TabbedBuilder = new TabbedPage
{
BarBackgroundColor = Color.FromHex("#212121"),
BarTextColor = Color.White
};
TabbedBuilder.Children.Add(new ImageBuilder());
TabbedBuilder.Children.Add(new ImageTemplates());
And I set the TabbedPage like this:
Navigation.PushAsync(TabbedBuilder);
But now I want to add an image in the center of the navigation bar, so in the ImageBuilder page I added the following code:
<NavigationPage.TitleView>
<StackLayout VerticalOptions="CenterAndExpand" Orientation="Horizontal">
<Image Source="Title_Dark.png" HeightRequest="25" />
</StackLayout>
</NavigationPage.TitleView>
But when I go to that page, no image is in the navigation bar....
What am I doing wrong?
答案1
得分: 1
由于您已将ContentPage的TitleView设置为ImageBuilder,当将其添加到Tabbed Page时,您应该将其初始化为NavigationPage:
TabbedBuilder.Children.Add(new NavigationPage(new ImageBuilder()));
TabbedBuilder.Children.Add(new NavigationPage(new ImageTemplates()));
但这种方式会导致您的应用程序有两个导航栏,因为您已将MainPage设置为NavigationPage。
因此,我建议您更改MainPage,而不是调用PushAsync方法:
App.Current.MainPage = TabbedBuilder;
通常,我们总是将页面设置为TabbedPage和Master-Detail-Page作为应用程序的MainPage,而不是子页面。
更新
如果您确实希望在它们之间导航,可以调用PushModalAsync方法:
Navigation.PushModalAsync(TabbedBuilder);
然后在想要返回时弹出它:
Navigation.PopAsync();
英文:
Since you had set the TitleView
of the ContentPage ImageBuilder
. You should init it as NavigationPage
when add it to the Tabbed Page
TabbedBuilder.Children.Add(new NavigationPage(new ImageBuilder()));
TabbedBuilder.Children.Add(new NavigationPage(new ImageTemplates()));
But in this way , you app will have two Navigation Bar
because you had set the MainPage as NavigationPage .
So I suggest that you could change the MainPage instead of call the method PushAsync
App.Current.MainPage = TabbedBuilder;
Normally , we always set the page like TabbedPage and Mater-Detail-Page as MainPage of App instead of a sub-page.
Update
If you do want to navigate between them you could call the method PushModalAsync
Navigation.PushModalAsync(TabbedBuilder);
And pop it when you want to return
Navigation.PopAsync();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论