Write device platform specific code in Xamarin.Forms
This is a scenario which is easily resolved with dependency injection.
Have a interface with the desired methods on your shared or PCL code, like:
public interface IUserPreferences
{
void SetString(string key, string value);
string GetString(string key);
}
Have a property on your App
class of that interface:
public class App
{
public static IUserPreferences UserPreferences { get; private set; }
public static void Init(IUserPreferences userPreferencesImpl)
{
App.UserPreferences = userPreferencesImpl;
}
(...)
}
Create platform-specific implementations on your target projects:
iOS:
public class iOSUserPreferences : IUserPreferences
{
public void SetString(string key, string value)
{
NSUserDefaults.StandardUserDefaults.SetString(value, key);
}
public string GetString(string key)
{
return NSUserDefaults.StandardUserDefaults.StringForKey(key);
}
}
Android:
public class AndroidUserPreferences : IUserPreferences
{
public void SetString(string key, string value)
{
var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
var prefsEditor = prefs.Edit();
prefEditor.PutString(key, value);
prefEditor.Commit();
}
public string GetString(string key)
{
(...)
}
}
Then on each platform-specific project create an implementation of IUserPreferences
and set it using either App.Init(new iOSUserPrefernces())
and App.Init(new AndroidUserPrefernces())
methods.
Finally, you could change your code to:
public class MyPage : ContentPage
{
public MyPage()
{
//do work to initialize MyPage
}
public void LogIn(object sender, EventArgs eventArgs)
{
bool isAuthenticated = false;
string accessToken = string.Empty;
//do work to use authentication API to validate users
if(isAuthenticated)
{
App.UserPreferences.SetString("AccessToken", accessToken);
}
}
}
Write platform specific code in crossplatform xamarin project
Welcome to stack overflow.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
Above is a link to DependencyService documentation. DependencyService allows you as a developer to call platform-specific code from your Xamarin.Forms shared code.
Xamarin Forms: how to implement platform specific code
It looks like
Write device platform specific code in Xamarin.Forms
answers the question - which is not so much a Xamarin Forms question as a Visual Studio shared project question and can be solved by creating an interface/abstract class and then implementing that for each platform and injecting the implementation.
Creating platform specific code in Xamarin?
Based on OAuth1Authenticator
and GetUI()
I assume you use Xamarin.Auth.
Xamarin.Auth supports Xamarin.Forms with 2 implementations for GetUI()
, so called Presenters (actually Dependency Service/Injection) and CustomRenderers. Presenters are more tested (thus more evidence of stabilty), but you can check the code in the repo.
Samples how to use Xamarin.Auth with the Xamarin.Forms:
https://github.com/moljac/Xamarin.Auth.Samples.NugetReferences/tree/master/Xamarin.Forms/Evolve16Labs
NOTE: this repo contains extracted samples from the main Xamarin.Auth repo, so the handling is easier and samples are updated more often.
Xamarin.Forms platform specific code
You only have one activity. Main you find it in your Droid app. If you need something specifically up in the activity code it there and initiate it from withen your Xamarin forms Page with an event. Pages don't really correlate to Activities at least not in the way your thinking.
Xamarin Forms is Cross Platform by design thus if you couldn't do it on IOS, WP and Droid you cant do it inside Forms. Since obviously theres no Activities on the other 2 platforms Forms has no concept of an activity withen the PCL.
Im not familiar enough with what a GSDEvent is to help further. But to do what you want your going to have to put an event on your ContentPage. Assign the Code to the event from the Activity in Your Droid Project and fire it inside your PCL when you want it to function.
Alternately you could create a Class in your PCL with an event on it Instantiate it in your droid project and assign the event and then put it into an IOC container that you access from the PCL.
How to write diffrent code for ios and android in xamarin.forms?
In this case you must implement dependency service
Pseudo code example
In shared code create public interface
public interface IService
{
void DoSomething();
}
In specific platform code create dependency service
[assembly: Xamarin.Forms.Dependency(typeof(MyServiceIOS))]
namespace MyNamespace.iOS
{
public class MyServiceIOS : IService
{
public void DoSomething()
{
//your code here
}
}
}
Xamarin Forms Platform-Specific SetBarSelectedItemColor has no effect
From official document , you can set static color for BarSelectedItem as follow:
<TabbedPage ...
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarItemColor="Black"
android:TabbedPage.BarSelectedItemColor="Red">
...
</TabbedPage>
Solution:
By using DynamicResource,it can set BarSelectedItemColor dynamically:
android:TabbedPage.BarSelectedItemColor="Red"
To this:
android:TabbedPage.BarSelectedItemColor="{DynamicResource BarSelectedItemColor}"
Complete sample code:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabbedPageDemo;assembly=TabbedPageDemo"
x:Class="TabbedPageDemo.TabbedPageDemoPage"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarItemColor="Black"
android:TabbedPage.BarSelectedItemColor="{DynamicResource BarSelectedItemColor}">
<TabbedPage.Resources>
<ResourceDictionary>
<Color x:Key="BlueColor">Blue</Color>
<Color x:Key="YellowColor">Yellow</Color>
</ResourceDictionary>
</TabbedPage.Resources>
...
</TabbedPage>
Where you want to change color can be set in ContentPage
as follow:
Resources["BarSelectedItemColor"] = Resources["BlueColor"];
...
Resources["BarSelectedItemColor"] = Resources["YellowColor"];
If you do not need use this renderer,you should comment its reference. My Forms answer code will work.
//[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabRenderer))]
And should remove this property in Xaml:
<TabbedPage.BarTextColor>
<OnPlatform x:TypeArguments="Color">
<On Platform="Android" Value="Green" />
</OnPlatform>
</TabbedPage.BarTextColor>
Platform specific UI adjustments in Xamarin.Forms
This is possible. You can use the Device.OnPlatform()
method. Find out more in the API docs.
Usage could be:
Device.OnPlatform (iOS: () => webView.Padding = new Thickness (0, 20, 0, 0));
20
is the height of the statusbar.
There is also some documentation about platform tweaks here.
Related Topics
Windows.Forms.Panel 32767 Size Limit
How to Use Async to Increase Winforms Performance
How to Get the Last Four Characters from a String in C#
How to Quickly Remove Items from a List
Using C# to Authenticate User Against Ldap
How to Find Certificate by Its Thumbprint in C#
Change Pinned Taskbar Icon (Windows 7)
MVC Validation Lower/Higher Than Other Value
Case Insensitive Regex Without Using Regexoptions Enumeration
Calculating Hmacsha256 Using C# to Match Payment Provider Example
Finding Property Differences Between Two C# Objects
Intersect with a Custom Iequalitycomparer Using Linq
How to Get the Executing Exe's Path in .Net
Xml File Creation Using Xdocument in C#
How to Derive Xml Element Name from an Attribute Value of a Class Using Annotations