如何在工具栏或导航栏中添加徽标和文本

时间:2017-06-23 05:45:50

标签: xamarin.forms

我需要以这种方式添加徽标和文字:

Leftside上的徽标和工具栏或导航栏中的文字:

Logo MyCompany

我尝试了以下代码:

从P1到MainMenuPage

- 在P1:

使用方法1:

   NavigationPage NP = new NavigationPage(new MainMenuPage())
    {
           Title = "Company Name",
           Icon = "itemIcon1",
           BarBackgroundColor = Color.Blue,
           BarTextColor = Color.DarkGray
     };

    App.Current.MainPage = NP;

这种方法 - 它会显示导航栏但没有图标,没有文字

---在P1中使用方法2:

await Navigation.PushModalAsync(new MainMenuPage());
  • 这种方法,它根本不显示导航栏!

即使我把它放在MainMenuPage中。 ToolBar也不会显示出来!

<ContentPage.ToolbarItems> 
  <ToolbarItem Name="MenuItem1" Order="Primary" Icon="Microsoft.png" Text="Item 1" Priority="0" /> 
  <ToolbarItem Name="MenuItem2" Order="Primary" Icon="Xamarin.png" Text="Item 2" Priority="1" /> 
</ContentPage.ToolbarItems>
下面的

是MainMenuPage:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MainMenuPage"> 
  <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" BackgroundColor="Olive">
    <StackLayout Orientation="Vertical">
      <Label Text = "welcome" FontSize="24"/>
      <Label Text = "Show Company Logo"/>
    </StackLayout>
  </StackLayout>    
</ContentPage>

希望得到您的帮助。

由于

1 个答案:

答案 0 :(得分:0)

你有两个要这样做。

第一个解决方案: 您不显示导航栏,但在Xamarin.Forms中手动创建它。您仍然可以在导航页面中,但是可以在内容页面的构造函数中添加它:

NavigationController.SetNavigationBarHidden (true, true);

然后在您的页面中手动构建自己的导航栏。

第二个解决方案: 您可以使用内容页面的自定义渲染器(而不是导航页面的渲染器)来执行此操作。

对于ios,您可以使用以下内容:

public class ContentPageSubtitleRenderer : PageRenderer
{
    //Call this method when you need it
    // It can be in OnElementChanged and/or when a property is updated
    private void SetBar()
    {
            var navigationItem = NavigationController.TopViewController.NavigationItem;

            navigationItem.TitleView = myUIView;
    }
}