Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf Micro:使用Conductor在页面之间移动:如何从子类调用父类方法或属性?_Wpf_Mvvm_Autofac_Caliburn.micro_Conductor - Fatal编程技术网

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

Wpf Micro:使用Conductor在页面之间移动:如何从子类调用父类方法或属性?,wpf,mvvm,autofac,caliburn.micro,conductor,Wpf,Mvvm,Autofac,Caliburn.micro,Conductor,这是我的主要父类ViewModel: [AutofacRegisterType(页面名称,类型(IMainPage),IsSingleInstance=false)] 公共类MainPageViewModel:MainPageViewModelBase { public const string PAGE_NAME=“MainPage”; public MainPageChildsConductor ChildPages{get;private set;} 公共IMAInputChild Act

这是我的主要父类ViewModel:

[AutofacRegisterType(页面名称,类型(IMainPage),IsSingleInstance=false)]
公共类MainPageViewModel:MainPageViewModelBase
{
public const string PAGE_NAME=“MainPage”;
public MainPageChildsConductor ChildPages{get;private set;}
公共IMAInputChild ActiveChildPage
{
获取{return ChildPages.ActiveItem;}
}
公共MainPageViewModel()
{
PageName=页面名称;
显示名称=页面名称;
dispose=true;
InitChildPages();
}
私有void InitChildPages()
{
ChildPages=新的MainPageChildsConductor();
ChildPages.Parent=this;
ChildPages.ConductWith(this);
var trallchilds=TypeRegistry.GetItemsByType();
var trchilds=trallchilds.Where(p=>p.AutoRegister!=null&&p.AutoRegister.Name.StartsWith(PAGE_Name+“:”).ToList();
var childs=新列表();
foreach(trchilds中的var trchild)
{
var child=trchild.CreateType();
添加(child);
}
排序((a,b)=>a.PageIndex.CompareTo(b.PageIndex));
ChildPages.Items.AddRange(childs);
ChildPages.ActivateWith(此);
子页面。禁用(此);
}
}
这是我的子类之一,ViewModel:

[AutofacRegisterType(页面名称,类型(IMainPageChild),IsSingleInstance=false)]
公共类Child1PageViewModel:MainPageChildViewModelBase
{
public const string PAGE_NAME=“ChildPage:Child1Page”;
公共const int PAGE_INDEX=30;
public child1page视图模型()
{
PageName=页面名称;
DisplayName=“Child1”;
页面索引=页面索引;
初始化按钮();
InitSummaryData();
}
}
这是继承Caliburn.Micro类导体的类:

public class MainPageChildsConductor:Conductor.Collection.OneActive
{
public main页面childs导体()
{
}
公共重写void NotifyOfPropertyChange([CallerMemberName]字符串propertyName=null)
{
base.NotifyOfPropertyChange(propertyName);
if(父项为INotifyPropertyChangedEx)
((INotifyPropertyChangedEx)父项)。刷新();
}
}

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

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

有关更多详细信息,请参阅文档中的本页:

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

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;
    }}
public class主视图模型:Conductor.Collection.OneActive、IHandle、IHandle、IHandle、,
我处理,我处理
{
私有只读树视图模型_TreeView;
私有只读StatusBarViewModel\u StatusBar;
私有只读IEventagegrator\u聚合器;
私有只读进程DialogViewModel\u进程;
public Main视图模型(IEventAggregator聚合器、TreeListViewModel树视图、StatusBarViewModel状态栏)
{
if(聚合器==null)
抛出新的ArgumentNullException(“聚合器”);
_聚合器=聚合器;
_聚合器。订阅(此);
如果(状态栏==null)
抛出新的ArgumentNullException(“状态栏”);
_状态栏=状态栏;
if(treeView==null)
抛出新的ArgumentNullException(“treeView”);
_树视图=树视图;
this.Items.CollectionChanged+=Items\u CollectionChanged;
}
公共无效句柄(CreateNewGraphEvent消息)
{
ChartViewModel document=IoC.Get(message.SelectedGraphType.ToString());
如果(单据==null)返回;
document.DisplayName=message.GraphName;
document.CloseAction=此.CloseAction;
document.SelectedGraphType=message.SelectedGraphType;
ActivateItem(文件);
}
}
公共类图表视图模型:屏幕,IHandle
{
私有只读IEventagegrator\u聚合器;
私人只读ISupportServices(U服务);
public ChartViewModel(IEventAggregator聚合器,ISupportServices服务):基础(聚合器,服务)
{
if(聚合器==null)
抛出新的ArgumentNullException(“聚合器”);
_聚合器=聚合器;
_聚合器。订阅(此);
if(服务==null)
抛出新的异常(“服务”);
_服务=服务;
}}

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

我知道如何从父类调用存在于子类中的方法或属性,像这样:如果(ActiveChildPage.PageName==“ChildPage:Child1Page”){(Pages.ChildrenPageTabs.Child1PageViewModel)ActiveChildPage).DoShowClickedButton();}但是我不知道如何用另一种方式来做-如何从子类调用父类中存在的方法或属性?有人知道我该怎么做吗?父类将自己交给子类。您的子视图模型是从Screen继承还是实现IScreen?这是子视图模型所需要的,并且仅在激活时可用。更多信息请参见此处