MVVM Xamarin 应用程序
MVVM Xamarin app
我正在使用 Xamarin 构建一个应用程序,使用 PCL 项目,所以我在那里有我的核心功能,在不同的项目中具有特定于平台的实现。我使用的是 MVVM 结构,但使用 Xamarin 时我遇到了复杂的事情。我正在使用 Xamarin.Auth,它允许 OAuth2 登录,但它仅适用于 .ios 和 .droid 项目。
所以我在核心项目的视图上有一个 "Log in with?" 提示。然后我转到调用自定义渲染器的平台特定项目(显示的模型 here)。
这里是renderer中的代码,需要根据用户选择的authenticator"customised"
var auth = new OAuth2Authenticator (
clientId: "xxx", // your OAuth2 client id (For FB Also called App-ID)
scope: "", // the scopes for the particular API you're accessing, delimited by "+" symbols
authorizeUrl: new Uri ("https://m.facebook.com/dialog/oauth/"), // the auth URL for the service (i.e FB, Twitter)
redirectUrl: new Uri ("http://www.facebook.com/connect/login_success.html")); // the redirect URL for the service
正确的做法是什么?我是否使用 public 变量来传达这一点,传递细节?等。最简单的解决方案是什么?
通常您会在 PCL 中创建一个接口,然后在实现它的特定于平台的项目中创建 classes。
你的情况可能是这样的
public interface IAuthenticateService
{
Task<AuthenticateResult> AuthenticateAsync(string clientId, CancellationToken cancellationToken)
}
AuthenticateResult 应该是在 PCL 中也可用的 class。
一旦你得到它,你只需将接口注入你的视图模型。
请注意,您必须在特定于平台的项目中注册您的接口实现。
MVVMCross 是一个相对较好的框架,可以帮助您实现您想要实现的目标。
你必须创建一个依赖项class,这是正确的方法。
不要使用渲染器,如果可能的话,在核心中完成所有可视化工作,并让 OAuth 的具体内容成为 class。
为此,您创建一个接口来公开您需要使用的方法,然后在特定项目中创建一个 class 实现该接口并使用 Dependency 属性进行装饰。
当您需要使用它时,您向 DependencyService 请求接口实例,它将从特定项目中检索正确的 class。
这里是关于 DependencyService 的文档:https://developer.xamarin.com/guides/xamarin-forms/dependency-service/
我正在使用 Xamarin 构建一个应用程序,使用 PCL 项目,所以我在那里有我的核心功能,在不同的项目中具有特定于平台的实现。我使用的是 MVVM 结构,但使用 Xamarin 时我遇到了复杂的事情。我正在使用 Xamarin.Auth,它允许 OAuth2 登录,但它仅适用于 .ios 和 .droid 项目。
所以我在核心项目的视图上有一个 "Log in with?" 提示。然后我转到调用自定义渲染器的平台特定项目(显示的模型 here)。
这里是renderer中的代码,需要根据用户选择的authenticator"customised"
var auth = new OAuth2Authenticator (
clientId: "xxx", // your OAuth2 client id (For FB Also called App-ID)
scope: "", // the scopes for the particular API you're accessing, delimited by "+" symbols
authorizeUrl: new Uri ("https://m.facebook.com/dialog/oauth/"), // the auth URL for the service (i.e FB, Twitter)
redirectUrl: new Uri ("http://www.facebook.com/connect/login_success.html")); // the redirect URL for the service
正确的做法是什么?我是否使用 public 变量来传达这一点,传递细节?等。最简单的解决方案是什么?
通常您会在 PCL 中创建一个接口,然后在实现它的特定于平台的项目中创建 classes。
你的情况可能是这样的
public interface IAuthenticateService
{
Task<AuthenticateResult> AuthenticateAsync(string clientId, CancellationToken cancellationToken)
}
AuthenticateResult 应该是在 PCL 中也可用的 class。 一旦你得到它,你只需将接口注入你的视图模型。 请注意,您必须在特定于平台的项目中注册您的接口实现。
MVVMCross 是一个相对较好的框架,可以帮助您实现您想要实现的目标。
你必须创建一个依赖项class,这是正确的方法。
不要使用渲染器,如果可能的话,在核心中完成所有可视化工作,并让 OAuth 的具体内容成为 class。
为此,您创建一个接口来公开您需要使用的方法,然后在特定项目中创建一个 class 实现该接口并使用 Dependency 属性进行装饰。
当您需要使用它时,您向 DependencyService 请求接口实例,它将从特定项目中检索正确的 class。
这里是关于 DependencyService 的文档:https://developer.xamarin.com/guides/xamarin-forms/dependency-service/