gpt4 book ai didi

移至弹出窗口时绑定(bind)失败

转载 作者:行者123 更新时间:2023-12-02 13:56:51 25 4
gpt4 key购买 nike

我有一个 slider 绑定(bind)到 MediaElement 的音量属性。如果标记如下,则此方法有效:

<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>

但是当我将 slider 放入弹出窗口时,绑定(bind)不再起作用。

<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>

没有错误,我无法进行绑定(bind)跟踪,因为 TraceListener 已从 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();
}

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

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

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

但是为什么还要费心绑定(bind)呢?

最佳答案

我现在有时会遇到这种错误,我认为 Flyouts 是单独处理的,其中 Bindings 无法查找 ElementName。

我建议您应该使用 ViewModel 来执行此操作,而不是在页面中进行交叉绑定(bind)

Slider.Value [TwoWay-Bind] -> ViewModel.Volume

ViewModel.Volume -> videoMediaElement.Volume

关于移至弹出窗口时绑定(bind)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26929456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com