查閱文檔here。
最簡單的辦法是使用編譯器指令是這樣的:
public string ImageSource {
get {
#if __IOS__
return (PaymentType == PaymentType.Check ? "check" : "card");
#endif
#if __ANDROID__
return (PaymentType == PaymentType.Check ? "check.png" : "card.png");
#endif
}
}
但也有可能是更優雅的解決方案。
namespace MyApp.ValueConverters
{
using System;
using System.Globalization;
using Xamarin.Forms;
public class ImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
switch (Device.OS)
{
case TargetPlatform.Android:
return string.Format("{0}.png", value);
default:
// No conversion for other platforms
}
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
然後設置任何所需的頁面來訪問新ImageSourceConverter:
我喜歡編譯器指令,所以我將其標記爲正確的答案。 (我真的不希望在頂部使用另一條線)。我設置了一個局部變量來保存沒有.png的fileName,然後使用編譯器指令有條件地追加.png。 –
@AaronBratcher很樂意幫忙!這似乎是現在到達您的解決方案的最快方式,祝您好運! –