更改的屏幕方向MediaElement从屏幕消失

时间:2014-04-20 15:21:53

标签: c# wpf windows-runtime windows-store-apps findcontrol

我的项目有一个有趣的问题。我正在研究应用程序的方向,我的问题是,当我更改屏幕方向纵向或横向时,我的视频元素已消失。我为我的图像使用相同的代码,它正常工作。所以,我不明白是什么问题。有什么想法吗?我的代码在这里;

此行为mediaelement返回null:

MediaElement mediaControl = RecurseChildren<MediaElement>(fwQuestions).Where(p => p.Name == "myVideoElement" + elementOrder).FirstOrDefault();

但是这行相同的结构正确地返回了我的图像:

Image ImageController = RecurseChildren<Image>(fwQuestions).Where(p => p.Name == "QuestionImage" + elementOrder).FirstOrDefault();

我的代码是:

MediaElement myVideoElement = new MediaElement();
myVideoElement.Name = "myVideoElement" + currentQuestion;
myVideoElement.Width = (Window.Current.Bounds.Width * 0.2) - 10;
myVideoElement.Height = 300;
myVideoElement.Margin = new Thickness(10);
myVideoElement.Source = new Uri(AppConfiguration.TestVideoLink + questionVideoLink, UriKind.Absolute);
myVideoElement.AutoPlay = false;
myVideoElement.Margin = new Thickness(0, 10, 0, 10);
myVideoElement.AreTransportControlsEnabled = true;
myVideoElement.HorizontalAlignment = HorizontalAlignment.Center;
myVideoElement.Visibility = Visibility.Collapsed;
spMediaBody.Children.Add(myVideoElement);

DynamicGrid.Children.Add(spMediaBody);
pnlGeneralBody.Children.Add(DynamicGrid);
fwQuestions.Items.Add(pnlGeneralBody);

public static IEnumerable<T> RecurseChildren<T>(DependencyObject root) where T : UIElement
        {
            if (root is T)
            {
                yield return root as T;
            }

            if (root != null)
            {
                var count = VisualTreeHelper.GetChildrenCount(root);


                for (var idx = 0; idx < count; idx++)
                {
                    foreach (var child in RecurseChildren<T>(VisualTreeHelper.GetChild(root, idx)))
                    {
                        yield return child;
                    }
                }
            }
        }

private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (IsPageLoadComplete)
            {
                for (int i = 0; i < AppConfiguration.QuestionList.allques.Count; i++)
                {
                    string elementOrder = i.ToString();


                    Grid grdBody = RecurseChildren<Grid>(fwQuestions).Where(p => p.Name == "DynamicGrid" + elementOrder).FirstOrDefault();
                    MediaElement mediaControl = RecurseChildren<MediaElement>(fwQuestions).Where(p => p.Name == "myVideoElement" + elementOrder).FirstOrDefault();
                    if (mediaControl != null && mediaControl.Visibility == Visibility.Visible)
                    {
                        mediaControl.Width = (Window.Current.Bounds.Width * 0.2) - 10;
                        grdBody.ColumnDefinitions[0].Width = new GridLength((Window.Current.Bounds.Width * 0.2) - 5, GridUnitType.Pixel);
                        grdBody.ColumnDefinitions[1].Width = new GridLength((Window.Current.Bounds.Width * 0.8) - 5, GridUnitType.Pixel);
                    }
                    else
                    {
                        grdBody.ColumnDefinitions[0].Width = new GridLength((Window.Current.Bounds.Width * 0), GridUnitType.Pixel);
                        grdBody.ColumnDefinitions[1].Width = new GridLength((Window.Current.Bounds.Width * 1) - 10, GridUnitType.Pixel);
                    }

                    MediaElement AudioControl = RecurseChildren<MediaElement>(fwQuestions).Where(p => p.Name == "myAudioElement" + elementOrder).FirstOrDefault();
                    if (AudioControl != null && AudioControl.Visibility == Visibility.Visible)
                    {
                        AudioControl.Width = (Window.Current.Bounds.Width * 0.2) - 10;
                        grdBody.ColumnDefinitions[0].Width = new GridLength((Window.Current.Bounds.Width * 0.2) - 5, GridUnitType.Pixel);
                        grdBody.ColumnDefinitions[1].Width = new GridLength((Window.Current.Bounds.Width * 0.8) - 5, GridUnitType.Pixel);
                    }
                    else
                    {
                        grdBody.ColumnDefinitions[0].Width = new GridLength((Window.Current.Bounds.Width * 0), GridUnitType.Pixel);
                        grdBody.ColumnDefinitions[1].Width = new GridLength((Window.Current.Bounds.Width * 1) - 10, GridUnitType.Pixel);
                    }

                    Image ImageController = RecurseChildren<Image>(fwQuestions).Where(p => p.Name == "QuestionImage" + elementOrder).FirstOrDefault();
                    if (ImageController != null && ImageController.Visibility == Visibility.Visible)
                    {
                        ImageController.Width = (Window.Current.Bounds.Width * 0.2) - 10;
                        grdBody.ColumnDefinitions[0].Width = new GridLength((Window.Current.Bounds.Width * 0.2) - 5, GridUnitType.Pixel);
                        grdBody.ColumnDefinitions[1].Width = new GridLength((Window.Current.Bounds.Width * 0.8) - 5, GridUnitType.Pixel);
                    }
                    else
                    {
                        grdBody.ColumnDefinitions[0].Width = new GridLength((Window.Current.Bounds.Width * 0), GridUnitType.Pixel);
                        grdBody.ColumnDefinitions[1].Width = new GridLength((Window.Current.Bounds.Width * 1) - 10, GridUnitType.Pixel);
                    }
                }

            }
        }

0 个答案:

没有答案