win10 uwp clone
最后更新于:2022-04-01 20:23:02
clone 可以用MemberwiseClone来复制一个类
但这个复制是浅复制,创建一个新的object然后复制值字段,对于引用就直接复制引用,不复制引用的本身,指向同样引用
如果要复制引用,可以使用序列化和反序列化复制类
序列化和反序列化可以使用
序列化本来有BinaryFormatter 而现在没有了SoapFormatter
可以用的微软的XmlSerializer,需要`using System.Xml.Serialization;`
Nuget下载很多的json Newtonsoft.Json
需要在项目管理
![这里写图片描述](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-04-08_5707636a74b4e.jpg "")
安装
简单的通讯录
~~~
public class caddressBook: notify_property
{
public caddressBook()
{
}
///
/// 标识符
///
public string id
{
set
{
_id = value;
OnPropertyChanged();
}
get
{
return _id;
}
}
///
/// 通讯人姓名
///
public string name
{
set
{
_name = value;
OnPropertyChanged();
}
get
{
return _name;
}
}
///
/// 联系方式
///
public string contact
{
set
{
_contact = value;
OnPropertyChanged();
}
get
{
return _contact;
}
}
///
/// 工作地点
///
public string address
{
set
{
_address = value;
OnPropertyChanged();
}
get
{
return _address;
}
}
///
/// 城市
///
public string city
{
set
{
_city = value;
OnPropertyChanged();
}
get
{
return _city;
}
}
///
/// 备注
///
public string comment
{
set
{
_comment = value;
OnPropertyChanged();
}
get
{
return _comment;
}
}
///
/// 输入正确
///
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;
///
/// 一直添加value
///
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(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,一个显示原有的,一个显示复制的
~~~
~~~
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(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;
}
~~~
';