移动到弹出窗口时绑定失败

时间:2014-11-14 11:54:16

标签: binding windows-runtime winrt-xaml

我有一个滑块绑定到MediaElement的volume属性。如果标记如下,则此方法有效:

<UserControl
    x:Class="GenTest.VideoElement"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:GenTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="600">
    <UserControl.Resources>
        <Style x:Name="transportStyle"  TargetType="Button">
            <Setter Property="Height" Value="40" />
            <Setter Property="Width" Value="75" />
            <Setter Property="FontSize" Value="11" />
        </Style>
        <Style x:Name="atransportStyle"  TargetType="AppBarButton">
            <Setter Property="IsCompact" Value="true" />
            <Setter Property="FontSize" Value="11" />
        </Style>
    </UserControl.Resources>
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>

        <ContentControl x:Name="videoContainer" 
                            KeyUp="VideoContainer_KeyUp" 
                            HorizontalContentAlignment="Center" 
                            VerticalContentAlignment="Center" 
                            Height="400" Grid.Row="0" >
            <MediaElement Name="videoMediaElement"
                              MediaOpened="videoElement_MediaOpened" 
                              MediaEnded="videoMediaElement_MediaEnded" 
                              MediaFailed="videoMediaElement_MediaFailed"
                              CurrentStateChanged="videoMediaElement_CurrentStateChanged"
                              AutoPlay="False" />
        </ContentControl>
        <!-- Transport Controls -->
        <StackPanel Name="TransportControlsPanel" 
                    HorizontalAlignment="Center" 
                    Grid.Row="1" >
            <Slider Name="timelineSlider" Margin="0,0" Width="500" Height="37"/>
            <StackPanel Orientation="Horizontal">
                <AppBarButton x:Name="btnPlay" Icon="Play" Click="btnPlay_Click" Style="{StaticResource atransportStyle}" Label="Play"/>
                <AppBarButton x:Name="btnStop" Icon="Stop" Click="btnStop_Click" Style="{StaticResource atransportStyle}"/>
                <AppBarButton x:Name="btnReverse" Icon="Previous" Click="btnReverse_Click" Style="{StaticResource atransportStyle}"/>
                <AppBarButton x:Name="btnForward" Icon="Next" Click="btnForward_Click" Style="{StaticResource atransportStyle}"/>


                 <Button Name="btnFullScreenToggle" Click="btnFullScreenToggle_Click" 
                        Style="{StaticResource transportStyle}" Content="Full" />
                <ComboBox Name="cbAudioTracks"
                          SelectionChanged="cbAudioTracks_SelectionChanged" 
                          Width="75" />
                <AppBarButton x:Name="btnTest" Icon="Other" Style="{StaticResource atransportStyle}">
                    <AppBarButton.Flyout>
                        <Flyout>
                            <StackPanel Orientation="Vertical">

                            </StackPanel>
                        </Flyout>
                    </AppBarButton.Flyout>
                </AppBarButton>
                <Slider Minimum="0" Maximum="1"  StepFrequency="0.1" Height="100" Value="{Binding Mode=TwoWay, 
                    ElementName=videoMediaElement, Path=Volume}" Orientation="Vertical"/>
             </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

但是当我将滑块放入Flyout时,Binding不再有效。

<AppBarButton x:Name="btnTest" Icon="Other" Style="{StaticResource atransportStyle}">
                    <AppBarButton.Flyout>
                        <Flyout>
                            <StackPanel Orientation="Vertical">
                                <Slider Minimum="0" Maximum="1"  StepFrequency="0.1" Height="100" Value="{Binding Mode=TwoWay, 
                    ElementName=videoMediaElement, Path=Volume}" Orientation="Vertical"/>
                            </StackPanel>
                        </Flyout>
                    </AppBarButton.Flyout>
                </AppBarButton>

没有错误,我无法进行Binding跟踪,因为TraceListeners已从WinRt中删除。我试过了:

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;
    this.UnhandledException += App_UnhandledException;
    DebugSettings.BindingFailed += OnDebugSettingsOnBindingFailed;
}

private void OnDebugSettingsOnBindingFailed(object sender, BindingFailedEventArgs args)
{
    new MessageDialog(args.Message).ShowAsync();
}

但是没有抛出任何错误,输出窗口中没有任何内容。在设计模式下,使用属性Windows,如果我更改videoMediaElement的音量值,则Slider的Value属性会更改,但我无法更新滑块的值,它是只读的。

如果我使用代码,我可以在弹出窗口中使用它:

private void RangeBase_OnValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        var slider = sender as Slider;
        videoMediaElement.Volume = slider.Value; 
    }

但是为什么还要为绑定烦恼呢?

1 个答案:

答案 0 :(得分:1)

我现在有几次出现这种错误,我认为Flyouts是单独处理的,Bindings无法查找ElementName。

我建议你应该使用ViewModel,而不是在页面中交叉绑定

Slider.Value [TwoWay-Bind] - &gt; ViewModel.Volume

ViewModel.Volume - &gt; videoMediaElement.Volume