条件编译似乎在Xamarin Studio中不起作用 [英] Conditional Compilation seems to be not working in Xamarin Studio

查看:124
本文介绍了条件编译似乎在Xamarin Studio中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Xamarin Forms应用程序. 并在带有名为"MyLabel"的标签的新页面内. 在我的页面后面的代码中,我有

I created a Xamarin Forms app. And inside a new page with a label named "MyLabel". In the code behind for my page I have

private void SetUpUI()
{
    #if __IOS__
    this.MyLabel.BackgroundColor = Color.Navy;
    #endif
}

在我的iOS项目选项中,我可以在编译器"选项卡中看到符号__IOS__. (请参见屏幕截图)

In my iOS project options I can see symbol __IOS__ in the "Compiler" tab. (please see screenshot)

当我在iOS中运行时,它不会使标签变成蓝色:

When I run in iOS it doesn't make the label blue:

但是,如果我移除#if __IOS__块,则会使标签变成蓝色:

But if I remove #if __IOS__ block it makes the label blue:

因此,似乎条件编译不起作用. 我在Mac上.因此无法在Visual Studio上进行测试. 卡住了很长时间,但无法弄清我错过了什么.

So it seems conditional compilation is not working. I'm on a Mac. So couldn't test on Visual Studio. Stuck with it for a long time but cannot figure out what I missed.

推荐答案

您正在您的 PCL项目中使用条件语句,该条件语句不包含那些编译器定义,因此,为什么条件代码为变灰了.

You are using the conditionals in your PCL project which would not contain those compiler defines, thus why your conditional code is greyed out.

在您的PCL项目中,您可以使用Device.OnPlatform来执行基于平台的处理:

In your PCL project you can use Device.OnPlatform to perform platform based processing:

Device.OnPlatform (iOS: () => this.MyLabel.BackgroundColor = Color.Navy; );

re: https://developer.xamarin.com/api/member/Xamarin.Forms.Device.OnPlatform/

这篇关于条件编译似乎在Xamarin Studio中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆