我正在使用Petzold使用Xamarin Forms创建移动应用程序书,将C#代码翻译成F#,其中F#代码在GitHub上不可用(他在第7章后停止发布FS).在第9章,第189页中,他使用了Dependency属性,如下所示:
[assembly: Dependency(typeof(DisplayPlatformInfo.iOS.PlatformInfo))]
namespace DisplayPlatformInfo.iOS
{
public interface IPlatformInfo
{
string GetModel();
string GetVersion();
}
using System;
using UIKit;
using Xamarin.Forms;
public class PlatformInfo : IPlatformInfo
{
UIDevice device = new UIDevice();
//etc...
Run Code Online (Sandbox Code Playgroud)
我想在F#中做相同的事情.我创建了类型,并且我可以添加该属性的唯一位置是通用的do()语句:
type PlatformInfo () =
[<assembly: Dependency(typeof(Greetings.iOS.PlatformInfo))>]
do()
interface IPlatformInfo with
member this.GetModel () =
let device = new UIDevice()
device.Model.ToString()
member this.GetVersion () =
let device = new UIDevice()
String.Format("{0} {1}", device.SystemName, device.SystemVersion)
Run Code Online (Sandbox Code Playgroud)
问题是我得到了
警告:此构造中将忽略属性.
我该如何将此属性放入类型中?
F#中的程序集级别属性需要是顶层的模块.
我将上面的C#翻译为:
namespace rec DisplayPlatformInfo.iOS
// Make a module specifically for this attribute
module DisplayPlatformAssemblyInfo =
[<assembly: Dependency(typeof(Greetings.iOS.PlatformInfo))>]
do ()
type IPlatformInfo =
abstract member GetModel : unit -> string
abstract member GetVersion : unit -> string
// ... Implement your type, etc
Run Code Online (Sandbox Code Playgroud)