页面标题不显示在内容页面

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

Page Title Not Shown On Content Page

问题

I set visibility of this page's navigation bar and my title was gone like that.
But there is a blue bar I can navigate back. I'm new at Xamarin Forms. I don't know its name, but it works now. I want to show my page's title on that blue bar, not the Navigation bar. My page title must be shown on that empty space in the picture. How can I do that?

Xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
        xmlns:local="clr-namespace:İdaServis.ViewModels"
        xmlns:renderers="clr-namespace:İdaServis.CustomRenderer"
        x:Class="İdaServis.View.PageName"
        Title="{Binding Title}"
        x:Name="Page"
        BackgroundColor="White">
 </ContentPage>

CS:

public PageName()
{
    InitializeComponent();
    NavigationPage.SetHasNavigationBar(this, false);
}

页面标题不显示在内容页面

英文:

I set visibiliy of this page's navigation bar and my title was gone like that.
But there is a bluebar i can navigate back. I'm new at xamarin forms. I dont know its name but it works now. I want to show my page's title on that blue bar, not the Navigation bar. My page title must be shown on that empty space on the picture. How can i do that ?

页面标题不显示在内容页面
Xaml:

&lt;ContentPage xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
        xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
        xmlns:xct=&quot;http://xamarin.com/schemas/2020/toolkit&quot;
        xmlns:local=&quot;clr-namespace:İdaServis.ViewModels&quot;
        xmlns:renderers=&quot;clr-namespace:İdaServis.CustomRenderer&quot;
        x:Class=&quot;İdaServis.View.PageName&quot;
        Title=&quot;{Binding Title}&quot;
        x:Name=&quot;Page&quot;
        BackgroundColor=&quot;White&quot;&gt;
 &lt;/ContentPage&gt;

CS:

public PageName()
{
    InitializeComponent();
    NavigationPage.SetHasNavigationBar(this, false);
}

答案1

得分: 1

Here is the translated content:

如果您想在蓝色条上显示页面标题而不是导航栏,请将NavigationPage.SetHasNavigationBar的第二个参数设置为true

public PageName()
{
    InitializeComponent();
    NavigationPage.SetHasNavigationBar(this, true);
}

然后,您可以通过为页面添加代码Title="My Page"来设置页面标题,就像下面这样:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="My Page"
             x:Class="MVVMApp.SecondPage">
    
   
</ContentPage>

当然,您还可以使用TitleView来自定义标题栏,例如:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="My Page"
             x:Class="MVVMApp.SecondPage">
    
    <NavigationPage.TitleView>
        <Label  Text="自定义标题栏-----"/>
    </NavigationPage.TitleView>
    <ContentPage.Content>
        <StackLayout>
            <Label Text="欢迎访问第二页"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />

        </StackLayout>
    </ContentPage.Content>
</ContentPage>
英文:

> I want to show my page's title on that blue bar, not the Navigation
> bar.

If you want show your page's title on that blue bar, please set the second parameter of NavigationPage.SetHasNavigationBar to true :

 public PageName()
{
    InitializeComponent();
    NavigationPage.SetHasNavigationBar(this, true);
}

Then you can set your page's title by adding code Title=&quot;My Page&quot; for your Page. Just as follows:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; 
&lt;ContentPage xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             Title=&quot;My Page&quot;
             x:Class=&quot;MVVMApp.SecondPage&quot;&gt;

   
&lt;/ContentPage&gt;

Of cource, you can also custom this with TitleView.

For example:

 &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; 
&lt;ContentPage xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             Title=&quot;My Page&quot;
             x:Class=&quot;MVVMApp.SecondPage&quot;&gt;

    &lt;NavigationPage.TitleView&gt;
        &lt;Label  Text=&quot;custom TitleView-----&gt;&quot;/&gt;
    &lt;/NavigationPage.TitleView&gt;
    &lt;ContentPage.Content&gt;
        &lt;StackLayout&gt;
            &lt;Label Text=&quot;Welcome to secoend page&quot;
                VerticalOptions=&quot;CenterAndExpand&quot; 
                HorizontalOptions=&quot;CenterAndExpand&quot; /&gt;

        &lt;/StackLayout&gt;
    &lt;/ContentPage.Content&gt;
&lt;/ContentPage&gt;

答案2

得分: 1

为了重现您的问题,我在官方示例FlyoutSample中添加了一个LoginPage

类的顺序是:App.cs --> Login --> MainPage(FlyoutPage)

步骤如下:

  1. App.cs中,将MainPage = new FlyoutPageNavigation.MainPage ();替换为MainPage = new NavigationPage(new Login());
public class App : Application 
{
    public App ()
    {
        //MainPage = new FlyoutPageNavigation.MainPage ();
        MainPage = new NavigationPage(new Login());
    }
}
  1. Login.xaml.cs页面上,添加一个按钮以导航到MainPage(FlyoutPage)
public partial class Login : ContentPage 
{
    public Login()
    {
        InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new MainPage());// 这将添加另一个导航栏

        // 使用Navigation.PushModalAsync不会添加导航栏
        //Navigation.PushModalAsync(new MainPage());
    }
}

注意:

Button_Clicked方法中,如果您使用以下代码,将在屏幕顶部添加一个导航栏。如果使用FlyoutPage,则无需添加此导航栏。

Navigation.PushAsync(new MainPage());

因此,请尝试改用Navigation.PushModalAsync

Navigation.PushModalAsync(new MainPage());
英文:

To reproduce your problem, I added a LoginPage to offcial sample FlyoutSample.

The class order is: App.cs --> Login--> MainPage(FlyoutPage).

The steps are as follows:

1.In App.cs,replace MainPage = new FlyoutPageNavigation.MainPage (); with MainPage = new NavigationPage(new Login());

  public class App : Application 
      {
            public App ()
            {
                  //MainPage = new FlyoutPageNavigation.MainPage ();
                  MainPage = new NavigationPage(new Login());

            }

  }

2.On Page Login.xaml.cs,add a Button to navigate to MainPage(FlyoutPage)

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

    private void Button_Clicked(object sender, EventArgs e)
    {

        Navigation.PushAsync(new MainPage());// this will add another navigate bar

        // Navigation.PushModalAsync will not add the navigate bar
        //Navigation.PushModalAsync(new MainPage());
    }
}

Note:

In method Button_Clicked,if you used the following code, a navigate bar will be added to the top of the screen. If you used FlyoutPage,there is no need to add this navigate bar.

Navigation.PushAsync(new MainPage());

So, try to use Navigation.PushModalAsync instead:

  Navigation.PushModalAsync(new MainPage());

huangapple
  • 本文由 发表于 2023年7月6日 20:34:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628893.html
匿名

发表评论

匿名网友

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

确定