how to open a window in WPF

dstylesunf

Member
Joined
Jan 4, 2010
Messages
6
Programming Experience
10+
Simple question that I cannot find an answer to. How do I open a window created in wpf like you can with a winforms program? Example, form1.show would open a winform form with no problem. here is my code:
VB.NET:
Imports System.Windows.Threading

Public Class Window1


    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Dim duration As Duration = New Duration(TimeSpan.FromSeconds(30))
        Dim doubleanimation As Animation.DoubleAnimation = New Animation.DoubleAnimation(200.0, duration)
        ProgressBar1.BeginAnimation(ProgressBar.ValueProperty, doubleanimation)
        Dim dt As DispatcherTimer = New DispatcherTimer()
        AddHandler dt.Tick, AddressOf dispatcherTimer_Tick
        dt.Interval = New TimeSpan(0, 0, 15)
        dt.Start()
    End Sub
      Public Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        'I want to open the window named MainWindow 
        Me.Close()
    End Sub

End Class

On the tick event I want to open the window Mainwindow and close the current window Window1


Thanks
 
WPF doesn't have a "default window instance" feature, you have to create the instance yourself then call the Show method. How to: Use the New Keyword
 
VB.NET:
    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Dim duration As Duration = New Duration(TimeSpan.FromSeconds(30))
        Dim doubleanimation As Animation.DoubleAnimation = New Animation.DoubleAnimation(200.0, duration)
        ProgressBar1.BeginAnimation(ProgressBar.ValueProperty, doubleanimation)
        Dim dt As DispatcherTimer = New DispatcherTimer()
        AddHandler dt.Tick, AddressOf dispatcherTimer_Tick
        dt.Interval = New TimeSpan(0, 0, 15)
        dt.Start()
    End Sub
    Public Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        'I want to open the window named MainWindow 
        Dim frm As MainWindow = New MainWindow
        frm.Show()

        Me.Close()
    End Sub

All you need to do is create a instance of your window and call the show method. If you are using pages then you will find that there isnt a show method and you have to add the page to a NavigationWindow and then use the Navigate method to display the page.

Check the XAML for either:
<Window x:Class="MainWindow"
or
<Page x:Class="MainWindow"

Hope this helps :rolleyes:
 
Back
Top