尝试在 Caliburn.Micro 中创建新 ViewModel 时的 StackOverflow

StackOverflow When Trying to Create a New ViewModel in Caliburn.Micro

过去几天我一直在尝试创建一个新的视图模型并将其显示在我的应用程序的主 shellview 上。目前,我已经能够使用 ActivateItemAsync 加载页面,但这会占用整个页面。我目前希望它位于活动项上方。这是页面的视图:

  <!-- Notification Bar -->
  <UserControl 
            x:Name="NotificationMessage"
            Visibility="{Binding IsNotificationMessageVisible, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=collapsed}"
            Grid.Row="0" Grid.Column="1"

            />


        <!-- Main Frame -->
        <Frame 
               x:Name="ActiveItem"  
               NavigationUIVisibility="Hidden" 
               Padding="10"
            Grid.Row="1"
            Grid.Column="1"
               />

正如您从上面看到的那样,我有两行,其中一行是通知栏,如果用户有通知,它就会出现。我目前希望只有在通知可用时才隐藏它。但是,我无法在页面中显示通知。当我尝试时,由于循环而收到堆栈溢出通知。以下是上述视图的 ViewModel 代码:

私人_notificationtMessage;

    public NotificationViewModel NotificationMessage
    {
        get {
            _notificationtMessage= new NotificationViewModel ();
            return _notificationtMessage; 
        }
        set { 
            _notificationtMessage= value;
            NotifyOfPropertyChange(() => NotificationMessage);
            NotifyOfPropertyChange(() => IsNotificationVisible);
        }
    }



    public bool IsNotificationVisible
    {
        get
        {
            bool b = false;
            if(Database.GetNotification().Title.Length !=0)
            {
                b = true;
                this.NotificationMessage = new NotificationViewModel ();
            }
            return b;
        }
    }

我的主要问题是如何在我的 ShellViewModel 中创建此模型的新实例并将其显示在我的 ShellView 中?

您不应在 getter 中创建对象。试试这个并确保您已经根据约定为 NotificationViewModel 定义了一个视图:

private NotificationViewModel _notificationtMessage = new NotificationViewModel();
public NotificationViewModel NotificationMessage
{
    get
    {
        return _notificationtMessage;
    }
    set
    {
        _notificationtMessage = value;
        NotifyOfPropertyChange(() => NotificationMessage);
        NotifyOfPropertyChange(() => IsNotificationVisible);
    }
}


public bool IsNotificationVisible
{
    get
    {
        return Database.GetNotification().Title.Length != 0;
    }
}