Udostępnij za pośrednictwem


CoreApplication Class

Definition

Enables apps to handle state changes, manage windows, and integrate with a variety of UI frameworks.

public ref class CoreApplication abstract sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class CoreApplication final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public static class CoreApplication
Public Class CoreApplication
Inheritance
Object Platform::Object IInspectable CoreApplication
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Remarks

The system creates this object as a singleton when it runs the app. It is run as an Application Single-Threaded Apartment (ASTA). Threads created from the app singleton, such as the view provider (seen in the sample below), should be attributed as Multi-Threaded Apartment (MTAThread).

This API is supported in Universal Windows Platform apps only, except for the Properties property, which is always available.

struct App : implements<App, IFrameworkViewSource, IFrameworkView>
{
public:
    IFrameworkView CreateView()
    {
        return *this;
    }
};

...

int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
    Windows::ApplicationModel::Core::CoreApplication::Run(App());
}
ref class MyFrameworkViewSource : IFrameworkViewSource
{
public:
    virtual IFrameworkView^ CreateView()
    {
        return ref new MyFrameworkView();
    }
};

// ...

[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
    auto frameworkViewSource = ref new MyFrameworkViewSource();
    Windows::ApplicationModel::Core::CoreApplication::Run(frameworkViewSource);
    return 0;
}
internal class ApplicationSource : IFrameworkViewSource
{
	public virtual IFrameworkView CreateView()
	{
		return new CoreApp();
	}
}

...

[MTAThread]
public static int Main()
{
	var appSource = new ApplicationSource();
	CoreApplication.Run(appSource);
	return 0;
}

Version history

Windows version SDK version Value added
1607 14393 BackgroundActivated
1607 14393 EnablePrelaunch
1607 14393 EnteredBackground
1607 14393 LeavingBackground
1709 16299 RequestRestartAsync
1709 16299 RequestRestartForUserAsync

Properties

Id

Gets the package-relative app identifier for the process.

MainView

Gets the main CoreApplicationView instance for all running apps that use this CoreApplication instance.

Properties

Gets a property set that the app and UI frameworks can use to store info while the process is running.

Views

Gets all views for the app.

Methods

CreateNewView()

Creates a new view for the app.

CreateNewView(IFrameworkViewSource)

Creates a new view for the app.

CreateNewView(String, String)

Creates a new view for the app.

DecrementApplicationUseCount()

Decrements the total count of running app instances that use this CoreApplication instance.

EnablePrelaunch(Boolean)

Enable or disable the operating system's ability to prelaunch your app.

Exit()

Shuts down the app.

GetCurrentView()

Gets the view corresponding to the current thread.

IncrementApplicationUseCount()

Increments the total count of running app instances that use this CoreApplication instance.

RequestRestartAsync(String)

Restart the app.

RequestRestartForUserAsync(User, String)

Restart the app in the context of a different user.

Run(IFrameworkViewSource)

Runs an external view provider factory to obtain a view provider.

RunWithActivationFactories(IGetActivationFactory)

Runs an activatable type factory to obtain an external activatable type.

Events

BackgroundActivated

Fired when your app is activated by a background trigger.

EnteredBackground

Fired when the app enters the running in the background state.

Exiting

Occurs when the app is shutting down.

LeavingBackground

Fired just before application UI becomes visible.

Resuming

Occurs when an app is resuming.

Suspending

Occurs when the app is suspending.

UnhandledErrorDetected

Occurs when there is an underlying error that is routed to the CoreApplication instance, as part of global error handling logic.

Applies to

See also