设置可见性=“折叠"不隐藏按钮.更新:由我自己修复,但我想知道问题的原因 [英] Setting Visibility="Collapsed" doesn't hide the Button. Update: Fixed by myself but I want to know the reason for the issue

查看:27
本文介绍了设置可见性=“折叠"不隐藏按钮.更新:由我自己修复,但我想知道问题的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自定义传输控件.在那,我添加了一个 AppBarButton.为了改变它的可见性,我为它创建了一个属性,但由于某种原因,它不起作用.AppBarButton 始终可见.

I am creating a custom transport controls. In that, I have added an AppBarButton. To change the visibility of that I have created a Property for it, but for some reason, it doesn't work. The AppBarButton is always visible.

房产代码

//To change Visibility for CompactOverlayButton
public bool IsCompactOverlayButtonVisible
{
    get
    {
        return compactOverlayButton != null && compactOverlayButton.Visibility == Visibility.Visible;
    }
    set
    {
        if (compactOverlayButton != null)       //To neglect the Visibility check before the Template has been applied
        {
            compactOverlayButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}

于是我开始调试它.我在 C# 部分找不到任何错误,所以我在 XAML 部分为 AppBarButton 设置了 Visibility="Collapsed".我很惊讶,即使我设置了 Visibility="Collapsed"AppBarButton 仍然可见.

So I started debugging it. I can't find any error in the C# part, so I set Visibility="Collapsed" for AppBarButton in the XAML part. I am surprised, Even though I set Visibility="Collapsed" the AppBarButton is still visible.

这是我在 XAML 部分的代码

Here is my code in XAML part

<AppBarButton x:Name='CompactOverlayButton'
    Style='{StaticResource AppBarButtonStyle}'
    MediaTransportControlsHelper.DropoutOrder='17' Visibility="Collapsed">
    <AppBarButton.Icon>
        <FontIcon Glyph="&#xEE40;"/>
    </AppBarButton.Icon>
</AppBarButton>

更新:

我找到了导致它的代码行.它来自我使用此 CustomMediaTransportControls 的页面的 C# 部分.

I found out the line of code which is causing it. It is from the C# part of the page where I have used this CustomMediaTransportControls.

导致这种情况的线路是

var youtubeUrl = await YouTube.GetVideoUriAsync("QTYVJhy04rs", YouTubeQuality.Quality144P, videoQuality);

我通过在上面的代码行之后设置 CustomMediaControl.IsCompactOverlayButtonVisible = false; 解决了这个问题.不过,我想知道上面的行是如何影响我的程序的.完整代码已包含在供参考部分

I have fixed the issue problem by setting CustomMediaControl.IsCompactOverlayButtonVisible = false; after the above line of code. Still, I want to how the above line is affecting my program. The entire code the been included in the for reference part

参考:

这是我的完整代码

  1. CustomMediaTransportControls.cs - 来自 MediaTransportControls
  2. MediaPlayerDictionary.xaml - ResourceDictionary
  3. VideosPage.xaml - C# 部分我使用此CustomMediaTransportControls
  4. 的页面
  1. CustomMediaTransportControls.cs - Derived class from MediaTransportControls
  2. MediaPlayerDictionary.xaml - ResourceDictionary
  3. VideosPage.xaml - C# part of the page where I have used this CustomMediaTransportControls

推荐答案

我不确定删除这一行是否解决了问题,因为我在没有这行代码的情况下遇到了同样的问题.但是我找到了解决此问题的方法:

I am not sure if removing this one line resolved the issue since i had the same issue without this line of code. However i found a workaround for this problem:

为可见性以及相应的侦听器添加属性:

Add a Property for the visibility as well as the according listener:

public Visibility CompactOverlayButtonVisibility
{
    get { return (Visibility)GetValue(CompactOverlayButtonVisibilityProperty); }
    set { SetValue(CompactOverlayButtonVisibilityProperty, value); }
}

public static readonly DependencyProperty CompactOverlayButtonVisibilityProperty =
    DependencyProperty.Register(nameof(CompactOverlayButtonVisibility) , typeof(Visibility), typeof(CustomMediaTransportControls), new PropertyMetadata(Visibility.Visible, OnVisibisityChanged));


internal static void OnVisibisityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){
    if (((CustomMediaTransportControls)d).compactOverlayButton != null)
    {
        if ((Visibility)e.NewValue != Visibility.Visible)
            ((CustomMediaTransportControls)d).commandBar.PrimaryCommands.Remove(((CustomMediaTransportControls)d).compactOverlayButton);
        else if (!((CustomMediaTransportControls)d).commandBar.PrimaryCommands.Contains(((CustomMediaTransportControls)d).compactOverlayButton))
            ((CustomMediaTransportControls)d).commandBar.PrimaryCommands.Insert(4, ((CustomMediaTransportControls)d).compactOverlayButton);
        ((CustomMediaTransportControls)d).compactOverlayButton.Visibility = Visibility.Visible;
    }

并将以下内容添加到您的 OnApplyTemplateMethod 中:

And add the following to your OnApplyTemplateMethod:

commandBar = GetTemplateChild("MediaControlsCommandBar") as CommandBar;
        compactOverlayButton = GetTemplateChild("CompactOverlayButton") as AppBarButton;
if (CompactOverlayButtonVisibility != Visibility.Visible)
    commandBar.PrimaryCommands.Remove(compactOverlayButton);
else if(!commandBar.PrimaryCommands.Contains(compactOverlayButton))
    commandBar.PrimaryCommands.Insert(4, compactOverlayButton);
compactOverlayButton.Visibility = Visibility.Visible;

但是,尽管在 xaml 代码中设置了 Visibility,但您可能想要调查为什么从代码中更改了 Visibility.也许这适用于所有 AppBarButtons 也许不是.您可以尝试一下,然后在此处查看相关的源代码.如果您找到原因,您可能会想出更好的解决方案.

However you might want to investigate why the Visibility is changed from code despite being set in the xaml code. Maybe this is true for all AppBarButtons maybe not. You could try that out and then look into the relevant sourcecode here. If you found the reason you might figure out a better solution.

这篇关于设置可见性=“折叠"不隐藏按钮.更新:由我自己修复,但我想知道问题的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆