zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    页面表单里的图片上传ENCTYPE="multipart/form-data"
    OSGI
    httpClient使用中报错org.apache.commons.httpclient.HttpMethodBase
    DNSPod--国内最早提供免费智能DNS产品的网站,致力于为各类网站提供高质量的多线智能DNS免费解析
    spring security 一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架
    Unable to parse request org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
    swfupload 上传报 security error # 2049 (security) 安全错误问题
    如何强制指定输入的参数必须为数字
    弹性盒子模型子元素垂直和水平居中
    Git常见错误处理
  • 原文地址:https://www.cnblogs.com/mrxiaohe/p/5856901.html
Copyright © 2011-2022 走看看