Xaml 如何在TopAppBar WinRT的文本块中显示当前日期时间

Xaml 如何在TopAppBar WinRT的文本块中显示当前日期时间,xaml,windows-runtime,winrt-xaml,Xaml,Windows Runtime,Winrt Xaml,我正在尝试在顶部应用程序栏中显示当前的sysdatetime,我想知道我是否可以在XAML中为win store应用程序显示当前的sysdatetime。在您的页面中,您可以设置page.TopAppBar和page.BottomAppBar,如下所示: <Page.TopAppBar> <AppBar> <TextBlock Text="Your text" /> </AppBar> </Page.TopAp

我正在尝试在顶部应用程序栏中显示当前的sysdatetime,我想知道我是否可以在XAML中为win store应用程序显示当前的sysdatetime。

在您的页面中,您可以设置page.TopAppBar和page.BottomAppBar,如下所示:

<Page.TopAppBar>
    <AppBar>
        <TextBlock Text="Your text" />
    </AppBar>
</Page.TopAppBar>


从这里,您可以绑定Text属性(如果您使用的是MVVM模式),或者通过给TextBlock元素命名,在页面的代码隐藏中分配一个值。

在页面中,您可以设置page.TopAppBar和page.BottomAppBar,如下所示:

<Page.TopAppBar>
    <AppBar>
        <TextBlock Text="Your text" />
    </AppBar>
</Page.TopAppBar>
public class MainViewModel : INotifyPropertyChanged
    {
        private readonly DispatcherTimer _timer = new DispatcherTimer();

    private string _resDateTime;
    public string ResDateTime
    {
        get
        {
            return _resDateTime;
        }
        set
        {
            _resDateTime = value;
            NotifyPropertyChanged("ResDateTime");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }

    public MainViewModel()
    {
        _timer.Tick += TimerOnTick;
        _timer.Start();
    }

    private void TimerOnTick(object sender, object o)
    {
        ResDateTime = DateTime.Now.ToString();
    }
}

从这里,您可以绑定Text属性(如果您使用的是MVVM模式),或者通过给TextBlock元素指定一个名称,在页面的代码后面指定一个值

public class MainViewModel : INotifyPropertyChanged
    {
        private readonly DispatcherTimer _timer = new DispatcherTimer();

    private string _resDateTime;
    public string ResDateTime
    {
        get
        {
            return _resDateTime;
        }
        set
        {
            _resDateTime = value;
            NotifyPropertyChanged("ResDateTime");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }

    public MainViewModel()
    {
        _timer.Tick += TimerOnTick;
        _timer.Start();
    }

    private void TimerOnTick(object sender, object o)
    {
        ResDateTime = DateTime.Now.ToString();
    }
}
添加到代码后面

public MainPage()
        {
            this.InitializeComponent();
            DataContext = new MainViewModel();
        }
然后穿上xaml

<Page.TopAppBar>
    <AppBar>
      <TextBlock Text="{Binding ResDateTime}"></TextBlock>
    </AppBar>
  </Page.TopAppBar>

希望能有所帮助

添加到代码后面

public MainPage()
        {
            this.InitializeComponent();
            DataContext = new MainViewModel();
        }
然后穿上xaml

<Page.TopAppBar>
    <AppBar>
      <TextBlock Text="{Binding ResDateTime}"></TextBlock>
    </AppBar>
  </Page.TopAppBar>

希望能有所帮助