2017-01-24 2 views
0

私はビデオを再生し、別の画面に移動するためのWPFアプリケーションを持っています。アプリケーションはビデオを最初に再生し、page2.xamlにナビゲートして再びビデオページに移動しますが、以下の例外があります。wpf 'System.Windows.Controls.ContentControl.Content'が例外をスローしました。指定された要素は既に別の要素の論理的な子です。

私のXAMLは、次のようになります。

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="MainGrid">  
    <ContentControl x:Name="cntctrl" Content="{Binding VlcObject}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> 
</Grid> 

VlcObjectがVlc.DotNet.Wpf.VlcControlのインスタンスであると私はVlcObject.MediaPlayerが2ページ目に移動上に配置されていることに気づきました。したがって、ビデオページへのナビゲート時に、私はVlcObjectの新しいインスタンスを作成しています。 私のViewModelには、次のようになります。ここでは

private Vlc.DotNet.Wpf.VlcControl _vlcObject = null; 
    public Vlc.DotNet.Wpf.VlcControl VlcObject 
    { 
     get { return _vlcObject; } 
     set { Set(nameof(VlcObject), ref _vlcObject, value); } 
    } 
    private void InitialiseVLC() 
    { 
     if (VlcObject == null || VlcObject.MediaPlayer.IsDisposed) 
     { 
      DispatcherHelper.CheckBeginInvokeOnUI(() => 
      { 
       VlcObject = null; 
       VlcObject = new Vlc.DotNet.Wpf.VlcControl(); 
       VlcObject.MediaPlayer.VlcLibDirectoryNeeded += MediaPlayer_VlcLibDirectoryNeeded; 
       VlcObject.MediaPlayer.EndInit(); 
       VlcObject.MediaPlayer.Opening += MediaPlayer_Opening; 
       VlcObject.MediaPlayer.EndReached += MediaPlayer_EndReached; 
       VlcObject.MediaPlayer.EncounteredError += MediaPlayer_EncounteredError; 
       VlcObject.MediaPlayer.Disposed += MediaPlayer_Disposed; 
      }); 
     } 
    } 
    private void MediaPlayer_Disposed(object sender, EventArgs e) 
    { 
     Logger.LogInfo("Media Player is disposed."); 
     DispatcherHelper.CheckBeginInvokeOnUI(() => 
     { 
      try 
      { 
       VlcObject.MediaPlayer.VlcLibDirectoryNeeded -= MediaPlayer_VlcLibDirectoryNeeded; 
       VlcObject.MediaPlayer.Opening -= MediaPlayer_Opening; 
       VlcObject.MediaPlayer.EndReached -= MediaPlayer_EndReached; 
       VlcObject.MediaPlayer.EncounteredError -= MediaPlayer_EncounteredError; 
       VlcObject.MediaPlayer.Disposed -= MediaPlayer_Disposed; 
       //VlcObject = null; 

      } 
      catch (Exception ex) 
      { 
       Logger.LogError("Error while disposing MediaPlayer: "); 
       Logger.LogError(ex); 
      } 
     }); 
    } 
    public void OnNavigatedTo(object parameter, bool isBack = false) 
    {    
      InitializeVLC(); 
    } 

は私が手例外です:

System.Windows.Markup.XamlParseException:設定するプロパティ 'System.Windows.Controls.ContentControl.Content' 投げました例外。 ---> System.InvalidOperationException:指定された要素は既に別の要素の論理的な子です。最初に切断します。 System.Windows.Controls.ContentControl.OnContentChangedで System.Windows.FrameworkElement.AddLogicalChildで System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObjectの 新規の親)(オブジェクトの子)で システムで(オブジェクト oldContent、newContentオブジェクト)。 System.Windows.DependencyObjectでSystem.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)に System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)にWindows.Controls.ContentControl.OnContentChanged(のDependencyObject D、DependencyPropertyChangedEventArgs E) .NotifyPropertyChange(DependencyProper System.Windows.DependencyObjectでSystem.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex、DPたDependencyProperty、PropertyMetadataメタデータ、 EffectiveValueEntry oldEntry、EffectiveValueEntry & newEntry、ブール coerceWithDeferredReference、ブールcoerceWithCurrentValue、 OPERATIONTYPE OPERATIONTYPE)でtyChangedEventArgs 引数) .SetValueCommon(DPたDependencyProperty、 オブジェクト値、PropertyMetadataメタデータ、ブール coerceWithDeferredReference、ブールcoerceWithCurrentValue、 OPERATIONTYPE OPERATIONTYPE、ブールisInternal) System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(オブジェクトで例えば、オブジェクト値) MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(オブジェクト工大、 XamlMemberプロパティで MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember部材、 オブジェクトobj、Object値)で、Object value)---内部例外スタックの終わり トレース--- IXamlLineInfo lineInfo、Uri baseUri)例外: System.Windows.Markup.WpfXamlLoader.Load (XamlReader xamlReader、 IXamlObjectWriterFactory writerFactory、Boolean skipJournaledProperties、Object rootObject、XamlObjectWriterSettings 設定、Uriベース) System.Win dows.Markup.WpfXamlLoader。LoadBaml System.Windowsで System.Windows.Markup.XamlReader.LoadBaml(ストリームストリーム、ParserContext parserContext、オブジェクトの親、ブールcloseStream)で(XamlReader xamlReader、 ブールskipJournaledPropertiesは、rootObject、XamlAccessLevel ACCESSLEVEL、ウリBASEURIオブジェクト)。 MS.Internal.AppModel.AppModelKnownContentFactory.BamlConverterでApplication.LoadBamlStreamWithSyncInfo(ストリームストリーム、 ParserContextパソコン)(ストリーム ストリーム、ウリBASEURI、ブールcanUseTopLevelBrowser、ブール sandboxExternalContent、ブールallowAsync、ブール isJournalNavigation、XamlReader & asyncObjectConverter)で MS.Internal.AppModel.MimeObjectF actory.GetObjectAndCloseStream(ストリーム S、ContentTypeをcontentTypeの、ウリBASEURI、ブール canUseTopLevelBrowser、ブールsandboxExternalContent、ブール allowAsync、ブールisJournalNavigation、XamlReader & asyncObjectConverter) System.Windows.Navigation.NavigationService.GetObjectFromResponseで(WebRequestクラス 要求にWebResponse システムでSystem.Windows.Navigation.NavigateQueueItem.Dispatch(オブジェクトOBJ)で応答、ウリdestinationUri、navStateオブジェクト)System.Windows.Navigation.NavigationService.DoNavigateで
(URI源、 NavigationMode fは、navStateオブジェクト)。 Windows.Threading.ExceptionWrapper.InternalRealCall(デリゲート コールバック、いくつかのいずれかが私を助けてください MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(オブジェクト ソース、デリゲートメソッド、引数オブジェクト、のInt32 numArgs、委任 catchHandler)

で引数、のInt32 numArgs)をオブジェクト。

+0

'VlcObject'は何ですか? –

答えて

0

「最初に切断してください。ここでエラーメッセージの一部が重要です。コンテンツをバインドしているので、同じバインディングが別のコントロール(またはページ)でアクティブになっているように見えます。コンテンツが複数のコントロールに束縛されないようにする必要があります。ページは同じビューモデルを共有していますか?各ビューモデルに固有のコンテンツラッパーを使用して、各ページに固有のビューモデルを作成できます。

+0

VlcObjectは1つのコントロール、つまりContentControlにのみバインドされており、このViewModelにバインドされたビューは1つのみです。 VlcObjectはVlc.DotNet.Wpf.VlcControlのインスタンスであり、VlcObject.MediaPlayerは2ページ目に移動していることに気づきました。したがって、ビデオページへのナビゲート時に、私はVlcObjectの新しいインスタンスを作成しています。 – ramya