2016-08-10 6 views
0

저는 현재 그 원인이나 해결책을 모르는 충돌로 어려움을 겪고 있습니다.요소가 이미 다른 요소의 자식입니다. - PivotItems

나는 그 정적 자원

DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=TabMainVm}" 

<Pivot ItemsSource="{Binding PivotItems}"> 

으로 XAML에서 사용 그리고 TabMainViewModel이처럼 보이는 생성자 (

ctor { ... SimpleIoc.Default.Register<TabMainViewModel>(); } 
public TabMainViewModel TabMainVm => ServiceLocator.Current.GetInstance<TabMainViewModel>(); 

이 VML에 등록 된 내 VM과 ViewModelLocator 있습니다 :

public TabMainViewModel(MenuPivotFactory factory) 
{ 
    PivotItems = new ObservableCollection<PivotItem>(factory.PivotItems); 
} 

public ObservableCollection<PivotItem> PivotItems { get; set; } 

그리고 공장은 ServiceLocator에 의해 주입됩니다 (또한 regi 스 티어드) 피벗 아이템 목록을 생성합니다.

/*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)) 
    }, 

제 객체 생성 과정이 다소 분명해지기를 바랍니다.

이제는 문제없이 다른 PivotItem 사이를 탐색 할 수 있습니다. 그러나 뒤로 이동하고 뒤로 버튼을 누르면 때때로 작동하고 때때로 App.g.i.cs에 제목에 언급 된 오류가 발생합니다. 요소는 이미 다른 요소의 하위 요소입니다.

디버깅 중에 뷰에서 VM PivotItems 속성의 get 메서드를 호출 한 다음이 오류를 throw합니다. 그래서 PivotItems 또는 다른 뷰의 뷰와 관련이 있다고 가정합니다.

이 오류가 발생하지 않도록 개체를 올바르게 만들면 어떻게 충돌없이 피벗 페이지와 다른 페이지 사이를 탐색 할 수 있습니까?

+0

당신이 선회를 제거/추가 : 여기

TabMainPageOnNavigatingFrom 방법, 해결책인가? 아니면 항상 같은 세트입니까? – Depechie

답변

0

은 내가 문제를 재현했다고 생각 : ObservableCollection<PivotItem> : 당신의 ItemsSoucePivotItem 컨트롤의 컬렉션이기 때문에

enter image description here

이입니다. TabMainPage으로 돌아 가면 public ObservableCollection<PivotItem> PivotItems { get; set; }PivotItem을 다시 입력하면 Pivot이됩니다.

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) 
{ 
    this.DataContext = null; 
    base.OnNavigatingFrom(e); 
}