0

我有一个 WPF 应用程序,我正在尝试有效地切换窗口的内容。我想出了以下解决方案:

应用程序.cs

internal static Lazy<HomeUserControl> HomePage;

主窗口.cs

public MainWindow()
   {
       InitializeComponent();

       Application.Current.MainWindow.Content = App.HomePage;

   }

家庭用户控制.cs

public HomeUserControl()
   {
       InitializeComponent();
   }

我遇到了一个MainWindow.Content基本上被设置为空白窗口的问题(它实际上是在改变 的内容MainWindow)。如果我使用App.MainWindow.Content = new HomePageUserControl(),一切正常。但是,我想保留一个页面实例,这就是我在 App 类中创建一个静态页面的原因。无论是否使用都会出现此问题Lazy<>。我试过检查是否HomePagenull,我得到了一个标签,上面写着Value is not created.,我很确定这是一个未初始化的表示Lazy<>;但是,这只发生在我检查App.HomePage == null. 有任何想法吗?

4

1 回答 1

1

尝试

Application.Current.MainWindow.Content = App.HomePage.Value;
于 2013-10-22T21:28:17.483 回答