简体   繁体   English

使用MEFedMVVM获取ViewModel数据

[英]Get ViewModel data using MEFedMVVM

I am currently using the MEFedMVVM framework to get access to ViewModels and want to know how to get data from another ViewModel currently being used. 我目前正在使用MEFedMVVM框架来访问ViewModel,并想知道如何从当前使用的另一个ViewModel获取数据。 This is coupled with the use of Cinch. 结合使用Cinch。

At present my tab control is defined as below: 目前,我的标签控件定义如下:

<Window.Resources>


    <DataTemplate DataType="{x:Type CinchV2:WorkspaceData}">
        <AdornerDecorator>
            <Border HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch" 
                CinchV2:NavProps.ViewCreator="{Binding}"/>
        </AdornerDecorator>
    </DataTemplate>


</Window.Resources>

And my Main Window ViewModel is setup in the following way once the View has been loaded: 加载视图后,将以以下方式设置我的主窗口ViewModel:

private void ViewAwareStatusService_ViewLoaded()
    {

        if (Designer.IsInDesignMode)
            return;

        //String imagePath = ConfigurationManager.AppSettings["YourImagePath"].ToString();

        WorkspaceData loginWorkSpace = new WorkspaceData(null, "LoginUserControl", null, "Login", true);
        WorkspaceData aboutWorkspace = new WorkspaceData(null, "About", null, "About", true);
        WorkspaceData viewAlbumsWorkspace = new WorkspaceData(null, "ViewAlbums", null, "View Albums", true);
        WorkspaceData readReviewSelectWorkspace = new WorkspaceData(null, "ReadReviewsSelect", null, "Select Review", true);
        WorkspaceData adminWorkspace = new WorkspaceData(null, "Admin", null, "Admin", true);

        Views.Add(aboutWorkspace);
        Views.Add(loginWorkSpace);
        Views.Add(readReviewSelectWorkspace);
        Views.Add(viewAlbumsWorkspace);


        SetActiveWorkspace(aboutWorkspace);

        UserName = new DataWrapper<string>(this, UserNameChangeArgs);
        UserName.IsEditable = true;
        //UserName.DataValue = ConfigurationManager.AppSettings["UserName"];

        UserRole = new DataWrapper<string>(this, UserNameChangeArgs);
        UserRole.IsEditable = true;
        //UserRole.DataValue = ConfigurationManager.AppSettings["UserType"];
    }

The MainWindow ViewModel inherits the ViewModelBase Class which is configured as below (Cinch Class): MainWindow ViewModel继承了ViewModelBase类,该类的配置如下(Cinch类):

namespace Cinch
{
public abstract class ViewModelBase : INotifyPropertyChanged, ICinchDisposable,  IParentablePropertyExposer
{
    public ViewModelBase();

    public SimpleCommand<object, object> CloseActivePopUpCommand { get; }
    public SimpleCommand<object, object> CloseWorkSpaceCommand { get; }
    public string DisplayName { get; set; }
    public bool IsCloseable { get; set; }
    protected virtual bool ThrowOnInvalidPropertyName { get; }
    public ObservableCollection<WorkspaceData> Views { get; set; }

    public event EventHandler<EventArgs> ActivateRequest;
    public event EventHandler<CloseRequestEventArgs> CloseRequest;
    public event EventHandler<EventArgs> CloseWorkSpace;
    public event Action<string> FocusRequested;
    public event PropertyChangedEventHandler PropertyChanged;

    public void Dispose();
    public Delegate[] GetINPCSubscribers();
    protected void NotifyPropertyChanged(PropertyChangedEventArgs args);
    protected void NotifyPropertyChanged(string propertyName);
    protected virtual void OnDispose();
    public virtual void RaiseActivateRequest();
    public virtual void RaiseCloseRequest(bool? dialogResult);
    public void RaiseFocusEvent(string focusProperty);
    public void SetActiveWorkspace(WorkspaceData viewnav);
    [DebuggerStepThrough]
    [Conditional("DEBUG")]
    public void VerifyPropertyName(string propertyName);
}

Now what I want to be able to do is, from a button click within the loginWorkspace which is a UserControl with a ViewModel, is add the Admin tab to the MainWindow's Views ObservableCollection from the LoginViewModel. 现在,我想要做的是,从loginWorkspace(这是一个带有ViewModel的UserControl)中单击按钮,将Admin选项卡从LoginViewModel添加到MainWindow的Views ObservableCollection中。

Read up about using the Mediator. 阅读有关使用调解器的信息。 You can send a message from your loginviewmodel. 您可以从您的loginviewmodel发送消息。 Then have the mainwindowview model can listen for the appropriate message. 然后让mainwindowview模型可以侦听相应的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM