• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#属性和字段

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

不将理论,直接看不同的代码产生什么样的影响

下面代码中有字段和其对应的属性:

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#中OADate和DateTime相互转换发布时间:2022-07-18
下一篇:
C#SocketTCPServer&amp;Client&amp;nodejsclient发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap