How to use Dark Theme Light Theme in Xamarin Forms? - xamarin

I am new to Xamarin Forms, I want to use a logo that changes based on the device mode, if it's dark mode, the logo will be white and if it's light mode, the logo will be black. I only found examples on how to do it for colors by adding them to the resource dictionaries(the example is attached below), I tried to do it for the logo, but It didn't work.Can someone share tips or a link of a tutorial that show how to do that?
<ResourceDictionary
x:Class="OpsMobile.Styles.DarkTheme"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Source="Global.xaml">
<!-- Colours -->
<!--<Color x:Key="MainPage">#F58220</Color>-->
</ResourceDictionary>
<ResourceDictionary
x:Class="OpsMobile.Styles.LightTheme"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Source="Global.xaml">
<!-- Colours -->
<!--<Color x:Key="MainPage">#F58220</Color>-->
</ResourceDictionary>

Related

iOS Xamarin Forms - Toolbar and Navigation items not showing

I am adding iOS support for my already existing Xamarin Forms android app.
The toolbar items, navigational back arrow and the hamburger menu do not show on iOS:
While they work perfectly on Android:
The Application.MainPage is set to a Shell:
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="app.MainPage"
FlyoutBackgroundColor="{StaticResource cBackgroundColorDark}"
Shell.TabBarTitleColor="{StaticResource cBackgroundColor}"
Shell.BackgroundColor="{StaticResource cBackgroundColor}">
<Shell.Resources>
<ResourceDictionary>
<vm:FlyoutVM x:Key="vm" x:Name="vm"/>
</ResourceDictionary>
</Shell.Resources>
<Shell.FlyoutHeader>
</Shell.FlyoutHeader>
<ShellContent Title="page1"
IsTabStop="True"
ContentTemplate="{DataTemplate views:Page1Page}"/>
...
<Shell.FlyoutContent>
</Shell.FlyoutContent>
<Shell.FlyoutFooter>
</Shell.FlyoutFooter>
</Shell>
The page with the toolbar items is simply:
<ContentPage>
<ContentPage.ToolbarItems>
<ToolbarItem Text="Example"
IconImageSource="Example.png"
Clicked="ExampleToolbarItem_Clicked"
/>
</ContentPage.ToolbarItems>
</ContentPage>
All navigation (hamburger menu, back button ect) and toolbar items can be tapped on iOS and work as intended - they just are not visible.
I do not understand why this does not work on iOS, any suggestions why this might be the case?
(I cannot find any styling that might be making it act differently. I have also tried displaying a toolbar with just text (no icons) and the problem persists)
I can provide more code if needed.
EDIT
I ended up creating a new Xamarin shell template project and moving everything over. That fixed the issue...
Most likely the image size for ios is incorrect. I use this https://appicon.co/#image-sets to create image set for ios.

Labels are not visible in Xamarin.iOS

I just recently started Xamarin.Forms. I run the following code by deploying it to my own IPhone device. The code is the default code that is added whenever a new page is added.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestReal.MainPage">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
The problem is that the label that is supposed to say "Welcome to Xamarin Forms!" doesn't appear. I tried rotating the screen, giving it a bigger padding and margin. Giving it a different background color, foreground color and etc. However a slider or a button is displayed correctly. Just the label is not visible.
I found the solution to the issue. Since the dark mode was on on my IPhone device the text was looking white. When dark mode is turned off I am able to see the label and it works as expected.
Try this: Why won't text on a label show up on an actual device but will in the simulator? (Xcode)

Why can't I get my image aspectfit on rotated view of android emulator

I'm a beginner with Xamarin and my code is very simple
<?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:local="clr-namespace:XamarineTest2"
x:Class="XamarineTest2.MainPage">
<StackLayout>
<Image BackgroundColor="Fuchsia"
Aspect="AspectFit"
Source="Juice.jpg"/>
</StackLayout>
</ContentPage>
With the basic android emulator debugger on my visual studio 2017,
I get aspectfit image on portrait(vertical) view as expected like below.
[enter image description here][1]
But when I rotate it to landscape(horizontal) view using the button on the emulator,
I get aspectfill-like image as shown below.
[enter image description here][2]
How can I keep it aspectfit on landscape view which means the bottom of the image should not be cut off?
Thank you guys in advance.
[1]: https://i.stack.imgur.com/ddseU.jpg
[2]: https://i.stack.imgur.com/1hdDS.jpg
I found everything works fine if I use Grid instead of StackLayout. I don't know why, but there seems to be more such problem with StackLayout. Plz add more comment for anyone having a similar problem, since this issue is kind of very basic one in Xamarin.

Insert logo at the top of my project xamarin form ios, android

I have a project that this without logo written only the name of the company, as the image below
enter image description here
now I want to put the company logo on the top of my project, to stay with a designer perfect, as you can see in the image below
enter image description here
I'm using xamarin forms and developing app for android and ios, how can I do this in my project? does anyone have any examples?
EDIT
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="projeto.Views.home"
x:Name="Name"
Title="Name" Icon="">
Here is a rough layout of what your page could look like:
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="projeto.Views.home"
x:Name="Name"
Title="Name" Icon="">
<!--Content page may only have one child element-->
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Orientation="Vertical">
<Image Source="CompanyLogo.png" AspectRatio="AspectFit"/>
<StackLayout>
<!--Other content--->
</StackLayout>
</StackLayout>
</ContentPage>
Note that this very basic approach is one of several. You need to familiarise yourself with xaml and the different components you could use to achieve certain things.

Xamarin Android Images not displaying

Im trying to display an image on a kindle fire device using a Xamarin project.
The kindle device is android API level 15.
Ive tried to display the image using both URI and local resource techniques but both being used by XAML rather then code behind.
XAML code behind:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AbsoluteLayoutExercise.Exercise2">
<StackLayout>
<Image Source="Lily2.png" />
</StackLayout>
</ContentPage>
With PNG image (saved using MSPaint to make sure that it is an actual PNG) saved into resources/Drawable on the Android project.
XAML uri
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AbsoluteLayoutExercise.Exercise2">
<StackLayout>
<Image Source="http://lorempixel.com/1920/1080/nature/7" Aspect="AspectFill" />
</StackLayout>
</ContentPage>
Both these ways build successfully but when deployed to the device in question, i get nothing but a white screen.
Anyone got any words of advice?

Resources