试图使一个跨平台的应用程序库,将能够在 UWP,WinForms,WPF,Android 和 iOS 的工作。
使用 NetStandard2.0
我一直在使用 Xamarin Forms 来测试库,因为跨平台选项,但我想做的是获取硬件信息并模拟音频输出等设备。
我找不到一种适用于所有平台 / 设备的方法,只有那些针对特定的。我找不到一种方法来运行特定的代码或使用基于正在运行的平台的特定库。
尝试创建专门针对 UWP,Android 和 iOS 等平台的单独库,但在引用它们时,我得到一个不支持的错误:
Project ****.UWP is not compatible with netstandard2.0 (.NETStandard,Version=v2.0).
Project ****.UWP supports: uap10.0.17763 (UAP,Version=v10.0.17763)
这样的东西,并为每个项目(UWP,Android 和 iOS)
我能做什么?
定位多个框架不起作用,因为它们会导致所有其他文件中的错误。尝试创建专门针对 UWP 等平台的单独库,
平台特定的代码不能直接用于共享库。为每个平台调用本机 api。更好的方法是使用DependencyService
方法。这里是official document,你可以参考。
另一种方法是使用Conditional Compilation
例如
public static string DatabaseFilePath
{
get
{
var filename = "TodoDatabase.db3";
#if SILVERLIGHT
// Windows Phone 8
var path = filename;
#else
#if __ANDROID__
string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
#else
#if __IOS__
// we need to put in /Library/ on iOS5.1 to meet Apple's iCloud terms
// (they don't want non-user-generated data in Documents)
string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder
string libraryPath = Path.Combine (documentsPath, "..", "Library");
#else
// UWP
string libraryPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
#endif
#endif
var path = Path.Combine(libraryPath, filename);
#endif
return path;
}
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(16条)