开发者

Convert from string to data in silverlight?

开发者 https://www.devze.com 2023-03-24 15:45 出处:网络
Basically I\'m trying to do this: Path path = new Path( ); string sData = \"M 250,40 L200,20 L200,60 Z\";

Basically I'm trying to do this:

Path path = new Path( ); 

string sData = "M 250,40 L200,20 L200,60 Z";

var converter = TypeDescriptor.GetConverter( typeof( Geometry ) );
path.Data = ( Geometry )converter.ConvertF开发者_JAVA技巧rom( sData );

but it won't compile, silverlight does not appear to have a TypeDescriptor class...


Try this:-

  Path path = XamlReader.Load("<Path Data=\"M 250,40 L200,20 L200,60\" />") as Path;

Edit

Should have been:

  public static GeneratePath(string data)
  {
      string pathEnvelope = "<Path xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Data=\"{0}\"/>")
      return XamlReader.Load(String.Format(pathEnvelope, data)) as Path;
  }

Usage:-

  string data = "M 250,40 L200,20 L200,60";

  Path path = GeneratePath(data);

See follow up question: xaml parse exception when attempting to load xaml from codebehind

0

精彩评论

暂无评论...
验证码 换一张
取 消