In this code , I have first created reportpage and about page.then i create a shell page and add this two page to tab bar . i have a Ad in reportpage .now i want to to carry Ad below my tabbar in shell,any ways show Ad below Shell
<Shell 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"
xmlns:local="clr-namespace:OZHealth.Views" xmlns:views1="clr-namespace:OZHealth.Models" xmlns:controls="clr-namespace:OZHealth.Controls"
Title="OZHealth"
x:Class="OZHealth.AppShell">
<Shell.Resources>
<ResourceDictionary>
<Color x:Key="NavigationPrimary">#2196F3</Color>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{StaticResource NavigationPrimary}" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
<TabBar>
<Tab Title="Browse" Icon="tab_feed.png">
<ShellContent ContentTemplate="{DataTemplate local:ItemsPage}" />
</Tab>
<Tab Title="Reports" Icon="tab_about.png">
<ShellContent ContentTemplate="{DataTemplate local:ReportsPage}" />
</Tab>
</TabBar>
</shell>
this is my ReportPage.one of item in tabbar
<ContentPage 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="OZHealth.Views.ReportsPage"
xmlns:viewmodels="clr-namespace:OZHealth.ViewModels" xmlns:views="clr-namespace:OZHealth.Views" xmlns:views1="clr-namespace:OZHealth.Models" xmlns:controls="clr-namespace:OZHealth.Controls"
Title="{Binding Title}">
<ContentPage.BindingContext>
<viewmodels:ReportsViewModel />
</ContentPage.BindingContext>
<ContentPage.Resources>
</ContentPage.Resources>
<StackLayout Orientation="Vertical" Padding="16,40,16,40" Spacing="10">
<!--this is my chart-->
<views:PieChartView />
<!--this is my GoogleaAds-->
<views1:AdMobView x:Name="AdView" >
</views1:AdMobView>
</StackLayout>
</ContentPage>
This is my Shell page
I want to carry Ad bottom of Shell or app like this
How Can I Carry Ad To Bottom Of Shell?
I Cant Use NavBar In Shell
Based on my research, it cannot be achieved in the native Forms, we should achieve it by custom renderer for shell. Here is running GIF(If you want to do that, you should add the admob view in xamarin.android).
You should create a custom renderer like following code in shell.
[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace XFormsAdsWithShell.Droid
{
public class MyShellRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}
protected override IShellItemRenderer CreateShellItemRenderer(ShellItem shellItem)
{
return new ShellItemRenderer(this);
}
}
}
ShellItemRenderer.cs
internal class ShellItemRenderer : Xamarin.Forms.Platform.Android.ShellItemRenderer
{
public ShellItemRenderer(IShellContext shellContext) : base(shellContext)
{
}
BottomNavigationView _bottomView;
FrameLayout _navigationArea;
public override Android.Views.View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Android.Views.View _outerLayout = base.OnCreateView(inflater, container, savedInstanceState);
_bottomView = _outerLayout.FindViewById<BottomNavigationView>(Resource.Id.bottomtab_tabbar);
_bottomView.SetMinimumHeight(220);
_navigationArea = _outerLayout.FindViewById<FrameLayout>(Resource.Id.bottomtab_navarea);
return _outerLayout;
}
}
The important place is create a BottomTabLayout.xml(Please do not change anther name), Add following layout code.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/bottomtab.navarea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomtab.tabbar"
android:theme="#style/Widget.Design.BottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<FrameLayout
android:id="#+id/bottomtab.tabbar.container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#android:color/holo_red_dark">
<!--add your google AD here-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AD Area"
/>
</LinearLayout >
</FrameLayout>
</FrameLayout>
I have another way is achieve it like following gif.
By following code.
<TabBar>
<Tab Title="Browse" Icon="tab_feed.png">
<ShellContent Title="item" ContentTemplate="{DataTemplate local:ItemsPage}" />
<ShellContent Title="page1" ContentTemplate="{DataTemplate local:Page1}" />
</Tab>
</TabBar>
I hope others could give a better solution.
Related
I'm experimenting with TabbedPage and Shell in MAUI to create Horizontally scrollable tabs. I have got the expected behavior but in Android it shows a blank white space at top of Tabs and in iOS it shows Bar with a title of Tab Selected. I have attached a screenshot of Android.
Any one know how to remove it?
Code I have created:
Note: here page: are Content pages I have created in TabPages folder in project
MainPage.xaml
<TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Tyler.Energov.Mobile.EH.UI.InspectionOverview.InspectionOverviewPage"
xmlns:page="clr-namespace:Tyler.Energov.Mobile.EH.UI.InspectionOverview.TabPages"
Title="InspectionOverviewPage">
<Shell FlyoutBehavior="Disabled" FlyoutHeaderBehavior="Default">
<FlyoutItem Title="Abc">
<Tab>
<ShellContent Title="Parent record" ContentTemplate="{DataTemplate page:ParentRecordPage}"/>
<ShellContent Title="Additional info" ContentTemplate="{DataTemplate page:AdditionalInfoPage}"/>
<ShellContent Title="Contacts" ContentTemplate="{DataTemplate page:ContactsPage}"/>
<ShellContent Title="Previous inspections" ContentTemplate="{DataTemplate page:PreviousInspectionPage}"/>
<ShellContent Title="Attachments" ContentTemplate="{DataTemplate page:AttachmentsPage}"/>
</Tab>
</FlyoutItem>
</Shell>
</TabbedPage>
Tab Background color and Text color I have updated from the Style.xaml
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.TabBarBackgroundColor" Value="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource Black}}" />
<Setter Property="Shell.TabBarForegroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
<Setter Property="Shell.TabBarTitleColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource White}}" />
<Setter Property="Shell.TabBarUnselectedColor" Value="{AppThemeBinding Light={StaticResource LightGray}, Dark={StaticResource DarkGray}}" />
</Style>
Output:
You can use the Shell as the root element.
I created a demo and achieved this function.
You can refer to the following code:
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="TabedPageDemo.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TabedPageDemo"
xmlns:pages="clr-namespace:TabedPageDemo.Pages"
Shell.FlyoutBehavior="Disabled">
<TabBar>
<Tab Title="Test"
Icon="icon.png">
<ShellContent Title="Parent Record"
ContentTemplate="{DataTemplate pages:DiscoverPage}" />
<ShellContent Title="Additional Info"
ContentTemplate="{DataTemplate pages:AdditionalInfoPage}" />
<ShellContent Title="Contacts"
ContentTemplate="{DataTemplate pages:EpisodeDetailPage}" />
<ShellContent Title="Previous Inspection"
ContentTemplate="{DataTemplate pages:CategoriesPage}" />
<ShellContent Title="Attachements"
ContentTemplate="{DataTemplate pages:DiscoverPage}" />
<ShellContent Title="Updated Inspection"
ContentTemplate="{DataTemplate pages:ShowDetailPage}" />
<ShellContent Title="Inspector Profile"
ContentTemplate="{DataTemplate pages:EpisodeDetailPage}" />
<ShellContent Title="About us"
ContentTemplate="{DataTemplate pages:CategoriesPage}" />
</Tab>
</TabBar>
</Shell>
I am working on designing some user interfaces for my project, and am noticing that the title bar that is displayed when I run my app is not present in the XAML Design Previewer. Not only that, the title bar apparently is throwing off the size of my elements. Is there any way to get the title bar to display in the preview so I can accurately design my UI? I am using Visual Studio 2019. Here is some code. I am also attaching screenshots of the preview vs the emulator.
I've tried explicitly setting the NavigationPage.HasNavigationBar property to true in the content page header of MainScreen.
I initiate AppShell and make it the MainPage of the app:
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
This is the XAML of AppShell:
<?xml version="1.0" encoding="UTF-8"?>
<Shell 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"
xmlns:local="clr-namespace:MyApp.Views"
Title="MyApp"
x:Class="MyApp.AppShell"
>
<!--
Styles and Resources
-->
<Shell.Resources>
<ResourceDictionary>
<Color x:Key="NavigationPrimary">#2196F3</Color>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{StaticResource NavigationPrimary}" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="ShellItem" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
<!-- Your Pages -->
<FlyoutItem Title="Home Page" x:Name="MainScreenFlyout">
<ShellContent ContentTemplate="{DataTemplate local:MainScreen }"/>
</FlyoutItem>
<FlyoutItem Title="Second Page" x:Name="SecondPage">
<ShellContent ContentTemplate="{DataTemplate local:LoadingScreen}"/>
</FlyoutItem>
</Shell>
This is the XAML of the MainScreen view.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 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="MyApp.Views.MainScreen"
xmlns:vm="clr-namespace:MyApp.ViewModels"
Title="MyApp">
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="Primary">#2196F3</Color>
<Color x:Key="Accent">#96d1ff</Color>
<Color x:Key="LightTextColor">#999999</Color>
</ResourceDictionary>
</ContentPage.Resources>
<Grid Padding="0,25,0,25">
<Grid.RowDefinitions>
<RowDefinition Height="5*"></RowDefinition>
<RowDefinition Height="3*"></RowDefinition>
<RowDefinition Height="10*"></RowDefinition>
<RowDefinition Height="10*"></RowDefinition>
<RowDefinition Height="10*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
</ContentPage>
Edit: My ultimate goal was to get the preview to show the same way that the emulator ultimately would, and I did find a work around to accomplish it. I will have to manually adjust for each new device, but that's a headache for when I am making it response.
The solution is to set a top margin in the ignorable http://xamarin.com/schemas/2014/forms/design namespace. I tried to find the heights and got some numbers but they didn't work for me so I fooled around with it until it matched up. 87 appears to be the magic number for my device.
<!--Title bars are 48 and 56 for android devices, 44 for iphone, but apparently that isn't right?-->
<Grid Padding="0,25,0,25" d:Margin="0,87,0,0" x:Name="MainScreenGrid">
Because the "Title bar" called also navigation bar is generated by Shell, while the previewer renders only MainPage.xaml file (not Application.MainPage which is Shell).
I believe Shell is still not handled properly in the previewer, as well as in emulator hot reload, future versions of VS will probably bring some enhancements. For example if you open AppShell.xaml in the previewer nothing interesting will be rendered only a blank page.
Update
xaml previewer has been completely removed in VS 16.9.0 Preview 4.0, it will be replaced with an enhanced Hot Reload.
I'm working on a Xamarin Forms app and my problem consists on two factors:
I need the login page as startup page. If I set CurrentItem="{x:Reference homeTab} (or loginTab), the bottom TabBar no longer appears on any page. Currently it's hidden only on login and even if I set Shell.TabBarIsVisible="True" on the home page it won't appear. Also, removing the CurrentItem line will make it visible again.
How can I set click events on the TabBar? I need to have some logic behind it and not only navigate to a specific route.
Below is my shell xaml:
<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:PlusErp.Wms.Mobile.Views"
xmlns:resources="clr-namespace:PlusErp.Wms.Mobile.Resources"
Title="PlusErp.Wms.Mobile"
x:Class="PlusErp.Wms.Mobile.AppShell"
CurrentItem="{x:Reference homeTab}"
>
<!--
The overall app visual hierarchy is defined here, along with navigation.
https://learn.microsoft.com/xamarin/xamarin-forms/app-fundamentals/shell/
-->
<Shell.Resources>
<ResourceDictionary>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
<TabBar>
<ShellContent Title="{x:Static resources:AppResources.Back}" Icon="arrow_left.png" ContentTemplate="{DataTemplate local:HomePage}" />
<ShellContent x:Name="loginTab" Title="{x:Static resources:AppResources.Logout}" Icon="logout.png" Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
<ShellContent x:Name="homeTab" Title="{x:Static resources:AppResources.Home}" Icon="home.png" Route="HomePage" ContentTemplate="{DataTemplate local:HomePage}" />
<ShellContent Title="{x:Static resources:AppResources.InfoStock}" Icon="search.png" ContentTemplate="{DataTemplate local:LoginPage}" />
<ShellContent Title="{x:Static resources:AppResources.Forward}" Icon="arrow_right.png" Route="ItemsPage" ContentTemplate="{DataTemplate local:ItemsPage}" />
</TabBar>
</Shell>
I'm writing a xamarin app that targets Android, iOS, and UWP.
We're using the shell stack for routing and have a bottom bar with some flyout items.
<?xml version="1.0" encoding="UTF-8"?>
<Shell 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"
xmlns:views="clr-namespace:CADLearning.App.Views"
xmlns:user="clr-namespace:CADLearning.App.Views.User"
xmlns:controls="clr-namespace:CADLearning.App.Controls"
xmlns:services="clr-namespace:CADLearning.App.Services"
BackgroundColor="{DynamicResource HeaderBackgroundColor}"
FlyoutBackgroundColor="{DynamicResource HeaderBackgroundColor}"
x:Name="this"
x:Class="CADLearning.App.AppShell">
<!--
Styles and Resources
-->
<Shell.Resources>
<ResourceDictionary>
<Color x:Key="NavigationPrimary">#2196F3</Color>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{DynamicResource BackgroundColor}" />
<Setter Property="Shell.ForegroundColor" Value="{DynamicResource ForegroundColor}" />
<Setter Property="Shell.TitleColor" Value="{DynamicResource ForegroundColor}" />
<Setter Property="Shell.DisabledColor" Value="{DynamicResource DisabledForegroundColor}" />
<Setter Property="Shell.UnselectedColor" Value="{DynamicResource UnselectedForegroundColor}" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{DynamicResource HeaderBackgroundColor}" />
<Setter Property="Shell.TabBarForegroundColor" Value="{DynamicResource OrangeColor}"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="{DynamicResource UnselectedForegroundColor}"/>
<Setter Property="Shell.TabBarTitleColor" Value="{DynamicResource OrangeColor}"/>
</Style>
<Style TargetType="Tab" BasedOn="{StaticResource BaseStyle}" />
<Style x:Key="FlyoutItemLabelStyle" TargetType="Label">
<Setter Property="FontSize" Value="16"/>
<Setter Property="TextColor" Value="{DynamicResource ForegroundColor}"/>
<Setter Property="VerticalOptions" Value="Center"/>
</Style>
</ResourceDictionary>
</Shell.Resources>
<Shell.FlyoutHeader>
<controls:FlyoutHeader />
</Shell.FlyoutHeader>
<Shell.ItemTemplate>
<DataTemplate>
<StackLayout Padding="5,15,5,0" Orientation="Horizontal">
<Image Source="{Binding FlyoutIcon}" HeightRequest="24" WidthRequest="24" VerticalOptions="Center" Margin="0,0,5,0"/>
<Label Grid.Column="1" Text="{Binding Title}" Style="{DynamicResource FlyoutItemLabelStyle}" VerticalOptions="Center" />
</StackLayout>
</DataTemplate>
</Shell.ItemTemplate>
<Shell.MenuItemTemplate>
<DataTemplate>
<StackLayout Padding="5,15,5,0" Orientation="Horizontal">
<Image Source="{Binding FlyoutIcon}" HeightRequest="24" WidthRequest="24" VerticalOptions="Center" Margin="0,0,5,0"/>
<Label Grid.Column="1" Text="{Binding Title}" Style="{DynamicResource FlyoutItemLabelStyle}" VerticalOptions="Center" />
</StackLayout>
</DataTemplate>
</Shell.MenuItemTemplate>
<!--Actual Items-->
<FlyoutItem Title="Home" Icon="home.png" Route="home">
<Tab Title="Dashboard" Icon="workflow.png" Route="dashboard">
<ShellContent ContentTemplate="{DataTemplate views:DashboardPage}"/>
</Tab>
<Tab Title="Roles & Goals" Icon="goal.png" Route="goals">
<ShellContent ContentTemplate="{DataTemplate views:GoalsPage}"/>
</Tab>
<Tab Title="Library" Icon="library.png" Route="library">
<ShellContent ContentTemplate="{DataTemplate views:LibraryPage}"/>
</Tab>
<Tab Title="Playlists" Icon="playlist.png" Route="playlists">
<ShellContent ContentTemplate="{DataTemplate views:PlaylistsPage}"/>
</Tab>
</FlyoutItem>
<FlyoutItem Title="Support" Icon="Q.png" Route="support">
<ShellContent ContentTemplate="{DataTemplate views:QPage}"/>
</FlyoutItem>
<FlyoutItem Title="Settings" Icon="settings.png" Route="user">
<Tab Title="Settings" Icon="settings.png" Route="settings">
<ShellContent ContentTemplate="{DataTemplate user:SettingsPage}"/>
</Tab>
<Tab Title="Transcript" Icon="transcript.png" Route="transcript">
<ShellContent ContentTemplate="{DataTemplate user:TranscriptPage}"/>
</Tab>
</FlyoutItem>
<MenuItem Text="Logout" Icon="login.png" x:Name="miLogout" Clicked="miLogout_Clicked"/>
</Shell>
on Android and iOS this works okay and the bottom tabbed items display.
However in UWP they don't display unless you hover over them
Could anyone point me in the right direction on getting this to work in UWP?
My main resources look like this:
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:converters="clr-namespace:Japanese"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Japanese.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary Source="/Resources/DetailRes.xaml" />
I have this resource file:
<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Japanese.DetailRes">
<Style x:Key="DetailLabel" TargetType="Label">
<Setter Property="FontSize">
<Setter.Value>
<OnPlatform x:TypeArguments="x:Double">
<On Platform="iOS" Value="35" />
<On Platform="Android" Value="35" />
</OnPlatform>
</Setter.Value>
</Setter>
<Setter Property="VerticalOptions" Value="Center" />
<Setter Property="LineBreakMode" Value="WordWrap" />
<Setter Property="HorizontalTextAlignment" Value="Center" />
</Style>
</ResourceDictionary>
In XAML I access it like this:
<t:BaseFrameButtonTemplate xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:t="clr-namespace:Japanese.Templates"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.RoundButtonText" x:Name="this" >
<Label Text="{Binding Text, Source={x:Reference this}}"
Style="{StaticResource DetailRes}"
x:Name="Label" />
</t:BaseFrameButtonTemplate>
I would like to access this in C# but do not know how to do it. I tried this but it does not find the resource:
Label.Style = (Style)Application.Current.Resources["DetailRes"];
You can use the TryGetValue to obtain a single from your ResourceDictionary:
if (Application.Current.Resources.TryGetValue("DetailLabel", out object style))
{
someLabel.Style = (Style)style;
}