You can handle this a lot of ways. If you want a reliable and reusable approach you can use a converter on the visibility of the button you want to hide. The XAML would be something like this:
<Page.Resources>
<Converters:HiddenWhenPhoneConverter x:Name="HidePhone" />
</Page.Resources>
<Button Visibility="{Binding, Converter={StaticResource PhoneHide}}" />
Then you would have a converter something like this:
public class HiddenWhenPhoneConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
#if WINDOWS_PHONE_APP
return Visibility.Collapsed;
#else
return Visibility.Visible;
#endif
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{ throw new NotImplementedException(); }
}
public class VisibleWhenPhoneConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
#if WINDOWS_PHONE_APP
return Visibility.Visible;
#else
return Visibility.Collapsed;
#endif
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{ throw new NotImplementedException(); }
}
Remember: from a performance point of view, this will only execute when the view is loaded. So, it should be a minimal impact and give you the results you want. There's one more thing. If you have not set the Button
or the Button's parent's DataContext
with some value, the converter will not fire. You can solve this by setting DataContext="{x:Null}"
on the Button or its parent. But, in most cases you are using MVVM
and have already set the DataContext so this is moot.