我有以下Xamarin.Forms.ContentPage
类的结构。
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)
{
//I would to write device specific code to write to the access token to the device
//Example of saving the access token to iOS device
NSUserDefaults.StandardUserDefaults.SetString(accessToken, "AccessToken");
//Example of saving the access token to Android device
var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
var prefsEditor = prefs.Edit();
prefEditor.PutString("AccessToken", accessToken);
prefEditor.Commit();
}
}
}
我希望在MyPage
的LogIn
方法中编写特定于平台的代码,根据用户在我的应用上使用的设备操作系统保存访问令牌。
当用户在其设备上使用我的应用程序时,如何只运行特定于设备的代码?
App
类的代码片段中,我尝试编写代码时遇到了“方法必须具有返回类型”的提示。它是否应该具有void
的返回类型? - Michael Kniskernvoid
。 - valdeteroDependencyService
也值得一看。 - Pedro