C# Xamarin中特定于平台的返回类型
假设我需要在Xamarin中实现一些特定于平台的代码,但是有一个特定于我需要解释的平台的返回类型。你会怎么做?我尝试过为该类型编写接口,但似乎无法使其正常工作 IAuthService.csC# Xamarin中特定于平台的返回类型,c#,xamarin,xamarin.android,C#,Xamarin,Xamarin.android,假设我需要在Xamarin中实现一些特定于平台的代码,但是有一个特定于我需要解释的平台的返回类型。你会怎么做?我尝试过为该类型编写接口,但似乎无法使其正常工作 IAuthService.cs namespace MyApp.Interfaces { public interface IAuthService { Task<IUser> signInWithEmailAndPassword(string email, string password);
namespace MyApp.Interfaces
{
public interface IAuthService
{
Task<IUser> signInWithEmailAndPassword(string email, string password);
}
public interface IUser
{
string DisplayName { get; }
string PhoneNumber { get; }
string Email { get; }
string Uid { get; }
}
}
[assembly: Dependency(typeof(AuthService))]
namespace MyApp.Droid
{
public class AuthService : IAuthService
{
public async Task<IUser> signInWithEmailAndPassword(string email, string password)
{
FirebaseAuth auth = FirebaseAuth.Instance;
IAuthResult result = await auth.SignInWithEmailAndPasswordAsync(email, password);
IUser user = result.User;
return user;
}
}
}
名称空间MyApp.Interfaces
{
公共接口IAuthService
{
使用email和password(字符串电子邮件、字符串密码)进行任务登录;
}
公共接口IUser
{
字符串DisplayName{get;}
字符串PhoneNumber{get;}
字符串电子邮件{get;}
字符串Uid{get;}
}
}
AuthService\u Droid.cs
namespace MyApp.Interfaces
{
public interface IAuthService
{
Task<IUser> signInWithEmailAndPassword(string email, string password);
}
public interface IUser
{
string DisplayName { get; }
string PhoneNumber { get; }
string Email { get; }
string Uid { get; }
}
}
[assembly: Dependency(typeof(AuthService))]
namespace MyApp.Droid
{
public class AuthService : IAuthService
{
public async Task<IUser> signInWithEmailAndPassword(string email, string password)
{
FirebaseAuth auth = FirebaseAuth.Instance;
IAuthResult result = await auth.SignInWithEmailAndPasswordAsync(email, password);
IUser user = result.User;
return user;
}
}
}
[程序集:依赖项(typeof(AuthService))]
名称空间MyApp.Droid
{
公共类AuthService:IAuthService
{
使用email和password(字符串电子邮件、字符串密码)登录公共异步任务
{
FirebaseAuth auth=FirebaseAuth.Instance;
IAuthResult=使用email和PasswordAsync(电子邮件,密码)等待身份验证登录;
IUser user=result.user;
返回用户;
}
}
}
尝试分配结果时。用户
我看到错误
无法将类型“Firebase.Auth.FirebaseUser”隐式转换为
“MyApp.Interfaces.IUser”。存在显式转换(正在进行)
你错过了一个演员吗?)
这是可能的还是只能跨平台发送基本类型?在共享代码中
public class MyUser : IUser {
... implement properties ...
}
然后在你的平台代码中
IUser user = new MyUser() {
DisplayName = result.User.DisplayName,
... assign other properties from result.User ...
};
在共享代码中
public class MyUser : IUser {
... implement properties ...
}
然后在你的平台代码中
IUser user = new MyUser() {
DisplayName = result.User.DisplayName,
... assign other properties from result.User ...
};
谢谢杰森-简单!谢谢杰森-简单!