locked
How should I make a timer for a VB Windows Phone 8.1 app?

    Question

  • Hi everybody,

    I'm currently trying to make a "timer" within a Windows Phone 8.1 app (using VB, not C#) that runs a line of code a minute from when it is started. I also want to make it so it will display how much time is remaining.

    Anybody have any suggestions?

    Friday, October 31, 2014 3:37 AM

Answers

  • Windows Phone 8.1(Windows Runtime)

    XAML

    <Page
        x:Class="VBAPP1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:VBAPP1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
        <Grid>
            <TextBlock x:Name="textblock"/>
        </Grid>
    </Page>

    VB code
    Public NotInheritable Class MainPage
        Inherits Page
    
        Dim timer As DispatcherTimer
        Sub New()
            InitializeComponent()
            textblock.Text = DateTime.Now.ToString()
            timer = New DispatcherTimer()
            timer.Interval = New TimeSpan(0, 1, 0)
            AddHandler timer.Tick, AddressOf timer_Tick
            timer.Start()
        End Sub
        Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
    
    
        End Sub
    
        Private Sub timer_Tick(sender As Object, e As Object)
            textblock.Text = DateTime.Now.ToString()
        End Sub
    
    
    End Class
    


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Friday, October 31, 2014 4:43 PM

All replies

  • Hi this is code in C# which tick every minute 
    DispatcherTimer dispatcherTimer = new DispatcherTimer();
    
     public void increase()
            {
                dispatcherTimer = new DispatcherTimer();
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Interval = new TimeSpan(0,1, 0);
                dispatcherTimer.Start();
    
    
            }
    
    private async void dispatcherTimer_Tick(object sender, EventArgs e)
            {
    
    
    
    //write tour code here for what you want to do after every minute 
    }


    shah

    Friday, October 31, 2014 5:14 AM
  • Thanks for the example, but I'm trying to make it using Visual Basic, not C#.
    Friday, October 31, 2014 2:19 PM
  • Windows Phone 8.1(Windows Runtime)

    XAML

    <Page
        x:Class="VBAPP1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:VBAPP1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
        <Grid>
            <TextBlock x:Name="textblock"/>
        </Grid>
    </Page>

    VB code
    Public NotInheritable Class MainPage
        Inherits Page
    
        Dim timer As DispatcherTimer
        Sub New()
            InitializeComponent()
            textblock.Text = DateTime.Now.ToString()
            timer = New DispatcherTimer()
            timer.Interval = New TimeSpan(0, 1, 0)
            AddHandler timer.Tick, AddressOf timer_Tick
            timer.Start()
        End Sub
        Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
    
    
        End Sub
    
        Private Sub timer_Tick(sender As Object, e As Object)
            textblock.Text = DateTime.Now.ToString()
        End Sub
    
    
    End Class
    


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Friday, October 31, 2014 4:43 PM
  • Thank you so much!
    Friday, October 31, 2014 4:47 PM
  • Hi Clayton,

    Did you still have questions about this problem? If yes, please post more information so that we can help you. If not, I will mark the answer.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, November 07, 2014 8:06 AM
    Moderator