2013-03-09 1 views
0

바이너리 시계를 만들려고하는데 제대로 작동합니다.Silverlight, img 소스를 변경하여 gui 업데이트 C#

다음과 같이 작동합니다.

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

당신이 지금까지 작업 한 내용의 샘플을 게시 할 수 루프의하지 않는 사용하는 것입니다 작업? 이 '와 같은 당신이 원한다면 내 projct를 압축하여 업로드 할 수 있습니다 @SilverSolver –

+0

, 다른 의 일 동안 (진짜야) { img.Source = 새로운 BitmapImage (statemant가 true) 경우 (새 열린 우리당 (" img/knapSluk.png ", UriKind.Relative)); else imgHour1.Source = 새 BitmapImage (새 Uri ("img/knapTaand.png", UriKind.Relative)); } ' – DaCh

+0

btw, stackoverflow에 처음으로 아무것도 게시하지 않으므로 코드의 일부를 지나치는 것과 같은 모든 일을 실제로하지는 않습니다. – DaCh

답변

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)); 
} 
+0

시도해 볼 때 img가 나타나지 않았습니다. 새 코드를 업로드하고 싶습니다. , 내가 ansawer에서 그것을하거나 메인 포스트를 편집 할 수 있습니까? – DaCh

+0

기다려라, 나는 그것이 일하고 있다고 생각한다. , 예, 작동합니다. Thx는 많은 동료입니다! – DaCh