0

我正在尝试制作一个二进制时钟,它工作正常。

它是这样工作的。

每次调用 run 方法时,它都会设置时间,并更改一些 img 源(img 是我的灯,每次关闭或打开时都更改 img 源)。

但是我怎样才能让它一直运行呢?

如果我做一个循环,它不会在我的手机上启动,就像使用线程做任何事情一样。

更改某些属性后是否有更新 gui 的方法?

XMAL 代码

<phone:PhoneApplicationPage 
x:Class="BinSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Image Height="80" HorizontalAlignment="Left" Margin="141,187,0,0" Name="imag1" Stretch="Fill" VerticalAlignment="Top" Width="106" Source="/BinSample;component/img/knapSluk.png" />
    </Grid>
</Grid>

    public MainPage()
    {
        InitializeComponent();

        //test one
        //try to make a neverending loop
        Run();

        //testc two
        //try loop with thread an sleep funtion.
        thredRun();


    }

    public void thredRun()
    {
        new Thread(new ThreadStart(thredRun));
        while (true)
        {
            Run();
            Thread.Sleep(100);
        }
    }

    public void Run()
    {
        while (true)
        {
            int hour = DateTime.Now.Hour;
            if (hour % 10 == 0)
                imag1.Source = new BitmapImage(new Uri("img/knapTaand.png", UriKind.Relative));
            else
                imag1.Source = new BitmapImage(new Uri("img/knapSluk.png", UriKind.Relative));
        }
    }
}
4

1 回答 1

0

这里有几个问题。

首先,您没有正确使用 Thread 类,请查看本教程以开始使用

即使您确实更正了对 Thread 类的使用,您最终还是会阻塞 UI 线程,或者最终会出现跨线程异常。

在这种情况下,实现所需目标的最简单方法可能是使用DispatcherTimer

public MainPage()
{
    InitializeComponent();
    DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromHours(1) };
    timer.Tick += new EventHandler(timer_Tick);
    SetImageSource();
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    SetImageSource();
}

void SetImageSource()
{
    int hour = DateTime.Now.Hour;
    if (hour % 10 == 0)
        imag1.Source = new BitmapImage(new Uri("img/knapTaand.png", UriKind.Relative));
    else
        imag1.Source = new BitmapImage(new Uri("img/knapSluk.png", UriKind.Relative));
}
于 2013-03-10T11:10:08.430 回答