Freigeben über


Window.IsVisible Property

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

Gets a value that indicates whether the window is currently visible in the user interface.

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

Syntax

'Declaration
Public ReadOnly Property IsVisible As Boolean
public bool IsVisible { get; }

Property Value

Type: System.Boolean
A value that indicates whether the window is currently visible.

Exceptions

Exception Condition
NotSupportedException

The application is not running outside the browser.

Examples

By default, you cannot reuse a window after it is closed. To display and hide a window multiple times, you must set its Visibility property and cancel its Closing event, as shown in the following code example. This example requires a UserControl subclass named DemoUserControl. Additionally, this example will work only in a trusted, out-of-browser application targeting Silverlight 5.

Private Sub ShowHideDemoWindowButton_Click(
    sender As System.Object, e As System.Windows.RoutedEventArgs)

    If (Not Application.Current.IsRunningOutOfBrowser OrElse _ 
        Not Application.Current.HasElevatedPermissions) Then Return

    DemoWindow.Visibility = If(DemoWindow.IsVisible,
        Visibility.Collapsed, Visibility.Visible)

End Sub

Private _demoWindow As Window
Private ReadOnly Property DemoWindow As Window
    Get
        If (Not Application.Current.IsRunningOutOfBrowser OrElse
            Not Application.Current.HasElevatedPermissions) Then Return Nothing

        If _demoWindow Is Nothing Then
            _demoWindow = New Window With
            {
                .Title = "Demo Window",
                .Content = New DemoUserControl(),
                .Height = 300, .Width = 300, .Top = 0, .Left = 0
            }

            AddHandler _demoWindow.Closing,
                Sub(sender As Object,
                    e As System.ComponentModel.ClosingEventArgs)
                    If (e.IsCancelable) Then
                        e.Cancel = True
                        _demoWindow.Visibility = Visibility.Collapsed
                    End If
                End Sub
        End If

        Return _demoWindow
    End Get
End Property
private void ShowHideDemoWindowButton_Click(object sender, RoutedEventArgs e)
{
    if (!Application.Current.IsRunningOutOfBrowser || 
        !Application.Current.HasElevatedPermissions) return;
    DemoWindow.Visibility = DemoWindow.IsVisible ?
        Visibility.Collapsed : Visibility.Visible;
}

private Window _demoWindow;
private Window DemoWindow
{
    get
    {
        if (!Application.Current.IsRunningOutOfBrowser || 
            !Application.Current.HasElevatedPermissions) return null;

        if (_demoWindow == null)
        {
            _demoWindow = new Window()
            {
                Title = "Demo Window",
                Content = new DemoUserControl(),
                Height = 300, Width = 300, Top = 0, Left = 0
            };

            _demoWindow.Closing += (sender, e) =>
            {
                if (e.IsCancelable)
                {
                    e.Cancel = true;
                    _demoWindow.Visibility = Visibility.Collapsed;
                }
            };
        }

        return _demoWindow;
    }
}

Version Information

Silverlight

Supported in: 5

Platforms

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