C# 更改的屏幕方向MediaElement从屏幕中消失

C# 更改的屏幕方向MediaElement从屏幕中消失,c#,wpf,windows-runtime,windows-store-apps,findcontrol,C#,Wpf,Windows Runtime,Windows Store Apps,Findcontrol,我的项目有一个有趣的问题。我正在研究应用程序的方向,我的问题是,当我改变屏幕方向时,我的视频元素消失了。我对我的图像使用了相同的代码,它工作正常。所以,我不明白问题是什么。有什么想法吗?这里是我的代码 对于mediaelement,此行返回空值: MediaElement mediaControl = RecurseChildren<MediaElement>(fwQuestions).Where(p => p.Name == "myVideoElement" + elemen

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

对于mediaelement,此行返回空值:

MediaElement mediaControl = RecurseChildren<MediaElement>(fwQuestions).Where(p => p.Name == "myVideoElement" + elementOrder).FirstOrDefault();
MediaElement mediaControl=RecurseChildren(fwQuestions).Where(p=>p.Name==“myVideoElement”+elementOrder).FirstOrDefault();
但这行相同的结构是正确返回我的图像:

Image ImageController = RecurseChildren<Image>(fwQuestions).Where(p => p.Name == "QuestionImage" + elementOrder).FirstOrDefault();
Image-ImageController=RecurseChildren(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);
                    }
                }

            }
        }
MediaElement myVideoElement=新的MediaElement();
myVideoElement.Name=“myVideoElement”+当前问题;
myVideoElement.Width=(Window.Current.Bounds.Width*0.2)-10;
myVideoElement.高度=300;
myVideoElement.Margin=新厚度(10);
myVideoElement.Source=新Uri(AppConfiguration.TestVideoLink+questionVideoLink,UriKind.Absolute);
myVideoElement.AutoPlay=false;
myVideoElement.Margin=新厚度(0,10,0,10);
myVideoElement.AreTransportControlsEnabled=true;
myVideoElement.HorizontalAlignment=HorizontalAlignment.Center;
myVideoElement.Visibility=可见性。已折叠;
spMediaBody.Children.Add(myVideoElement);
DynamicGrid.Children.Add(spMediaBody);
pnlGeneralBody.Children.Add(DynamicGrid);
fwQuestions.Items.Add(pnlGeneralBody);
公共静态IEnumerable RecurseChildren(DependencyObject根),其中T:UIElement
{
if(根是T)
{
以T为单位产生回根;
}
if(root!=null)
{
var count=VisualTreeHelper.GetChildrenCount(根);
对于(var idx=0;idxp.Name==“DynamicGrid”+elementOrder.FirstOrDefault();
MediaElement mediaControl=RecurseChildren(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=新网格长度((Window.Current.Bounds.Width*0.2)-5,GridUnitType.Pixel);
grdBody.ColumnDefinitions[1].Width=新网格长度((Window.Current.Bounds.Width*0.8)-5,GridUnitType.Pixel);
}
其他的
{
grdBody.ColumnDefinitions[0].Width=新网格长度((Window.Current.Bounds.Width*0),GridUnitType.Pixel);
grdBody.ColumnDefinitions[1].Width=新网格长度((Window.Current.Bounds.Width*1)-10,GridUnitType.Pixel);
}
MediaElement AudioControl=RecurseChildren(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=新网格长度((Window.Current.Bounds.Width*0.2)-5,GridUnitType.Pixel);
grdBody.ColumnDefinitions[1].Width=新网格长度((Window.Current.Bounds.Width*0.8)-5,GridUnitType.Pixel);
}
其他的
{
grdBody.ColumnDefinitions[0].Width=新网格长度((Window.Current.Bounds.Width*0),GridUnitType.Pixel);
grdBody.ColumnDefinitions[1].Width=新网格长度((Window.Current.Bounds.Width*1)-10,GridUnitType.Pixel);
}
Image ImageController=RecurseChildren(fwQuestions).Where(p=>p.Name==“QuestionImage”+elementOrder).FirstOrDefault();
if(ImageController!=null&&ImageController.Visibility==Visibility.Visible)
{
宽度=(Window.Current.Bounds.Width*0.2)-10;
grdBody.ColumnDefinitions[0].Width=新网格长度((Window.Current.Bounds.Width*0.2)-5,GridUnitType.Pixel);
grdBody.ColumnDefinitions[1].Width=新网格长度((Window.Current.Bounds.Width*0.8)-5,GridUnitType.Pixel);
}
其他的
{
grdBody.ColumnDefinitions[0].Width=新网格长度((Window.Current.Bounds.Width*0),GridUnitType.Pixel);
grdBody.ColumnDefinitions[1].Width=新网格长度((Window.Current.Bounds.Width*1)-10,GridUnitType.Pixel);
}
}
}
}

mediaControl是否正确查找元素?通常,我使用以下代码查找mediaControl。MediaElement mediaControl=RecurseChildren(fwQuestions).Where(p=>p.Name==“myVideoElement”+elementOrder).FirstOrDefault();但是,当我将方向更改为PortIt或横向视频元素时,无法使用相同的代码看到它们。同样的事,同样的事