如何使标签静态化

How to make a label static(如何使标签静态化)
本文介绍了如何使标签静态化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个程序告诉用户两个骨架是否匹配,但问题是我需要通过 class 访问 label.我不断收到的错误是

So I have a program in which I am telling the user whether two skeletons match, but the thing is that I need to access the label via a class. The error I keep getting is

Error1  An object reference is required for the
non-static field, method, or property 
'WpfApplication1.MainWindow.matchLabel'

这是我的代码中的内容:

标签

Here's what I have in my code:

Label

static Label matching
    {
        get { return matchLabel; } //errors here
        set { matchLabel = value; } //and here
    }

private class Scan
    {
        private void Start()
        {
            Skeleton skeleton = new Skeleton();

            if (PersonDetected == true)
            {
                int SkeletonID2 = skeleton.TrackingId;

                if (SkeletonID1 == SkeletonID2)
                {
                    matching.Content = "Your IDs are Matching!";
                }

                else if (SkeletonID2 != SkeletonID1)
                {
                    matching.Content = "Your IDs don't Match.";
                }
            }
        }

        private void Stop()
        {
            if (PersonDetected == true)
            {
                matching.Content = "Scan Aborted";
            }
        }
    }

基本上我想知道如何在 wpf static 中制作标签,或者是否有其他方法可以做到这一点.
提前致谢

Basically I want to know how to make the label in wpf static, or if there is another way to do this.
Thanks in advance

推荐答案

我认为您可以使用另一种方法,就像@Daniel 所说,在多个线程上使用 UI 元素是一个坏主意.

I think that you could use another approach, like @Daniel said, using UI elements on multiple threads is a bad idea.

如果我的理解是正确的,你只是想通知用户你的域逻辑的结果,我会做的方式很简单,创建一个事件:

If my understanding is correct, you just want to notify to the user the result from your domain logic, the way I would do it is simple, create an event:

公共事件动作我的事件 = 委托 { };

            if (SkeletonID1 == SkeletonID2)
            {
                this.MyEvent("Your IDs are Matching!");
            }

            else if (SkeletonID2 != SkeletonID1)
            {
                this.MyEvent("Your IDs don't Match.");
            }

 if (PersonDetected == true)
            {
                this.MyEvent("Scan Aborted");
            }

在您的 WPF 视图中

In your WPF view

this.MydomainComponent.MyEvent += (x) =>{ this.matchLabel.Content = x;};

这篇关于如何使标签静态化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)