【问题标题】:How to make a label static如何使标签静态化
【发布时间】:2025-12-24 15:55:11
【问题描述】:

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

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

这是我的代码中的内容:

static 标签

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";
            }
        }
    }

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

【问题讨论】:

    标签: c# wpf class static label


    【解决方案1】:

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

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

    public event Action<string> MyEvent = delegate { };

                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 视图中

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

    【讨论】:

    • 嗯嗯从来没想过。会尝试
    • MydomainComponent 是什么?
    • 在这种情况下,它是您的 Scan 类,该类将公开并引发事件
    • 啊,这是一个有趣的功能
    • 我认为这是一个有用的解决方案。你能解释一下这条线上发生了什么吗? this.MydomainComponent.MyEvent += (x) => { this.matchLabel.Content = x; };右侧是某种 lambda 函数,左侧是增量和重新分配。
    【解决方案2】:

    您最好的选择是使用内置的 WPF 数据绑定。您可以使用 MVVM 模式,但这不是必需的。

    窗口类 (XAML)

    <Window x:Class="WpfApplication2.MyWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MyWindow" Height="300" Width="300">
        <Grid>
            <Label Content="{Binding Path=MyLabelValue}" />
        </Grid>
    </Window>
    

    窗口代码后面(代码)

    using System.Windows;
    using System.ComponentModel;
    
    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for MyWindow.xaml
        /// </summary>
        public partial class MyWindow : Window, INotifyPropertyChanged
        {
            public MyWindow()
            {
                InitializeComponent();
    
                DataContext = this;  // Sets context of binding to the class 
            }
    
    
            // Property for binding
            private string _mylabelvalue;
            public string MyLabelValue
            {
                get { return _mylabelvalue; }
                set 
                { 
                    _mylabelvalue = value;
                    if(PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("MyLabelValue"));
                    }
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    }
    

    通过在设置/调用窗口上的属性时使用此方法,您可以获得标签的值。当您更改属性时 - 您通过数据绑定和 INotifyPropertyChanged 接口更新 UI 中的值。我在我的博客上有一个关于通过反射和使用 MVVM 模式的部分。

    http://tsells.wordpress.com/category/mvvm/

    【讨论】:

    • 嗯,会试试的。感谢您的快速响应,我也不必重做所有事情;)
    【解决方案3】:

    这是个坏主意。您不应在多个线程上创建 UI 元素。

    您确实应该考虑实施 MVVM 模式。它将使您的代码更加解耦并增加可测试性。

    【讨论】:

    • 你能提供一些链接和/或参考
    • 是的,但是有没有办法让标签变成静态的
    • @Daniel - 我认为这有助于为那些可能不了解 WPF 的人提供更多信息。放弃“使用 MVVM”作为任何 WPF 问题的解决方案并不总是有帮助。