我正在尝试制作一个二进制时钟,它工作正常。
就像这样。
每次调用一个名为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));
}
}
}
答案 0 :(得分:0)
这里有一些问题。
首先,您没有正确使用Thread类check this tutorial to get started
即使您确实更正了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));
}