我有一个WPF应用程序用于播放视频,然后导航到另一个屏幕。应用程序首先播放视频,导航到page2.xaml并再次导航到视频页面我得到以下异常。
我的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正在导航到第二页。因此,在导航到视频页面时,我正在创建一个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:设置属性&#39; System.Windows.Controls.ContentControl.Content&#39;抛出一个例外。 ---&GT; System.InvalidOperationException:指定的元素已经是另一个元素的逻辑子元素。首先断开它。在 System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject的 newParent)at System.Windows.FrameworkElement.AddLogicalChild(Object child)at System.Windows.Controls.ContentControl.OnContentChanged(对象 oldContent,Object newContent)at System.Windows.Controls.ContentControl.OnContentChanged(DependencyObject的 d,DependencyPropertyChangedEventArgs e)at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs 吃 System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs 吃 System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex,DependencyProperty dp,PropertyMetadata元数据, EffectiveValueEntry oldEntry,EffectiveValueEntry&amp; newEntry,布尔值 coerceWithDeferredReference,Boolean coerceWithCurrentValue, OperationType operationType)at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, 对象值,PropertyMetadata元数据,布尔值 coerceWithDeferredReference,Boolean coerceWithCurrentValue, OperationType operationType,Boolean isInternal)at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(对象 实例,对象值)at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember成员, 对象obj,对象值)at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember属性,Object value)---内部异常堆栈的结束 追踪--- at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo,Uri baseUri)at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory,Boolean skipJournaledProperties,Object rootObject,XamlObjectWriterSettings 设置,Uri baseUri)at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties,Object rootObject,XamlAccessLevel accessLevel,Uri baseUri)at System.Windows.Markup.XamlReader.LoadBaml(Stream stream,ParserContext parserContext,Object parent,Boolean closeStream)at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)at MS.Internal.AppModel.AppModelKnownContentFactory.BamlConverter(流 stream,Uri baseUri,Boolean canUseTopLevelBrowser,Boolean sandboxExternalContent,Boolean allowAsync,Boolean isJournalNavigation,XamlReader&amp; asyncObjectConverter)at MS.Internal.AppModel.MimeObjectFactory.GetObjectAndCloseStream(流 s,ContentType contentType,Uri baseUri,Boolean canUseTopLevelBrowser,Boolean sandboxExternalContent,Boolean allowAsync,Boolean isJournalNavigation,XamlReader&amp; asyncObjectConverter)at System.Windows.Navigation.NavigationService.GetObjectFromResponse(WebRequest的 请求,WebResponse响应,Uri destinationUri,对象navState)
在System.Windows.Navigation.NavigationService.DoNavigate(Uri source, NavigationMode f,Object navState)at System.Windows.Navigation.NavigateQueueItem.Dispatch(Object obj)at System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表 回调,对象args,Int32 numArgs)at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)
有人可以帮助我。
答案 0 :(得分:0)
“先断开连接”。错误信息的一部分是关键。由于您绑定了内容,因此它听起来像是在另一个控件(或页面)上激活相同的绑定。您应该尝试确保内容不绑定到多个控件。页面是否共享相同的视图模型?您可以尝试为每个页面创建一个唯一的视图模型,并为每个视图模型提供唯一的内容包装。
答案 1 :(得分:0)