clone 可以用MemberwiseClone来复制一个类
但这个复制是浅复制,创建一个新的object然后复制值字段,对于引用就直接复制引用,不复制引用的本身,指向同样引用
如果要复制引用,可以使用序列化和反序列化复制类
序列化和反序列化可以使用
序列化本来有BinaryFormatter 而现在没有了SoapFormatter
可以用的微软的XmlSerializer,需要using System.Xml.Serialization;
Nuget下载很多的json Newtonsoft.Json
需要在项目管理
安装
简单的通讯录
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 + "
");
}
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;
}