Xamarin Forms - TabbedPage 平台特定 xaml 代码隐藏
Xamarin Forms - TabbedPage platform specific xaml code to code-behind
我正在尝试更改标签页中图标的颜色。我可以做到这一点,转换为 android:TabbedPage.BarItemColor="Red"
.
但是图标的颜色必须根据当前显示的页面而改变。
我已经设法让这个在页面上触发的方法在我的代码隐藏中发生了变化:
Tabbepage.xaml.cs
private void TabbedPage_CurrentPageChanged(object sender, EventArgs e)
{
var navigationPage = CurrentPage as NavigationPage;
var currentPage = navigationPage.CurrentPage;
if (currentPage.GetType() == typeof(MenuPage))
{
Tabbar.BarTextColor = Color.White;
Tabbar.BarBackgroundColor = Color.FromHex("#004f3d");
}
else if (currentPage.GetType() == typeof(HerdList))
{
Tabbar.BarTextColor = Color.Black;
Tabbar.BarBackgroundColor = Color.White;
}
}
但我只能弄清楚如何设置颜色:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="ChrApp.Views.Tab.BottomTabNavigation"
xmlns:local="clr-namespace:ChrApp.Views"
x:Name="Tabbar"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarSelectedItemColor="Green"
android:TabbedPage.BarItemColor="Red"
xmlns:CustomRenderer="clr-namespace:ChrApp.CustomRenderer"
CurrentPageChanged="TabbedPage_CurrentPageChanged"
>
<NavigationPage
Title="Forside"
>
<x:Arguments>
<local:MenuPage/>
</x:Arguments>
<NavigationPage.IconImageSource>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}" Glyph="{x:Static CustomRenderer:Icon.House}"/>
</NavigationPage.IconImageSource>
</NavigationPage>
<NavigationPage
Title="Besætning"
>
<x:Arguments>
<local:HerdList/>
</x:Arguments>
<NavigationPage.IconImageSource>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}" Glyph="{x:Static CustomRenderer:Icon.Pig}"/>
</NavigationPage.IconImageSource>
</NavigationPage>
</TabbedPage>
我能否以某种方式将我的代码隐藏中的方法发送到我的视图模型,或者我能否从我的代码隐藏中访问演员表 android:TabbedPage.BarItemColor="Red"
?
提前致谢! ❤
这就是您可以在后面的代码中实现的方法
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.Xaml;
using TabbedPage = Xamarin.Forms.TabbedPage;
namespace DummyTestApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TabbedPage1 : TabbedPage
{
public TabbedPage1()
{
InitializeComponent();
On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom).SetBarItemColor(Color.Red).SetBarSelectedItemColor(Color.Green);
}
}
}
P.S. :BarSelectedItemColor
现在已过时,在 TabbedPage
中使用 SelectedTabColor
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="DummyTestApp.Views.TabbedPage1"
SelectedTabColor="Blue">
<!--Pages can be added as references or inline-->
<ContentPage Title="Tab 1" />
<ContentPage Title="Tab 2" />
<ContentPage Title="Tab 3" />
</TabbedPage>
我正在尝试更改标签页中图标的颜色。我可以做到这一点,转换为 android:TabbedPage.BarItemColor="Red"
.
但是图标的颜色必须根据当前显示的页面而改变。
我已经设法让这个在页面上触发的方法在我的代码隐藏中发生了变化:
Tabbepage.xaml.cs
private void TabbedPage_CurrentPageChanged(object sender, EventArgs e)
{
var navigationPage = CurrentPage as NavigationPage;
var currentPage = navigationPage.CurrentPage;
if (currentPage.GetType() == typeof(MenuPage))
{
Tabbar.BarTextColor = Color.White;
Tabbar.BarBackgroundColor = Color.FromHex("#004f3d");
}
else if (currentPage.GetType() == typeof(HerdList))
{
Tabbar.BarTextColor = Color.Black;
Tabbar.BarBackgroundColor = Color.White;
}
}
但我只能弄清楚如何设置颜色:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="ChrApp.Views.Tab.BottomTabNavigation"
xmlns:local="clr-namespace:ChrApp.Views"
x:Name="Tabbar"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarSelectedItemColor="Green"
android:TabbedPage.BarItemColor="Red"
xmlns:CustomRenderer="clr-namespace:ChrApp.CustomRenderer"
CurrentPageChanged="TabbedPage_CurrentPageChanged"
>
<NavigationPage
Title="Forside"
>
<x:Arguments>
<local:MenuPage/>
</x:Arguments>
<NavigationPage.IconImageSource>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}" Glyph="{x:Static CustomRenderer:Icon.House}"/>
</NavigationPage.IconImageSource>
</NavigationPage>
<NavigationPage
Title="Besætning"
>
<x:Arguments>
<local:HerdList/>
</x:Arguments>
<NavigationPage.IconImageSource>
<FontImageSource FontFamily="{StaticResource FontAwesomeSolid}" Glyph="{x:Static CustomRenderer:Icon.Pig}"/>
</NavigationPage.IconImageSource>
</NavigationPage>
</TabbedPage>
我能否以某种方式将我的代码隐藏中的方法发送到我的视图模型,或者我能否从我的代码隐藏中访问演员表 android:TabbedPage.BarItemColor="Red"
?
提前致谢! ❤
这就是您可以在后面的代码中实现的方法
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.Xaml;
using TabbedPage = Xamarin.Forms.TabbedPage;
namespace DummyTestApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TabbedPage1 : TabbedPage
{
public TabbedPage1()
{
InitializeComponent();
On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom).SetBarItemColor(Color.Red).SetBarSelectedItemColor(Color.Green);
}
}
}
P.S. :BarSelectedItemColor
现在已过时,在 TabbedPage
SelectedTabColor
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="DummyTestApp.Views.TabbedPage1"
SelectedTabColor="Blue">
<!--Pages can be added as references or inline-->
<ContentPage Title="Tab 1" />
<ContentPage Title="Tab 2" />
<ContentPage Title="Tab 3" />
</TabbedPage>