我可以从 VisualTree 中删除 ChildWindow 并将其放回以实现全屏吗?

发布于 2024-11-29 18:51:57 字数 1150 浏览 5 评论 0原文

我有一个弹出窗口,它是一个子窗口。在该弹出窗口中,我有一个 UserControl (MediaPreviewView),其中有一个 MediaElement 和一些用于控制视频的按钮。我的要求是,我需要一个全屏按钮,并在用户单击该按钮时全屏显示视频。

我正在使用 MVVM,因此我决定在 MVVM Light 中尝试使用消息传递。我正在向我的基本视图发送一条消息。在该视图的代码隐藏中,我显示了一个网格(隐藏在 XAML 的底部,具有较高的 zindex)。我的消息包含 MediaPreviewControl 并且我正在将 Grid.Children.Add( 设置为控件。我已经尝试了多种方法,并且可以使 ChildWindow 不可见,但按钮不起作用。似乎 ChildWindow即使宽度和高度为 0,仍然位于按钮顶部。 是否有更好可行的方法让我的 MediaPreviewView 全屏显示?

public class MediaPreviewFullScreenMessage
{
    public MediaPreviewView PreviewView { get; set; }
    public ChildWindow ContainerChildWindow { get; set; }
    public bool ChangeToFullScreen { get; set; }
}

// Register for FullScreen media preview
Messenger.Default.Register<MediaPreviewFullScreenMessage>(this,
(fullScreenMessage) =>
{
  this.fullScreenHolderGrid.Visibility = fullScreenMessage.ChangeToFullScreen ? Visibility.Visible : Visibility.Collapsed;
  this.fullScreenHolderGrid.Children.Clear();
  if (fullScreenMessage.ChangeToFullScreen)
  {
// I've tried, Visibility, width and height = 0 on the fullScreenMessage.ContainerChildWindow, even a TranslateTransform
....
}
});

I have a popup that is a ChildWindow. Inside that popup I have a UserControl (MediaPreviewView) that has a MediaElement and some buttons to control the video. My requirements state that I need a fullscreen button and show the video fullscreen when the user clicks on the button.

I'm using MVVM, so I decided to try this with Messaging in the MVVM Light. I'm sending a message to my base View. Inside that View's codebehind, I'm showing a Grid (hidden and at the bottom of the XAML, with a high zindex). My message contains the MediaPreviewControl and I'm setting the Grid.Children.Add( to the control. I've tried multiple things, and can get the ChildWindow to be invisible, but the buttons don't work. It seems that the ChildWindow is still on top of the buttons, even though the width and height was 0.
Is there a better a workable approach to making my MediaPreviewView fullscreen?

public class MediaPreviewFullScreenMessage
{
    public MediaPreviewView PreviewView { get; set; }
    public ChildWindow ContainerChildWindow { get; set; }
    public bool ChangeToFullScreen { get; set; }
}

// Register for FullScreen media preview
Messenger.Default.Register<MediaPreviewFullScreenMessage>(this,
(fullScreenMessage) =>
{
  this.fullScreenHolderGrid.Visibility = fullScreenMessage.ChangeToFullScreen ? Visibility.Visible : Visibility.Collapsed;
  this.fullScreenHolderGrid.Children.Clear();
  if (fullScreenMessage.ChangeToFullScreen)
  {
// I've tried, Visibility, width and height = 0 on the fullScreenMessage.ContainerChildWindow, even a TranslateTransform
....
}
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2024-12-06 18:51:57

简单地最大化 ChildWindow 怎么样?在您的全屏 Button 后面,

    private void FullScreenButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // get the explorer window size
        double height = App.Current.Host.Content.ActualHeight;
        double width = App.Current.Host.Content.ActualWidth;

        // need to make it a little bit smaller or it won't resize
        this.Height = height - 1;
        this.Width = width - 1;

        // need to update the layout here
        this.UpdateLayout();

        // the following code repositions the child window
        var root = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
        if (root == null)
        {
            return;
        }

        var contentRoot = root.FindName("ContentRoot") as FrameworkElement;
        if (contentRoot == null)
        {
            return;
        }

        var group = contentRoot.RenderTransform as TransformGroup;
        if (group == null)
        {
            return;
        }

        TranslateTransform translateTransform = null;
        foreach (var transform in group.Children.OfType<TranslateTransform>())
        {
            translateTransform = transform;
        }

        if (translateTransform == null)
        {
            return;
        }

        // reset transform
        translateTransform.X = 0.0;
        translateTransform.Y = 0.0;
    }

UPDATE

ChildWindow 的默认样式中,它的 Grid 称为ContentRoot 通过 RenderTransform 设置 ChildWindow 的位置。这就是为什么您需要重置 TranslateXTranslateY 以使其位于左上角。

                        <Grid x:Name="ContentRoot" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Height="{TemplateBinding Height}" RenderTransformOrigin="0.5,0.5" VerticalAlignment="{TemplateBinding VerticalAlignment}" Width="{TemplateBinding Width}">
                            <Grid.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Grid.RenderTransform>

How about just simply maximising the ChildWindow? Behind your full screen Button you do,

    private void FullScreenButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // get the explorer window size
        double height = App.Current.Host.Content.ActualHeight;
        double width = App.Current.Host.Content.ActualWidth;

        // need to make it a little bit smaller or it won't resize
        this.Height = height - 1;
        this.Width = width - 1;

        // need to update the layout here
        this.UpdateLayout();

        // the following code repositions the child window
        var root = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
        if (root == null)
        {
            return;
        }

        var contentRoot = root.FindName("ContentRoot") as FrameworkElement;
        if (contentRoot == null)
        {
            return;
        }

        var group = contentRoot.RenderTransform as TransformGroup;
        if (group == null)
        {
            return;
        }

        TranslateTransform translateTransform = null;
        foreach (var transform in group.Children.OfType<TranslateTransform>())
        {
            translateTransform = transform;
        }

        if (translateTransform == null)
        {
            return;
        }

        // reset transform
        translateTransform.X = 0.0;
        translateTransform.Y = 0.0;
    }

UPDATE

In the ChildWindow's default style, it has this Grid called ContentRoot which sets the position of the ChildWindow by RenderTransform. That's why you need to reset the TranslateX and TranslateY to make it top left.

                        <Grid x:Name="ContentRoot" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Height="{TemplateBinding Height}" RenderTransformOrigin="0.5,0.5" VerticalAlignment="{TemplateBinding VerticalAlignment}" Width="{TemplateBinding Width}">
                            <Grid.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Grid.RenderTransform>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文