使用ThreadPoolTimer C# uwp的时钟程序 [英] Clock program employing ThreadPoolTimer C# uwp

查看:46
本文介绍了使用ThreadPoolTimer C# uwp的时钟程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是模仿上一个问题的答案 如何在 Windows Core IoT 应用程序中显示带有当前时间的时钟?,我收到一个异常:该应用程序调用了一个为不同线程编组的接口."如果我通过单击事件更改属性,则绑定有效.

this is modelled on a answer to a previous question How to display a clock with the current time in a Windows Core IoT app?, I get an exception: "The application called an interface that was marshalled for a different thread." If I change the property with an click event, the binding works.

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 是他的视图模型的一个实例):

Xaml (test1 is an instance of he viewmodel):

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

推荐答案

异常是因为您试图在不同线程中修改 UI

Exception is because your trying to modify UI in different thread

使用这个

 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();
        });
    }

这篇关于使用ThreadPoolTimer C# uwp的时钟程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆