Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Add Singleton UserControl in WPF

Add Singleton UserControl in WPF

Scheduled Pinned Locked Moved WPF
wpfcsharpquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    Hi again! I created an UserControl which is a singleton.

    public partial class DeepDisplay : UserControl
    {
    private static DeepDisplay instance;
    private static object lockObject = new object();

        private DeepDisplay()
        {
            InitializeComponent();
        }
    
        public static DeepDisplay GetInstance()
        {
            lock (lockObject)
            {
                if (instance == null)
                    instance = new DeepDisplay();
            }
            return instance;
        }
     }
    

    What I want to do is to add this UserControl in the XAML Part of the code.

    <local:DeepDisplay x:Name="deepDisplay" Width="Auto"></local:DeepDisplay>

    Due to the fact that the Constructor is private, I need to call the GetInstance Method instead. Can this be done easily and if so, can someone be so kind a show me how? As always thank you in advance, eza

    M 1 Reply Last reply
    0
    • E ezazazel

      Hi again! I created an UserControl which is a singleton.

      public partial class DeepDisplay : UserControl
      {
      private static DeepDisplay instance;
      private static object lockObject = new object();

          private DeepDisplay()
          {
              InitializeComponent();
          }
      
          public static DeepDisplay GetInstance()
          {
              lock (lockObject)
              {
                  if (instance == null)
                      instance = new DeepDisplay();
              }
              return instance;
          }
       }
      

      What I want to do is to add this UserControl in the XAML Part of the code.

      <local:DeepDisplay x:Name="deepDisplay" Width="Auto"></local:DeepDisplay>

      Due to the fact that the Constructor is private, I need to call the GetInstance Method instead. Can this be done easily and if so, can someone be so kind a show me how? As always thank you in advance, eza

      M Offline
      M Offline
      MIHAI_MTZ
      wrote on last edited by
      #2

      I'm quite sure that is impossible. Anyway, a singleton control sounds like a very bad idea. First of all why would you need such a thing? Seccondly, how would you expect it to work when you add a seccond "instance" of the control? I'm asking this because a control can only have one parent, so it can't be in to places at once. On the other side, if you want to make sure that the control is only used once, you could keep track of the number of instances created in a static variable, and throw an error when another instance has already been created. Mihai,

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups