【问题标题】:Unable to Acess Native code (element) in Xamarin Forms with NETStandard 2.0无法使用 NET Standard 2.0 访问 Xamarin 表单中的本机代码(元素)
【发布时间】:2018-08-28 14:13:28
【问题描述】:

我的部分课程没有针对 NETStandard 2.0 的 Xamarin Forms 项目中的 #if

问题的一个例子是:

     public partial class App : Application
       {
       #region StaticString
        #if __IOS__
        public static String A
        #endif
        #if __ANDROID__
        public static String A
        #endif
        #if WINDOWS_UWP
        public static String A;         
        #endif
       #endregion

     public App ()
           {
            InitializeComponent();
            InitializeApplication();

           #region Teste
            #if __IOS__
            A="ios";
            #endif
            #if __ANDROID__
            A="droid";
            #endif
            #if WINDOWS_UWP
            A="UWP;         
            #endif
          #endregion
          }

        }

我需要为我的视图模型访问和发送特定代码,但对于另一个类(例如这个字符串 A)来说,它是无法和不可见的。

我在“Accessing Native Views in Code”中看到了一个示例/方式

它只适用于共享代码吗?

问候

【问题讨论】:

  • 您用于平台条件的代码在共享项目中使用 Device.OnPlatform 代替
  • #if是COMPILER指令,编译器不知道运行时平台是什么
  • 喜欢这个? switch(Device.RuntimePlatform) { case Device.iOS: A="ios";休息;案例 Device.UWP: A="UWP";休息;默认值:A="android" 中断; }

标签: xamarin xamarin.forms .net-standard-2.0


【解决方案1】:

编译器指令仅在共享代码中可用,Device.OnPlatform 已弃用。解决方案是在 switch 语句中使用Device.RuntimePlatform。这将使您的代码看起来像:

public static string A;
switch (Device.RuntimePlatform)
{
    case Device.iOS:
        A = "ios";
        break;
    case Device.Android:
        A = "droid";
        break;
    case Device.UWP:
        A = "UWP";
        break;
    default:
        A = "unknown";
        break;
}

查看Microsoft docs 了解有关使用Device.RuntimePlatform 的信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多