【问题标题】:Platform defines in XAMLXAML 中的平台定义
【发布时间】:2014-08-22 17:21:37
【问题描述】:
对于我的通用 WP8.1/WS8.1 应用程序,我需要 MainPage.xaml 中的平台特定代码。我有一个 webView,单击后退按钮时应该返回,但我在 WS 上没有本机后退按钮,那么如何才能使此代码仅适用于 WS8.1?
<Button Content="Back" HorizontalAlignment="Center" Margin="0,-10,0,0"
Width="100" Height="70"
VerticalAlignment="Top" Click="WebView_GoBack"
Style="{StaticResource NavigationBackButtonNormalStyle }"
></Button>
【问题讨论】:
标签:
xaml
windows-8.1
windows-phone-8.1
【解决方案1】:
你可以创建一个类
public class PlatformSpecificFlags
{
/// <summary>
/// Returns true if on windows store, otherwise false.
/// </summary>
public bool IsOnWindowsStore {
get
{
#if NETFX_CORE
return true;
#else
return false;
#endif
} }
}
您还可以根据需要为其他平台定义其他属性。
在 App.xaml 中定义一个资源:
<PlatformSpecificFlags x:Key="PlatformSpecificFlags"/>
然后像这样在xaml中使用它
<Button
Content="Back"
HorizontalAlignment="Center"
Margin="0,-10,0,0"
Width="100"
Height="70"
VerticalAlignment="Top"
Click="WebView_GoBack"
Style="{StaticResource NavigationBackButtonNormalStyle }"
Visibility="{Binding Source={StaticResource PlatformSpecificFlags}, Path=IsOnWindowsStore, Converter={StaticResource BoolToNegateVisibilityConverter}}"></Button>