简体   繁体   English

Caliburn.Micro在导航GoBack上重新绑定ContentControl

[英]Caliburn.Micro rebind ContentControl on navigation GoBack

I'm using Caliburn.Micro within WinRT application 我在WinRT应用程序中使用Caliburn.Micro

Here is my main VM: 这是我的主要VM:

public class MainViewModel : Conductor<Screen>
{
    protected override void OnActivate()
    {
        if (ActiveItem == null)
        {
           ActivateItem(
               ViewModelLocator.LocateForViewType(typeof(NewsFeedView)) as Screen);
        }

        base.OnActivate();
    }
}

here I use conductor because I want to load different controls in ContentControl, but now I have only this code. 这里我使用指挥因为我想在ContentControl中加载不同的控件,但现在我只有这个代码。 Here is my content control in main view: 这是我在主视图中的内容控件:

<ContentControl x:Name="ActiveItem" Grid.Column="1" Grid.Row="1" />

When I running the application everything work fine, MainViewModel.Activate gets called and ActiveItem set to NewsFeedViewModel and ContentControl loads NewsFeedView . 当我运行应用程序时,一切正常, MainViewModel.Activate被调用, ActiveItem设置为NewsFeedViewModelContentControl加载NewsFeedView

The problem: 问题:

When I navigate in NewsFeedView control to another view using NavigationService.NavigateToViewModel method and then in that view use NavigationService.GoBack , i'm returning to MainView and in that moment when MainViewModel.Activate gets called ActiveItem is not null , but ContentControl.Content is null . 当我使用NavigationService.NavigateToViewModel方法在NewsFeedView控件中NavigationService.NavigateToViewModel到另一个视图然后在该视图中使用NavigationService.GoBack ,我将返回MainView ,当MainViewModel.Activate被调用时, ActiveItem不为null ,但ContentControl.Contentnull I've tried use View.Model attached property for ContentControl but no luck, how to make it rebind? 我已经尝试过为ContentControl使用View.Model附加属性,但没有运气,如何重新绑定?

EDIT: Finally i'm setup logger in Caliburn to see what happens and I found an error - when MainView loaded after navigationg back this events occuring: 编辑:最后我在Caliburn设置记录器,看看会发生什么,我发现一个错误 - 当导航后加载MainView这个事件发生时:

Attaching ***.Views.MainView to ***.ViewModels.MainViewModel.
ViewModel bound on ActiveItem.
Using cached view for ***.ViewModels.NewsFeedViewModel.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: Unspecified error
at Windows.UI.Xaml.Controls.ContentControl.put_Content(Object value)
... some winRT stack
at Caliburn.Micro.View.SetContentPropertyCore(...

Though it was not so informative I've used InteliTrace to get more info and got this message: "Element is already child of another element". 虽然它不是那么有用,我已经使用InteliTrace获取更多信息并得到了这样的信息:“元素已经是另一个元素的孩子了”。 I suppose NewsFeedView stored somewhere and when time comes to put it in ContentControl this exception thrown. 我认为NewsFeedView存储在某个地方,当时间到了将它放入ContentControl这个异常时抛出。 How to solve this? 怎么解决这个?

You should adopt the view model first approach. 您应该采用视图模型的第一种方法。 In other words, activate an instance of a view model, and Caliburn.Micro will do the view location and binding for you. 换句话说,激活视图模型的实例,Caliburn.Micro将为您执行视图位置和绑定。

It also looks like you want to just instantiate the view model once in the constructor for example, or OnInitialise : 看起来你只想在构造函数中实例化一次视图模型,例如OnInitialise

public MainViewModel()
{
    this.ActivateItem(new NewsFeedViewModel());
}

正如@devdigital所说,可能只在构造函数中初始化新闻订阅源视图模型一次,为什么不使用Conductor.Collection.OneActive,因为在任何给定时间只有一个活动项,它用于这种情况,这可以解决你的问题问题。

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

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