简体   繁体   English

MVVMLight具有相应视图的多个instancekeyed-ViewModels

[英]MVVMLight multiple instancekeyed-ViewModels with corresponding Views

it might be a dump subject-title, but I don't know how to summarize this problem. 它可能是转储主题标题,但是我不知道如何总结这个问题。

First of all, I'm new to MVVM and MVVMLight. 首先,我是MVVM和MVVMLight的新手。

I'm trying to create several instances of a specific viewmodel (eg GalleryViewModel) and display the corresponding data in a corresponding view, bound to a specific instance of a GalleryViewModel. 我正在尝试创建特定视图模型(例如GalleryViewModel)的多个实例,并在绑定到GalleryViewModel特定实例的相应视图中显示相应数据。 So the user has (eg) five GalleryViews open, while in the background each GalleryView must be bound to the corresponding GalleryViewModel. 因此,用户已打开(例如)五个GalleryView,而在后台,每个GalleryView必须绑定到相应的GalleryViewModel。

Therefore, I call "GetInstance" of the SimpleIoC with a GUID as InstanceKey. 因此,我使用GUID作为InstanceKey来调用SimpleIoC的“ GetInstance”。

The problem is, that that way, the view doesn't get updated, when the data in the viewmodel changes. 问题在于,当视图模型中的数据更改时,视图不会更新。 I found out, that this is only the case, when I instanciate the viewmodel with an instance key. 我发现,只有当我使用实例键实例化viewmodel时,情况才如此。

So I think, i have to link/bind that current view to that viewmodel with instancekey XY. 所以我认为,我必须使用实例键XY将当前视图链接/绑定到该视图模型。 But I don't know how to do it. 但是我不知道该怎么做。

My "architecture" is like that: The MainViewModel calls the GetInstance(GUID)-Method. 我的“架构”是这样的:MainViewModel调用GetInstance(GUID)-Method。 Within the MainView, a new UserControl gets displayed/created with the GalleryView. 在MainView中,将使用GalleryView显示/创建一个新的UserControl。 Its datacontext is bound to GalleryViewModel via XAML (but this might be the problem, because this binding can't know the GUID, so it might get bound to a wrong instance (?) and the DataBindings don't get updated. 它的数据上下文通过XAML绑定到GalleryViewModel(但这可能是问题,因为此绑定无法识别GUID,因此它可能绑定到错误的实例(?),并且DataBindings不会更新。

Here is some code: 这是一些代码:

The ViewModelLocator returns only one instance (maybe that's the problem) ViewModelLocator仅返回一个实例(也许就是问题所在)

public GalleryMainViewModel Gallery
{
    get { return ServiceLocator.Current.GetInstance<GalleryMainViewModel>(); }
}

From within the MainViewModel the "navigation" gets called (although my application lacks of a NavigationInterface (right now). 从MainViewModel内部调用“导航”(尽管我的应用程序缺少NavigationInterface(现在)。

private void DoOpenTab(string windowname, string payload = null)
        {
            DockWindowViewModel window;
            string guid;            
            switch(windowname)
            {                    
                case "Gallery":
                    guid = Guid.NewGuid().ToString();
                    window = ServiceLocator.Current.GetInstance<GalleryMainViewModel>(guid);
                    window.ViewModelInstanceKey = guid; 
                    Messenger.Default.Send(
                        new NotificationMessage(payload), 
                        (window as GalleryMainViewModel).MessageToken);
                    DockManagerViewModel.AddDocument(window);
                    break;
            }
        }

And the GalleryView (which is a UserControl that is being placed within an Avalon DockManager-Tab), looks like this GalleryView(这是一个放置在Avalon DockManager-Tab中的UserControl)看起来像这样

<src:PluginView
    DataContext="{Binding Gallery, Source={StaticResource Locator}}"
    <!-- just normal bindings. -->
</src:PluginView>

I hope, someone can help me a bit. 我希望有人可以帮助我。 I'm feeling really close to the solution, but I really don't know how to solve it completly. 我感觉真的很接近解决方案,但是我真的不知道如何彻底解决它。

Thanks in advance! 提前致谢!

If this is in your Locator 如果这在您的定位器中

   public GalleryMainViewModel Gallery
    {
        get { return ServiceLocator.Current.GetInstance<GalleryMainViewModel>(); }
    }

And this is in your View 这是你的看法

<src:PluginView
    DataContext="{Binding Gallery, Source={StaticResource Locator}}"
    <!-- just normal bindings. -->
</src:PluginView>

Then the VM created using the guid is potentially never bound to the view. 然后,使用guid创建的VM 可能永远不会绑定到视图。 It would depend on what happens when the notification message is recived by the view. 这将取决于视图接收通知消息时发生的情况。

The above code tells the view to get its DataContext directly from the locator via the Gallery property. 上面的代码告诉视图通过Gallery属性直接从定位器获取其DataContext。 Which as you stated will be the same instance for all instances of this view. 如您所述,该视图的所有实例都是相同的实例。

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

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