2016-11-25 104 views
0

我在Xamarin的UWP项目中创建了一个导航栏。如何在Xamarin中以编程方式更改导航栏的背景颜色?

App.xaml.cs 
... 
public app() 
{ 
    InitializeComponent(); 
    MainPage = new NavigationPage(new LoginPage()){ 
    BarBackgroundColor = Color.Black; 
    } 
} 

所以,如果我在设置页面,我需要以编程方式更改导航栏的颜色。

SettingPage.xaml.cs 

... 
private void clicked_btn(sender, e) { 
    ... 
    // how can I get the handle of navigationbar and then change the attribute of one??? 
} 

这可能吗?

有没有办法我可以做?

回答

3

最好不要这样做,或者通过自定义渲染器来完成。 但低于是形式的方法:

var navigationPage = Application.Current.MainPage as NavigationPage; 
navigationPage.BarBackgroundColor = Color.Black; 
1

从类定义中可以设置栏背景颜色。喜欢这个。

namespace ProyectName 
{ 
    public class MainPage 
    { 
     public MainPage() 
     { 
      BarBackgroundColor = Color.FromHex("#484559"); 
      BarTextColor = Color.White; 
     } 
    } 
} 

或者从您的app.xml中添加的ResourceDictionary

<?xml version="1.0" encoding="utf-8"?> 
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="StockIt.App"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <Color x:Key="Primary">#484559</Color> 
      <Style TargetType="NavigationPage"> 
       <Setter Property="BarBackgroundColor" Value="{StaticResource Primary}" /> 
       <Setter Property="BarTextColor" Value="White" /> 
      </Style> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 
相关问题