32 lines
680 B
Markdown
32 lines
680 B
Markdown
|
|
# 类型转换
|
||
|
|
|
||
|
|
```csharp
|
||
|
|
[typeconvert(typeof(stringtohumanTypeConvert))]
|
||
|
|
public class Human
|
||
|
|
{
|
||
|
|
public string Name {get; set; }
|
||
|
|
public Human Child {get; set; }
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
```xaml
|
||
|
|
<windows.resource>
|
||
|
|
<local:Human x:key="human" Child="ABC"/>
|
||
|
|
</windows.resource>
|
||
|
|
```
|
||
|
|
|
||
|
|
```csharp
|
||
|
|
public class stringtohumanTypeConvert:typeconvert
|
||
|
|
{
|
||
|
|
public override object convertfrom(Itypedescriptioncontext context,system.globalization.culture.CultureInfo culture,object value)
|
||
|
|
{
|
||
|
|
if(value is string)
|
||
|
|
{
|
||
|
|
Human h=new Human();
|
||
|
|
h.Name=value as string;
|
||
|
|
return h;
|
||
|
|
}
|
||
|
|
return base.convertfrom(context, culture,value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|