2016-11-25 15 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> 
関連する問題