Caliburn.Micro:使用Conductor在页面之间移动:如何从子类调用父类方法或属性?

Caliburn.Micro:使用Conductor在页面之间移动:如何从子类调用父类方法或属性?

问题描述:

这是我的主,父类,视图模型:Caliburn.Micro:使用Conductor在页面之间移动:如何从子类调用父类方法或属性?

[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(); 
    } 
} 

的问题是:我怎么能调用父页面“MainPageViewModel”存在从孩子页面“Child1PageViewMode方法或属性升“???

+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 –

您的子视图需要从屏幕继承,并且在父视图模型中激活时,您可以通过从屏幕继承来获取对子VM的父属性的引用。

请参阅此文档中的更多详细信息: Screens, Conductors and Composition

这是我要做的事在我的项目之一:

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; 
    }} 

当ActivateItem项方法被调用在MainViewModel的CihldViewModel被添加到项目集合在MainViewModel并激活子VM,在那里你然后可以通过ChildViewModel中的this.Parent属性访问MainViewModel。