Changes in my userControl are erased when I rebuil

2019-07-24 07:30发布

问题:

This is my navigationItem.cs user control:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Uboldi.Helpers;

namespace Uboldi
{
    public partial class NavigationItem : UserControl
    {
        public bool IsSelected { get; set; }
        private string _linkText = String.Empty;
        [Browsable(true)]
        public string LinkText
        {
            get { return this._linkText; }
            set
            {
                this._linkText = value;
                RefreshDisplay();
            }
        }

        public NavigationItem()
        {
            InitializeComponent();
            RefreshDisplay();
        }

        private void RefreshDisplay()
        {
            if (IsSelected)
                this.BackColor = CustomizationHelper.GetSecondaryColor();
            else
                this.BackColor = CustomizationHelper.GetPrimaryColor();

            lblText.Text = Text;
        }
    }
}

My intention is to use this in another usercontrol called NavigationBar.

While I CAN see the LinkText attribute of the NavigationItem.cs class, when I change it from the properties pane, a warning pops up:

Warning 1 You must rebuild your project for the changes to Uboldi.LeftNavigationbar to show up in any open designers.

Fair enough, I rebuild, and then the changes I just typed in are gone!

Any ideas why?

Thank you for your time.

回答1:

Perhaps your changes are not persisted by the designer.

Did you try using DesignerSerializationVisibilityAttribute?

http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.aspx



回答2:

This is old but doesn't have an answer. Here is what I found that works. Hopefully this helps someone else.

[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Description("Test text displayed in the link"), Category("Data")]
public string LinkText
{
     get { return this._linkText; }
     set
     {
         this._linkText = value;
         RefreshDisplay();
     }
}