【问题标题】:How to bind appbar's visibility by code?如何通过代码绑定appbar的可见性?
【发布时间】:2013-10-22 23:18:05
【问题描述】:

如何在代码中设置 MultiApplicationBarBehavior.IsVisible 绑定?

问题:如果通过xaml绑定,即使绑定值为false也会闪烁。

EDIT1:那么,我要绑定什么?

<mainPivot:SplashScreenControl 
        Opacity="1"
        Name="SplashScreen"
        Visibility="{Binding Opacity, ElementName=SplashScreen, Converter={StaticResource DoubleToVisibilityConverter}}"
        />

 <cimbalino:MultiApplicationBarBehavior 
            SelectedIndex="{Binding PivotIndex}" 
            IsVisible="{Binding Opacity, ElementName=SplashScreen, Converter={StaticResource DoubleToBooleanInversedConverter}}"
            >

Splashscreen:可见性绑定到不透明度,因为不透明度=0 的可见对象仍在处理输入。

Appbar 只是绑定到 Splashscreen 的不透明度。根本没有代码隐藏(只是注释掉了所有内容)。但是,appbar 在页面加载期间闪烁。这就是为什么我想将其默认设置为 false 并稍后通过代码绑定。

appbar 不闪烁的唯一情况是绑定到自定义属性时,该属性在初始化期间设置为 false

<cimbalino:MultiApplicationBarBehavior 
            SelectedIndex="{Binding PivotIndex}" 
            IsVisible="{Binding IsSplashScreenVisible, Converter={StaticResource BooleanInversedConverter}}"
            >

转换器:

public class DoubleToBooleanInversedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return true;

        return !((value as double?) > 0);
    }
}

【问题讨论】:

    标签: c# windows-phone-7 mvvm appbar cimbalino


    【解决方案1】:

    我认为在代码中进行绑定不会有帮助,您是否确保在执行页面的构造函数时将绑定的值设置为 false(并且在构造函数中设置了 DataContext) ?
    如果您要绑定到构造函数中为 null 的某个对象属性,您可以在绑定上添加 FallbackValue=false。

    如果您没有找到任何其他解决方案,这里是如何在代码中创建相同的绑定:

    Binding binding = new Binding("Opacity");
    binding.ElementName = "SplashScreen";
    binding.Converter =  new DoubleToBooleanInversedConverter();
    multiAppBarBehavior.SetBinding(MultiApplicationBarBehavior.IsVisibleProperty, binding);
    

    (其中 multiAppBarBehavior 将是 MultiApplicationBarBehavior 控件名称)

    【讨论】:

    • 嗨,很高兴见到你。更新的问题。
    • 你页面的DataContext是用xaml设置的吗?如果是这种情况,您何时设置 IsSplashScreenVisible?
    • 添加了在代码中创建绑定的代码,尽管我认为如果在构建页面时将 IsSplashScreenVisible 正确设置为 false,您需要这样做
    • 是的,DataContext 是由 ViewModelLocator 在 xaml 中设置的。我默认设置 IsSplashScreenVisible:var isSplashScreenVisible = false;
    • 好的,现在更清楚了,我更新了代码以在代码中创建相同的绑定。如果您正在使用情节提要“逐步”更改不透明度值,那么像您一样直接绑定到该值可能不是一个好主意,因为每次情节提要修改不透明度值时都会强制执行转换器代码(所以不透明动画看起来不那么流畅)。在这种情况下,最好在情节提要中适当地设置值。
    猜你喜欢
    • 2010-11-23
    • 1970-01-01
    • 2020-10-14
    • 2021-11-15
    • 2012-05-15
    • 2011-12-21
    • 1970-01-01
    • 2018-03-23
    • 2011-01-08
    相关资源
    最近更新 更多