C# 屬性和字段


不將理論,直接看不同的代碼產生什么樣的影響

下面代碼中有字段和其對應的屬性:

 private string title;

 public string Title ...

通過做實驗可知,當我給屬性賦值,即Title="News";此時如果單步調試,會進入到屬性的set代碼段中,而且如果我們一直關注title和Title的值,可以發現,只有當title = value;這句話成功執行之后,title和Title才會一起變成新的值。也就是說只有當字段title被成功賦值,屬性Title才會跟被賦值。如果我們執行代碼段2,由於if(true),使得title = value;沒有得到執行,我們可以看到,最終的結果是title和Title都沒有變,還是原來的老值。或者不執行代碼段2,當老值和新值相同的時候,也能忽略掉title=value;

 

代碼段1:

        private string title;

        public string Title
        {
            get { return title; }
            //set { Set(ref title, value); }
            set
            {
                if (title == value) { return; }
                title = value;
                RaisePropertyChanged(() => Title);
            }
        }    

 

代碼段2

        private string title;

        public string Title
        {
            get { return title; }
            //set { Set(ref title, value); }
            set
            {
                if (title == value) { return; }
                if (true)
                {
                    return;
                }
                title = value;
                RaisePropertyChanged(() => Title);
            }
        }

 

如果我們如果給字段(private)賦值,即title="news",此時是不會進入到屬性Title的set代碼段的。而且執行完成之后,屬性Title和字段title,會同時被賦予新值"news"。

前台綁定的是Title(因為Title是public,而title是private),所以如果要讓前台做出變化,需要在賦值語句 title="news";后面再執行 RaisePropertyChanged(() => Title);

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System.Text.RegularExpressions;
using System.Windows.Input;

namespace MVVM.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        private string title;

        public string Title
        {
            get { return title; }
            //set { Set(ref title, value); }
            set
            {
                if (title == value) { return; }
                title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        public ICommand ChangeTitleCommand { get; set; }

        public MainViewModel()
        {
            Title = "Hello World";
            ChangeTitleCommand = new RelayCommand(ChangeTitle);
        }
        private void ChangeTitle()
        {
            string res="";
            string line = "root@HZ-CAS01-CVK01:~#";
            string pattern = ".*@(.*):.*";
            Regex re = new Regex(pattern);
            MatchCollection matches = re.Matches(line);
            foreach (Match m in matches)
            {
                GroupCollection groups = m.Groups;
                res = groups[1].Value;
            }
            //在這里直接對字段賦值,不進入set代碼段,屬性和字段同時被賦予新值news
            title = "news";
            RaisePropertyChanged(() => Title);

        }
    }
}

 

 

 

 

 

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System.Text.RegularExpressions;
using System.Windows.Input;

namespace MVVM.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private string title;

        public string Title
        {
            get { return title; }
            set
            {
                if (title == value) { return; }
                title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        public ICommand ChangeTitleCommand { get; set; }
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            Title = "Hello World";
            ChangeTitleCommand = new RelayCommand(ChangeTitle);


            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"-
            ////}
        }
        private void ChangeTitle()
        {
            string res="";
            string line = "root@HZ-CAS01-CVK01:~#";
            string line1 = "<WX5504>";
            string line2 = "[fangkuohao]";
            //string pattern = "@(?:.*@(.*):~#)|(?:<(.*)>)|(?<=[)(.*)(?=>])|(?:(.*)>|#))";
            string pattern = ".*@(.*):.*";
            Regex re = new Regex(pattern);
            MatchCollection matches = re.Matches(line);
            foreach (Match m in matches)
            {
                GroupCollection groups = m.Groups;
                res = groups[1].Value;
            }
            Title = res;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM