使用ThreadPoolTimer C#uwp的时钟程序

rur2641

这是基于对上一个问题的答案的模型的。如何在Windows Core IoT应用程序中显示带有当前时间的时钟?,我得到一个例外:“该应用程序调用了一个已编组用于另一个线程的接口。” 如果我通过click事件更改属性,则绑定有效。

public class RunClock: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    ThreadPoolTimer _clockTimer = null;
    private string clock="";
    public string Clock
    {
        set
        {
            clock= value;
            this.OnPropertyChanged();
        }
        get { return clock; } }
    public RunClock()
    {

        _clockTimer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromMilliseconds(1000));            
    private void _clockTimer_Tick(ThreadPoolTimer timer)
    {
        DateTime datetime = DateTime.Now;
        Str1 = datetime.ToString();
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

和Xaml:

<TextBlock Margin="15" Name="timec" Text="{x:Bind Path=RunClock.Clock, Mode=OneWay}"></TextBlock>

先感谢您!

更新,工作代码:

 public class test : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    ThreadPoolTimer _clockTimer = null;
    private string str1 = "";
    public string Str1
    {
        set
        {
            str1 = value;
            this.OnPropertyChanged();
        }
        get { return str1; }
    }
    public test()
    {

        _clockTimer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromMilliseconds(1000));
                }
    async private void _clockTimer_Tick(ThreadPoolTimer timer)
    {
       await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
        {
            DateTime dt = DateTime.Now;
            Str1 = dt.ToString();
        });
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Xaml(test1是他的viewmodel的一个实例):

                <TextBlock Margin="15" Name="timec" Text="{x:Bind test1.Str1, Mode=OneWay}"></TextBlock>
阿卡那

异常是因为您尝试在其他线程中修改UI

用这个

 private void _clockTimer_Tick(ThreadPoolTimer timer)
    {
       var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
       await dispatcher.RunAsync(
        CoreDispatcherPriority.Normal, () =>
        {
        // Your UI update code goes here!
        DateTime datetime = DateTime.Now;
        Str1 = datetime.ToString();
        });
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在C#UWP应用中存储传感器数据的最佳和最快方法

来自分类Dev

C#UWP SpeechRecognizer问题

来自分类Dev

在C#UWP中从RIOT Api反序列化Json的问题

来自分类Dev

C#UWP隐藏软导航栏

来自分类Dev

C#uwp应用程序防止空闲模式

来自分类Dev

C#UWP保存不带对话框的StorageFile

来自分类Dev

使用System.IO.Directory的C#UWP返回空数组?

来自分类Dev

如何使Webview作为BackgroundTask C#UWP Windows 10运行

来自分类Dev

如何在C#UWP应用程序中使用ASP.NET Core WebAPI?

来自分类Dev

C#UWP使用Microsoft Edge打开Web网址

来自分类Dev

C#UWP自动滚动文本

来自分类Dev

使时钟UWP(C#)

来自分类Dev

通过代码C#UWP旋转位图

来自分类Dev

C#UWP数组测量值的平均值

来自分类Dev

C#UWP如何将应用程序后面的背景设置为黑色

来自分类Dev

如何从C ++ UWP App引用C#UWP类库

来自分类Dev

当应用程序处于最小化状态时如何从边缘浏览器共享到UWP应用程序C#uwp

来自分类Dev

C#UWP为什么我不能添加第二个事件处理程序?

来自分类Dev

如何在Xfce中使用Gnome时钟小程序(或等效程序)?

来自分类Dev

在C#UWP中打印ScrollViewer的内容

来自分类Dev

C#UWP如何获取更改后的ComboBoxItem的值

来自分类Dev

如何在C#UWP应用程序中播放rtmp

来自分类Dev

C#UWP自动滚动文本

来自分类Dev

数据不显示(C#UWP)

来自分类Dev

从列表中获取价值(C#UWP)

来自分类Dev

imageControl.Source(C#UWP)

来自分类Dev

c#uwp模板绑定和依赖属性

来自分类Dev

在 UWP C# 中使用程序集重定向

来自分类Dev

比较c中的表并使用时钟问题