Condividi tramite


Window.Top Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the position of the top edge of the application window; see Remarks for restrictions on setting this property at run time.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Top As Double
public double Top { get; set; }

Property Value

Type: System.Double
The position of the top edge of the application window. The default is the WindowSettings.Top setting in the application manifest.

Exceptions

Exception Condition
NotSupportedException

The application is not running outside the browser.

UnauthorizedAccessException

The current thread is not the user interface (UI) thread.

Remarks

You can set this property at run time only if WindowStartupLocation is set to Manual. Additionally, you can set this property at run time only before the end of the Application.Startup event, after which any changes are ignored. For example, you can save the current value to isolated storage in an Application.Exit event handler, and restore the value in an Application.Startup event handler.

Examples

The following code example demonstrates the use of this property. In this example, the window position and dimensions were previously saved to the ApplicationSettings in the isolated storage as a user defined key/value pair. These key/value pairs are retrieved and used to set the current value of the window settings.

Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
    Me.RootVisual = New MainPage()

    ' Set the window position and dimensions to the previoius settings.
    ' The key/value pairs in the ApplicationSettings are user defined.
    If (Application.Current.IsRunningOutOfBrowser) Then

        ' Get the application settings from IsolatedStorage.
        Dim appSettings As IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings

        ' Get a reference to the main window.
        Dim mainWindow = Application.Current.MainWindow

        ' Set the Window.Top property.
        If (appSettings.Contains("WindowTop")) Then
            mainWindow.Top = Convert.ToDouble(appSettings("WindowTop"))
        End If

        ' Set the Window.Left property.
        If (appSettings.Contains("WindowLeft")) Then
            mainWindow.Left = Convert.ToDouble(appSettings("WindowLeft"))
        End If

        ' Set the Window.Width property.
        If (appSettings.Contains("WindowWidth")) Then
            mainWindow.Width = Convert.ToDouble(appSettings("WindowWidth"))
        End If

        ' Set the Window.Height property.
        If (appSettings.Contains("WindowHeight")) Then
            mainWindow.Height = Convert.ToDouble(appSettings("WindowHeight"))
        End If
    End If
End Sub
private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new MainPage();

    // Set the window position and dimensions to the previoius settings.
    // The key/value pairs in the ApplicationSettings are user defined.
    if (Application.Current.IsRunningOutOfBrowser)
    {
        // Get the application settings from IsolatedStorage.
        IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

        // Get a reference to the main window.
        Window mainWindow = Application.Current.MainWindow;

        // Set the Window.Top property.
        if (appSettings.Contains("WindowTop"))
        {
            mainWindow.Top = Convert.ToDouble(appSettings["WindowTop"]);
        }

        // Set the Window.Left property.
        if (appSettings.Contains("WindowLeft"))
        {
            mainWindow.Left = Convert.ToDouble(appSettings["WindowLeft"]);
        }

        // Set the Window.Width property.
        if (appSettings.Contains("WindowWidth"))
        {
            mainWindow.Width = Convert.ToDouble(appSettings["WindowWidth"]);
        }

        // Set the Window.Height property.
        if (appSettings.Contains("WindowHeight"))
        {
            mainWindow.Height = Convert.ToDouble(appSettings["WindowHeight"]);
        }
    }
}

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.