无法访问Timer_Elapsed事件中的标签内容

时间:2019-04-12 10:47:49

标签: c# wpf xaml

在我的XAML文件中,我有一个名为eorzeaTime的标签,我正试图从后面的代码中访问其内容。

XAML代码是:

<StackPanel>
    <StackPanel.Background>
        <ImageBrush ImageSource="Resources/Images/Background.png"/>
    </StackPanel.Background>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="16"/>
        </Grid.RowDefinitions>
    </Grid>

    <Label x:Name="eorzeaTime" Content="00:00 AM"
           HorizontalAlignment="Center"
           FontSize="37" FontFamily="./Resources/Fonts/#Gilroy Light"
           Margin="0 200 0 0"/>
</StackPanel>

C#代码为:

namespace Eorzea_Timers
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Timer timer = new Timer();
        timer.Interval = 2916.666666666667;
        timer.Elapsed += Timer_Elapsed;
        timer.Start();
    }

    #region Methods

    #endregion

    #region Events
    private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //https://stackoverflow.com/a/7875351/10617365
        var eorzeaTimeNow = DateTime.Now.ToEorzeaTime().ToString("hh:mm tt", CultureInfo.InvariantCulture);
        eorzeaTime.Content = eorzeaTimeNow;
    }
    #endregion

我的问题是由于Timer_Elapsed是静态的,我实际上无法访问我认为的标签内容,但是我无法更改它。我应该如何更改计时器的每个刻度的内容?

0 个答案:

没有答案