2

我有一个 WPF 应用程序。在 App.xaml.cs 文件中我有这个

public static App Instance
    {
        get
        {
            return App.Current as App;
        }
    } 

我使用的是 MVVM LightToolkit,之后我安装了 TimePunchWPFLibrary。调出 TimePunch 具有我需要从 MVVM 获得的所有东西,所以我使用 NuGet 删除了 MVVM。现在由于某种原因,我将 Instance 属性始终设为空。这是为什么?好吧,我已经调试过了,从现在开始,App.Current属性不是来自App类型,而是XamlGeneratedNamespace.GeneratedApplication。谁能告诉我为什么?

PS 我不确定移除 MVVM Light Toolkit 是否做到了这一点,但它同时发生了。

更新:我已经想通了!问题是 App.xaml 中缺少我的 x:Class 定义。这导致 App.xaml.cs 的任何启动代码都没有被执行,并且正在生成的应用程序有点像一个虚拟应用程序。

4

1 回答 1

0

Would this work for you? I use it in my WPF Applications.

    Application.Current

From Microsoft:
Application is a per-AppDomain singleton type that implements the static Current property to provide shared access to the Application instance for the current AppDomain. This design guarantees that state managed by Application, including shared resources and state, is available from a single, shared location. This property is thread safe and is available from any thread.

于 2013-08-31T14:11:47.730 回答