clone 可以用MemberwiseClone来复制一个类
但这个复制是浅复制,创建一个新的object然后复制值字段,对于引用就直接复制引用,不复制引用的本身,指向同样引用
如果要复制引用,可以使用序列化和反序列化复制类
序列化和反序列化可以使用
序列化本来有BinaryFormatter 而现在没有了SoapFormatter
可以用的微软的XmlSerializer,需要`using System.Xml.Serialization;`
Nuget下载很多的json Newtonsoft.Json
需要在项目管理
![这里写图片描述](https://box.kancloud.cn/2016-04-08_5707636a74b4e.jpg "")
安装
简单的通讯录
~~~
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;
}
~~~
- 前言
- UWP win10 app 新关键字x:Bing
- win10应用 UWP 使用MD5算法
- win10 UWP读写文件
- UWP appButtonBar样式
- C# 6.0 $&quot;Hello {csdn}&quot;
- Win10 UWP xaml 延迟加载元素
- UWP xaml 圆形头像
- UWP 绘制图形
- win10 uwp 通知Toast
- win10 UWP 显示地图
- win10 uwp 参考
- win10 uwp clone
- win10 uwp 装机必备应用 含源代码
- RichEditBox 使用自定义菜单
- win10 UWP FlipView
- win10 UWP 获取系统信息
- win10 UWP 申请微软开发者
- win10 UWP button
- win10 UWP Markdown 含源代码
- win10 UWP 应用设置
- win10 UWP 九幽数据分析
- win10 UWP 圆形等待
- win10 UWP 标题栏后退
- win10 UWP 单元测试
- win10 UWP 你写我读
- win10 UWP RSS阅读器
- win10 UWP MessageDialog 和 ContentDialog
- win10 UWP Hmac
- win10 UWP GET Post
- Win10 UWP Intro to controls and events
- win10 UWP Controls by function
- win10 uwp App-to-app communication 应用通信