Xamarin Forms导航栏中的图像不可见

huangapple go评论59阅读模式
英文:

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(&quot;#212121&quot;),
    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:

&lt;NavigationPage.TitleView&gt;
    &lt;StackLayout VerticalOptions=&quot;CenterAndExpand&quot; Orientation=&quot;Horizontal&quot;&gt;
        &lt;Image Source=&quot;Title_Dark.png&quot; HeightRequest=&quot;25&quot; /&gt;
    &lt;/StackLayout&gt;
&lt;/NavigationPage.TitleView&gt;

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;

通常,我们总是将页面设置为TabbedPageMaster-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();

huangapple
  • 本文由 发表于 2020年1月3日 18:15:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定