结合Singleton类观察的集合会员 [英] Binding To Singleton Class Observable Collection Member

查看:136
本文介绍了结合Singleton类观察的集合会员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不能似乎摸不着头脑。我发现了一些类似的问题在这里,但不是我不能为我的方法找出正确的方向还是我做的事情完全地错了。

I just can't seem to figure this out. I found some similar Questions here but either I can't figure out the right direction for my approach or I am doing something completly wrong.

我的应用程序有一个Singleton类Logger,也从每类在我的程序将保存日志消息。

My Application has a Singleton Class Logger, which saves Log messages from every class in my program.

public class Logger
{
    private Logger()
    {

    }

    private static volatile Logger instance;

    public static Logger GetInstance()
    {
        // DoubleLock
        if (instance == null)
        {
            lock (m_lock)
            {
                if (instance == null)
                {
                    instance = new Logger();
                }
            }
        }
        return instance;
    }

    //Helper for Thread Safety
    private static object m_lock = new object();

    private ObservableCollection<string> _Log;

    public ObservableCollection<string> Log
    {
        get { return _Log; }
    }

    public void Add(string text)
    {
        if (_Log == null)
            _Log = new ObservableCollection<string>();

        Log.Add(DateTime.Now.ToString() + " " + text);
    }

    public void Clear()
    {
        _Log.Clear();
    }

}

现在我要绑定登录到列表框在我的主窗口,但我无法找出正确的绑定

Now I want to bind Log to ListBox in my MainWindow, but I can't figure out the right Binding

<ListBox Name="lstboxLog" Grid.Row="2" Margin="10,0,10,10" ItemsSource="{Binding Source={x:Static tools:Logger.Log}}" Height="100" />

工具是在我的XAML的单例类的命名空间。我敢肯定,这比我想象的更简单,但我只是忽视的东西。

tools is the namespace of the singleton class in my XAML. I'm sure this is simpler than I think, but I am just overlooking something.

推荐答案

请您的GetInstance()方法将获取属性。
而且要对确保侧实例日志观察的集合访问它。如果你打电话给你的第一个添加()方法上之前就必然这样的结合也不会被覆盖。

Make your GetInstance() method to a get property. And to be on the sure side instantiate your log Observable Collection before you access it. That way the binding won't be overriden if it is bound before you call your first Add() method on it.

XAML:

ItemsSource="{Binding Source={x:Static tools:Logger.Instance}, Path=Log}"

日志:

public static Logger Instance
    {
      get
      {
      // DoubleLock
      if (instance == null)
      {
        lock (m_lock)
        {
          if (instance == null)
          {
            instance = new Logger();
          }
        }
      }
      return instance;
      }
    }

    //Helper for Thread Safety
    private static object m_lock = new object();

    private ObservableCollection<string> _Log;

    public ObservableCollection<string> Log
    {
      get
      {
        if (_Log == null)
        { 
          _Log = new ObservableCollection<string>();
        }
        return _Log;
      }
    }

    public void Add(string text)
    {
      Log.Add(DateTime.Now.ToString() + " " + text);
    }

这篇关于结合Singleton类观察的集合会员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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