2017-03-04 19 views
-1

我試圖做一個簡單的時鐘,它不工作,因爲罪和cos給wpf沒有負值!C#時鐘不工作原因在wpf沒有負座標

namespace Desenho 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     [DllImport("Kernel32")] 
     public static extern void AllocConsole(); 

    [DllImport("Kernel32")] 
    public static extern void FreeConsole(); 

    private Timer timer1; 
    int i = 0; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     AllocConsole(); 
     Console.Clear(); 
     InitTimer(); 

    } 

    public void InitTimer() 
    { 
     timer1 = new Timer(); 
     timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick); 
     timer1.Interval = 1; // in miliseconds 
     timer1.Start(); 
     /*for(int a = 0; a>-200; a--) 
     { 
      Console.WriteLine("Angulo: {0}; Cos: {1}; Sen: {2}; Rad: {3};", a, Math.Cos(toRad(a)), Math.Sin(toRad(a)), toRad(a)); 
     }*/ 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     /*if(i == 5) 
     { 
      i = 0; 
     } 
     mudaCores(i); 
     i++;*/ 
     float x = 100+(float)Math.Cos(toRad(i)); 
     float y = 100+(float)Math.Sin(toRad(i)); 
     desenhaLinha(x, y); 
     i--; 
     Console.WriteLine("Angulo: {0}; X: {1}; Y: {2};", i, x, y); 
    } 

    private async void desenhaLinha(float x, float y) 
    { 

      await Task.Run(() => 
      { 
        panel.Dispatcher.BeginInvoke((Action)(() => panel.linhas.Add(new Cordenadas(new Pen(new SolidColorBrush(Colors.Red), 2), new Point(100, 100), new Point(x, y))))); 
        panel.Dispatcher.BeginInvoke((Action)(() => panel.InvalidateVisual())); 

      }); 

     } 



    private async void mudaCores(int i) 
    { 
     Color[] cores = new Color[] { Colors.Red, Colors.Black, Colors.Blue, Colors.Green, Colors.Yellow, Colors.Violet }; 
     try 
     { 
      await Task.Run(() => { panel.Dispatcher.BeginInvoke((Action)(() => panel.color = cores[i])); 
       panel.Dispatcher.BeginInvoke((Action)(() => panel.InvalidateVisual())); 
      }); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    double toRad(double graus) 
    { 
     double rad; 
     rad = graus * Math.PI/180; 
     return rad; 
    } 
} 

}

我的drawLine FUNC只需要一個點作爲參數,因爲它假定中心一個爲100,100。我怎樣才能使這項工作?

+1

嗯,在每個座標上加100,這就是中心? –

+0

做什麼工作?您至少*必須向我們展示您的drawLine方法。除此之外,LineGeometry的Transform屬性中的RotateTransform可能比現在更合適。 – Clemens

+0

試圖添加100,沒有工作:/ –

回答

2

國際海事組織你應該扔掉你的所有代碼,並編寫一個MVVM應用程序與視圖和視圖模型之間的分離。

首先爲您的時鐘創建視圖模型,例如,具有時鐘的小時,分​​鍾和秒針的三個角度屬性,以及一個DispatcherTimer,用於在UI線程中更新當前時間(DateTime.Now)的這些屬性。

public class ClockViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public ClockViewModel() 
    { 
     var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; 
     timer.Tick += TimerTick; 
     timer.Start(); 
    } 

    private void TimerTick(object sender, EventArgs e) 
    { 
     var t = DateTime.Now.TimeOfDay; 
     HoursAngle = t.TotalHours * 30 % 360; // fractional hours 
     MinutesAngle = t.Minutes * 6; // full minutes 
     SecondsAngle = t.Seconds * 6; // full seconds 
    } 

    private double hoursAngle; 
    public double HoursAngle 
    { 
     get { return hoursAngle; } 
     set 
     { 
      hoursAngle = value; 
      PropertyChanged?.Invoke(this, 
       new PropertyChangedEventArgs(nameof(HoursAngle))); 
     } 
    } 

    private double minutesAngle; 
    public double MinutesAngle 
    { 
     get { return minutesAngle; } 
     set 
     { 
      minutesAngle = value; 
      PropertyChanged?.Invoke(this, 
       new PropertyChangedEventArgs(nameof(MinutesAngle))); 
     } 
    } 

    private double secondsAngle; 
    public double SecondsAngle 
    { 
     get { return secondsAngle; } 
     set 
     { 
      secondsAngle = value; 
      PropertyChanged?.Invoke(this, 
       new PropertyChangedEventArgs(nameof(SecondsAngle))); 
     } 
    } 
} 

然後創建具有三行時鐘指針,其每一個具有數據綁定Angle特性的RotateTransform的圖。下面顯示的視圖將時鐘放置在2xx網格的右下角單元格的左上角,以便將網格中的線條居中。

窗口的DataContext被設置爲視圖模型的實例。

<Window.DataContext> 
    <local:ClockViewModel/> 
</Window.DataContext> 
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <Canvas Grid.Column="1" Grid.Row="1"> 
     <Line Y2="-50" 
       Stroke="Black" StrokeThickness="9" 
       StrokeStartLineCap="Round" StrokeEndLineCap="Triangle"> 
      <Line.RenderTransform> 
       <RotateTransform Angle="{Binding HoursAngle}"/> 
      </Line.RenderTransform> 
     </Line> 
     <Line Y2="-100" 
       Stroke="Gray" StrokeThickness="7" 
       StrokeStartLineCap="Round" StrokeEndLineCap="Triangle"> 
      <Line.RenderTransform> 
       <RotateTransform Angle="{Binding MinutesAngle}"/> 
      </Line.RenderTransform> 
     </Line> 
     <Line Y2="-100" 
       Stroke="Red" StrokeThickness="3" 
       StrokeStartLineCap="Round" StrokeEndLineCap="Triangle"> 
      <Line.RenderTransform> 
       <RotateTransform Angle="{Binding SecondsAngle}"/> 
      </Line.RenderTransform> 
     </Line> 
    </Canvas> 
</Grid>