1

这是我的主要父类 ViewModel:

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

这是我的子类之一,ViewModel:

[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 类 Conductor 的类:

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

问题是:如何从子页面'Child1PageViewModel'调用父页面'MainPageViewModel'中存在的方法或属性???

4

1 回答 1

0

您的子视图需要从 Screen 继承,当在父视图模型中激活时,您可以通过从 Screen 继承获得对子 VM 的 Parent 属性的引用。

有关更多详细信息,请参阅文档中的此页面: 屏幕、导体和组成

这就是我在我的一个项目中的做法:

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

在 MainViewModel 中调用 ActivateItem 项方法时,会将 CihldViewModel 添加到 MainViewModel 中的 Items 集合中并激活子 VM,然后您可以通过 ChildViewModel 中的 this.Parent 属性访问 MainViewModel。

于 2017-02-17T17:13:07.727 回答