问题描述
我有一个来自 MediaTransportControls
的派生类,并尝试根据某些事件禁用上一个和下一个跟踪按钮。我写了以下
I have a derived class from MediaTransportControls
and trying to disable previous and next track buttons based on some events. I wrote following
public class MyMediaTransportControls : MediaTransportControls
{
public static readonly DependencyProperty IsPreviousTrackButtonEnabledProperty = DependencyProperty.Register(
"IsPreviousTrackButtonEnabled", typeof(bool), typeof(MyMediaTransportControls), new PropertyMetadata(false, IsPreviousTrackButtonEnabledChangedCallback));
private static async void IsPreviousTrackButtonEnabledChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var mediaTransportControls = d as MyMediaTransportControls;
if(mediaTransportControls?.GetTemplateChild("PreviousTrackButton") is Button previousTrackButton)
{
await mediaTransportControls.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
previousTrackButton.IsEnabled = (bool)e.NewValue;
});
}
}
public bool IsPreviousTrackButtonEnabled
{
get { return (bool) GetValue(IsPreviousTrackButtonEnabledProperty); }
set { SetValue(IsPreviousTrackButtonEnabledProperty, value); }
}
}
我已经使用数据绑定来绑定 IsPreviousTrackButtonEnabled
和ViewModel逻辑。但是,我发现即使设置 previousTrackButton.IsEnabled
,它也不会影响UI。不过有时,在调试时,我可以看到对UI的影响。
I have used data binding to bind IsPreviousTrackButtonEnabled
with the ViewModel logic. However, I find that even when previousTrackButton.IsEnabled
is set it has no impact on the UI. Sometime, though, when debuging, I can see the impact on UI.
更新
MediaTransportControls
提供自己的 ControlTemplate
,我希望不要创建自己的模板并对其进行数据绑定。
The MediaTransportControls
provide its own ControlTemplate
and I wish not to create my own template and do data binding with it.
推荐答案
写入通知属性已更改的事件处理程序
Write Notify Property Changed Event Handler
INotifyPropertyChanged
这篇关于无法使用Dispatcher从背后的代码更新UWP UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!