【问题标题】:How to show current date time in Textblock in TopAppBar-WinRT如何在 TopAppBar-WinRT 的文本块中显示当前日期时间
【发布时间】:2013-04-22 14:23:30
【问题描述】:

我正在尝试在热门应用栏中显示当前的 sysdatetime,但我想知道无论如何我可以在 XAML 中为 win 商店应用程序执行此操作。

【问题讨论】:

    标签: xaml windows-runtime winrt-xaml


    【解决方案1】:
    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>
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      在您的页面中,您可以像这样设置 Page.TopAppBar 和 Page.BottomAppBar:

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

      从那里,您可以绑定 Text 属性(如果您使用 MVVM 模式),或者通过为 TextBlock 元素命名来简单地在页面后面的代码中分配一个值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-07
        • 1970-01-01
        相关资源
        最近更新 更多