Silverlight,通过更改img源代码c#更新gui



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

它是这样工作的。

每次调用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));
        }
    }
}

这里有几个问题。

首先你没有正确使用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));
}

最新更新