2016-11-25 102 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> 
相關問題