clone 能够用MemberwiseClone来复制2个类
但以此复制是浅复制,创造叁个新的object然后复制值字段,对于引用就平素复制引用,不复制引用的本身,指向同样引用

假设要复制引用,能够接纳类别化和反类别化复制类
json,连串化和反体系化可以行使

类别化本来有BinaryFormatter 而后天从不了SoapFormatter

能够用的微软的XmlSerializer,须求using System.Xml.Serialization;

Nuget下载很多的json Newtonsoft.Json
要求在品种管理
json 1
安装

简短的通信录

    public class caddressBook: notify_property
    {
        public caddressBook()
        {

        }
        /// <summary>
        /// 标识符
        /// </summary>
        public string id
        {
            set
            {
                _id = value;
                OnPropertyChanged();
            }
            get
            {
                return _id;
            }
        }
        /// <summary>
        /// 通讯人姓名
        /// </summary>
        public string name
        {
            set
            {
                _name = value;
                OnPropertyChanged();
            }
            get
            {
                return _name;
            }
        }
        /// <summary>
        /// 联系方式
        /// </summary>
        public string contact
        {
            set
            {
                _contact = value;
                OnPropertyChanged();
            }
            get
            {
                return _contact;
            }
        }
        /// <summary>
        /// 工作地点
        /// </summary>
        public string address
        {
            set
            {
                _address = value;
                OnPropertyChanged();
            }
            get
            {
                return _address;
            }
        }
        /// <summary>
        /// 城市
        /// </summary>
        public string city
        {
            set
            {
                _city = value;
                OnPropertyChanged();
            }
            get
            {
                return _city;
            }
        }
        /// <summary>
        /// 备注
        /// </summary>
        public string comment
        {
            set
            {
                _comment = value;
                OnPropertyChanged();
            }
            get
            {
                return _comment;
            }
        }
        /// <summary>
        /// 输入正确
        /// </summary>
        public bool accord
        {
            set
            {
                value = false;
            }
            get
            {
                if (string.IsNullOrEmpty(name))
                {
                    return false;
                }                
                return true;
            }
        }

        private string _id;
        private string _name;
        private string _contact;
        private string _address;
        private string _city;
        private string _comment;
    }

notify_property是通知

    public class notify_property : INotifyPropertyChanged
    {
        public notify_property()
        {
            _reminder = new StringBuilder();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 一直添加value
        /// </summary>
        public string reminder
        {
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    _reminder.Clear();
                }                
                else
                {
                    _reminder.Append(value + "\r\n");
                }
                OnPropertyChanged("reminder");
            }
            get
            {
                return _reminder.ToString();
            }
        }

        public void UpdateProper<T>(ref T properValue , T newValue , [System.Runtime.CompilerServices.CallerMemberName] string properName = "")
        {
            if (object.Equals(properValue , newValue))
                return;

            properValue = newValue;
            OnPropertyChanged(properName);
        }
        public void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name="")
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this , new PropertyChangedEventArgs(name));
            }
        }


        private StringBuilder _reminder;
    }

复制利用MemberwiseClone

        public object clone()
        {
            return this.MemberwiseClone();
        }

前台三个Grid,一个显得原有的,三个显得复制的

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />                
            </Grid.RowDefinitions>
            <TextBlock Text="通讯录" HorizontalAlignment="Center"/>
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <TextBlock Text="id" Grid.Column="0" Grid.Row="0"/>
                <TextBox Text="{x:Bind view.addressBook.id,Mode=TwoWay}" Grid.Column="1" Grid.Row="0"/>
                <TextBlock Text="name" Grid.Column="0" Grid.Row="1"/>
                <TextBox Text="{x:Bind view.addressBook.name,Mode=TwoWay}" Grid.Column="1" Grid.Row="1"/>
            </Grid>
        </Grid>
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <!--<TextBlock Text="通讯录" HorizontalAlignment="Center"/>-->
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <TextBlock Text="id" Grid.Column="0" Grid.Row="0"/>
                <TextBox Text="{x:Bind view.clone.id,Mode=TwoWay}" Grid.Column="1" Grid.Row="0"/>
                <TextBlock Text="name" Grid.Column="0" Grid.Row="1"/>
                <TextBox Text="{x:Bind view.clone.name,Mode=TwoWay}" Grid.Column="1" Grid.Row="1"/>
            </Grid>
        </Grid>
        <Button Grid.Column="2" Content="确定" Click="{x:Bind view.cloneaddress}"/>
    </Grid>

viewModel

        public caddressBook addressBook
        {
            set;
            get;
        }=new caddressBook();

        public caddressBook clone
        {
            set;
            get;
        } = new caddressBook();

在ViewModel初始给addressBook id和name一个值

            addressBook.id = "id";
            addressBook.name = "name";

点击按钮复制clone值

        public void cloneaddress()
        {
            //clone = addressBook.clone() as caddressBook;
            caddressBook temp = new caddressBook();
            temp.id = "0";
            temp.name = "clone";

            clone.id = temp.id;
            clone.name = temp.name;
        }

平昔复制clone会让引用未有针对复制的值

        public void cloneaddress()
        {
            //clone = addressBook.clone() as caddressBook;
            caddressBook temp = new caddressBook();
            temp = addressBook.clone() as caddressBook;

            clone.id = temp.id;
            clone.name = temp.name;
        }

能够复制

最笨的

        public object Clone()
        {
            caddressBook addressbook = new caddressBook();
            addressbook.id = id;
            addressbook.name = name;
            addressbook.contact = contact;
            addressbook.address = address;
            addressbook.city = city;
            addressbook.comment = comment;
            return addressbook;
        }

        public void cloneaddress()
        {
            //clone = addressBook.clone() as caddressBook;
            caddressBook temp = new caddressBook();
            //temp = addressBook.clone() as caddressBook;
            temp = addressBook.Clone() as caddressBook;

            clone.id = temp.id;
            clone.name = temp.name;
        }

能够复制

序列化XmlSerializer

        public object clonejson()
        {
            string str = JsonConvert.SerializeObject(this);
            return JsonConvert.DeserializeObject<caddressBook>(str);
        }

        public void cloneaddress()
        {
            //clone = addressBook.clone() as caddressBook;
            caddressBook temp = new caddressBook();
            //temp = addressBook.clone() as caddressBook;
            //temp = addressBook.Clone() as caddressBook;

            temp = addressBook.clonejson() as caddressBook;

            clone.id = temp.id;
            clone.name = temp.name;
        }

<script type=”text/javascript”> $(function () { $(‘pre.prettyprint
code’).each(function () { var lines =
$(this).text().split(‘\n’).length; var $numbering =
$(‘<ul/>’).addClass(‘pre-numbering’).hide();
$(this).addClass(‘has-numbering’).parent().append($numbering); for (i =
1; i <= lines; i++) { $numbering.append($(‘<li/>’).text(i)); };
$numbering.fadeIn(1700); }); }); </script>

相关文章

网站地图xml地图