元素已经 child 的另一个元素 - PivotItems
Element already child of another element - PivotItems
我目前正在为崩溃而苦苦挣扎,我真的不知道崩溃的根源或解决方案。
我的 VM 有一个 ViewModelLocator,它已在构造函数中注册 (
ctor { ... SimpleIoc.Default.Register<TabMainViewModel>(); }
public TabMainViewModel TabMainVm => ServiceLocator.Current.GetInstance<TabMainViewModel>();
此 VML 随后被 XAML 用作静态资源
DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=TabMainVm}"
<Pivot ItemsSource="{Binding PivotItems}">
TabMainViewModel 看起来像这样:
public TabMainViewModel(MenuPivotFactory factory)
{
PivotItems = new ObservableCollection<PivotItem>(factory.PivotItems);
}
public ObservableCollection<PivotItem> PivotItems { get; set; }
工厂由 ServiceLocator 注入(也已注册),它只是创建一个 PivotItem 的列表
/*NavigationHelper*/
private static readonly IDictionary<Type, Type> ViewModelRouting
= new Dictionary<Type, Type>
{
{typeof(MonitorViewModel), typeof(MonitorView)},
{typeof(RouteViewModel), typeof(RouteView)},
{typeof(MapViewModel), typeof(MapView)}
}
public static Page GetView(Type viewModel)
{
return (Page)Activator.CreateInstance(ViewModelRouting[viewModel]);
}
/*Factory*/
new PivotItem
{
Header = new TabHeader {Label = "Monitor", Glyph = "\uE121"},
Content = NavigationHelper.GetView(typeof(MonitorViewModel))
},
我希望这能让我的 object 创作过程更加清晰。
现在我可以毫无问题地在不同的 PivotItems 之间导航。但是当我导航离开,然后按下后退按钮时,有时它会起作用,有时我会被扔进 App.g.i.cs 并出现标题中提到的错误:元素已经是另一个元素的 child。
在调试时,我到达了 View 调用 VM PivotItems 属性 的 get 方法然后抛出此错误的地步。所以我假设它与 PivotItems 中的视图或其他东西有关。
如何正确创建 object 以便不引发此错误并且我可以在数据透视页和其他页面之间导航而不会崩溃?
我想我已经重现了你的问题:
这是因为您的 ItemsSouce
是 PivotItem
控件的集合:ObservableCollection<PivotItem>
。当您返回 TabMainPage
时,在 public ObservableCollection<PivotItem> PivotItems { get; set; }
中,相同的 PivotItem
第二次添加到 Pivot
。
这里有一个解决方法,在TabMainPage
的OnNavigatingFrom
方法中:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
this.DataContext = null;
base.OnNavigatingFrom(e);
}
我目前正在为崩溃而苦苦挣扎,我真的不知道崩溃的根源或解决方案。
我的 VM 有一个 ViewModelLocator,它已在构造函数中注册 (
ctor { ... SimpleIoc.Default.Register<TabMainViewModel>(); }
public TabMainViewModel TabMainVm => ServiceLocator.Current.GetInstance<TabMainViewModel>();
此 VML 随后被 XAML 用作静态资源
DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=TabMainVm}"
<Pivot ItemsSource="{Binding PivotItems}">
TabMainViewModel 看起来像这样:
public TabMainViewModel(MenuPivotFactory factory)
{
PivotItems = new ObservableCollection<PivotItem>(factory.PivotItems);
}
public ObservableCollection<PivotItem> PivotItems { get; set; }
工厂由 ServiceLocator 注入(也已注册),它只是创建一个 PivotItem 的列表
/*NavigationHelper*/
private static readonly IDictionary<Type, Type> ViewModelRouting
= new Dictionary<Type, Type>
{
{typeof(MonitorViewModel), typeof(MonitorView)},
{typeof(RouteViewModel), typeof(RouteView)},
{typeof(MapViewModel), typeof(MapView)}
}
public static Page GetView(Type viewModel)
{
return (Page)Activator.CreateInstance(ViewModelRouting[viewModel]);
}
/*Factory*/
new PivotItem
{
Header = new TabHeader {Label = "Monitor", Glyph = "\uE121"},
Content = NavigationHelper.GetView(typeof(MonitorViewModel))
},
我希望这能让我的 object 创作过程更加清晰。
现在我可以毫无问题地在不同的 PivotItems 之间导航。但是当我导航离开,然后按下后退按钮时,有时它会起作用,有时我会被扔进 App.g.i.cs 并出现标题中提到的错误:元素已经是另一个元素的 child。
在调试时,我到达了 View 调用 VM PivotItems 属性 的 get 方法然后抛出此错误的地步。所以我假设它与 PivotItems 中的视图或其他东西有关。
如何正确创建 object 以便不引发此错误并且我可以在数据透视页和其他页面之间导航而不会崩溃?
我想我已经重现了你的问题:
这是因为您的 ItemsSouce
是 PivotItem
控件的集合:ObservableCollection<PivotItem>
。当您返回 TabMainPage
时,在 public ObservableCollection<PivotItem> PivotItems { get; set; }
中,相同的 PivotItem
第二次添加到 Pivot
。
这里有一个解决方法,在TabMainPage
的OnNavigatingFrom
方法中:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
this.DataContext = null;
base.OnNavigatingFrom(e);
}