2017-02-15 13 views
0

이 나의 주, 부모 클래스, 뷰 모델입니다 :Caliburn.Micro : 지휘자가있는 페이지 간 이동 : 하위 클래스에서 부모 클래스 메서드 또는 속성을 호출하려면 어떻게해야합니까?

[AutofacRegisterType(PAGE_NAME, typeof(IMainPage), IsSingleInstance = false)] 
public class MainPageViewModel : MainPageViewModelBase 
{ 
    public const string PAGE_NAME = "MainPage"; 

    public MainPageChildsConductor ChildPages { get; private set; } 

    public IMainPageChild ActiveChildPage 
    { 
     get { return ChildPages.ActiveItem; } 
    } 

    public MainPageViewModel() 
    { 
     PageName = PAGE_NAME; 
     DisplayName = PAGE_NAME; 

     DisposeOnDeactivate = true; 

     InitChildPages(); 
    } 

    private void InitChildPages() 
    { 
     ChildPages = new MainPageChildsConductor(); 
     ChildPages.Parent = this; 
     ChildPages.ConductWith(this); 

     var trallchilds = TypeRegistry.GetItemsByType<IMainPageChild>(); 
     var trchilds = trallchilds.Where(p => p.AutoRegister != null && p.AutoRegister.Name.StartsWith(PAGE_NAME + ":")).ToList(); 

     var childs = new List<IMainPageChild>(); 
     foreach (var trchild in trchilds) 
     { 
      var child = trchild.CreateType<IMainPageChild>(); 
      childs.Add(child); 
     } 

     childs.Sort((a, b) => a.PageIndex.CompareTo(b.PageIndex)); 
     ChildPages.Items.AddRange(childs); 

     ChildPages.ActivateWith(this); 
     ChildPages.DeactivateWith(this); 
    } 
} 

이, 뷰 모델 내 자식 클래스 중 하나입니다

[AutofacRegisterType(PAGE_NAME, typeof(IMainPageChild), IsSingleInstance = false)] 
public class Child1PageViewModel : MainPageChildViewModelBase 
{ 
    public const string PAGE_NAME = "ChildPage:Child1Page"; 
    public const int PAGE_INDEX = 30; 

    public Child1PageViewModel() 
    { 
     PageName = PAGE_NAME; 
     DisplayName = "Child1"; 
     PageIndex = PAGE_INDEX; 

     InitButtons(); 
     InitSummaryData(); 
    } 
} 

을 그리고 이것은 Caliburn.Micro 클래스 지휘자를 상속하는 클래스입니다 :

public class MainPageChildsConductor : Conductor<IMainPageChild>.Collection.OneActive 
{ 
    public MainPageChildsConductor() 
    { 
    } 

    public override void NotifyOfPropertyChange([CallerMemberName] string propertyName = null) 
    { 
     base.NotifyOfPropertyChange(propertyName); 

     if (Parent is INotifyPropertyChangedEx) 
      ((INotifyPropertyChangedEx)Parent).Refresh(); 
    } 
} 

질문은 : 나는 자식 페이지 'Child1PageViewMode에서 상위 페이지'MainPageViewModel '에 존재하는 메서드 또는 속성을 호출하는 방법 엘'???

+0

는 나는이 같은 부모 클래스에서 하위 클래스에 존재하는 메서드 또는 속성을 호출 할 수있는 방법을 알고 : 경우 (ActiveChildPage.PageName == "ChildPage : Child1Page") { \t (페이지입니다. ChildrenPageTabs.Child1PageViewModel) ActiveChildPage) .DoShowClickedButton(); } 하지만 다른 방법으로는 어떻게해야할지 모르겠다 - 자식 클래스의 부모 클래스에있는 메서드 나 속성을 호출하는 방법은 무엇입니까? 아무도 내가 어떻게 할 수 있을지 아무 생각이 없니? – Navuhodonosor

+0

부모 클래스는 자식 클래스에 자신을 제공합니다. – Will

+0

자녀보기 모델이 Screen에서 상속 받거나 IScreen을 구현합니까? 이는 하위 뷰 모델에서 필요하며 활성화 된 경우에만 사용할 수 있습니다. 자세한 내용은 http://stackoverflow.com/questions/16832780/caliburn-micro-ichild-parent-is-null-unless-activated-by-conductor를 참조하십시오. –

답변

0

자식 뷰는 Screen에서 상속해야하며 부모 뷰 모델에서 활성화되면 Screen에서 상속을 통해 자식 VM의 Parent 속성에 대한 참조를 가져옵니다.

자세한 내용은 설명서의이 페이지를 참조하십시오. Screens, Conductors and Composition. VM, 당신을 ActivateItem 항목 방법 MainViewModel에서 호출되면

public class MainViewModel : Conductor<IScreen>.Collection.OneActive, IHandle<CreateNewGraphEvent>, IHandle<AddMeasurementsToGraphEvent>, IHandle<DeleteNamedGraphEvent>, 
    IHandle<GraphRenamedEvent>, IHandle<AddDuplicateGraphEvent> 
{ 
    private readonly TreeListViewModel _TreeView; 
    private readonly StatusBarViewModel _StatusBar; 
    private readonly IEventAggregator _Aggregator; 
    private readonly ProgressDialogViewModel _Progress; 

    public MainViewModel(IEventAggregator aggregator, TreeListViewModel treeView, StatusBarViewModel statusBar) 
    { 
     if (aggregator == null) 
      throw new ArgumentNullException("aggregator"); 
     _Aggregator = aggregator; 
     _Aggregator.Subscribe(this); 

     if (statusBar == null) 
      throw new ArgumentNullException("statusBar"); 
     _StatusBar = statusBar; 

     if (treeView == null) 
      throw new ArgumentNullException("treeView"); 
     _TreeView = treeView; 
     this.Items.CollectionChanged += Items_CollectionChanged; 
    } 

    public void Handle(CreateNewGraphEvent message) 
    { 
     ChartViewModel document = IoC.Get<ChartViewModel>(message.SelectedGraphType.ToString()); 
     if (document == null) return; 
     document.DisplayName = message.GraphName; 
     document.CloseAction = this.CloseAction; 
     document.SelectedGraphType = message.SelectedGraphType; 
     ActivateItem(document); 
    } 
} 

public class ChartViewModel : Screen, IHandle<MeasurementRenamedEvent> 
{ 
    private readonly IEventAggregator _Aggregator; 
    private readonly ISupportServices _Services; 

    public ChartViewModel(IEventAggregator aggregator, ISupportServices services) : base(aggregator, services) 
    { 
     if (aggregator == null) 
      throw new ArgumentNullException("aggregator"); 
     _Aggregator = aggregator; 
     _Aggregator.Subscribe(this); 

     if (services == null) 
      throw new ArgumentNullException("services"); 
     _Services = services; 
    }} 

의 CihldViewModel이 MainViewModel에서 Items 컬렉션에 추가되고 아이를 활성화 :

이것은 내 프로젝트 중 하나에 그것을 할 방법입니다 그런 다음 ChildViewModel의 this.Parent 속성을 통해 MainViewModel에 액세스 할 수 있습니다.