Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit b8ad8c5

Browse files
author
gumme
committed
Merge remote-tracking branch 'remotes/origin/WpfDesignerXamlParserNativeTypesConversion'
2 parents a8b03d9 + 6252f3b commit b8ad8c5

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -668,31 +668,52 @@ public void AddBrushAsResource()
668668
}
669669

670670
[Test]
671-
public void AddStringAsResource()
671+
public void AddNativeTypeAsResource(object component, string expectedXamlValue)
672672
{
673673
DesignItem textBlock = CreateCanvasContext("<TextBlock/>");
674674
DesignItem canvas = textBlock.Parent;
675675

676676
DesignItemProperty canvasResources = canvas.Properties.GetProperty("Resources");
677677

678-
DesignItem str = canvas.Services.Component.RegisterComponentForDesigner("stringresource 1");
679-
str.Key = "str1";
678+
DesignItem componentItem = canvas.Services.Component.RegisterComponentForDesigner(component);
679+
componentItem.Key = "res1";
680680

681681
Assert.IsTrue(canvasResources.IsCollection);
682-
canvasResources.CollectionElements.Add(str);
682+
canvasResources.CollectionElements.Add(componentItem);
683+
684+
DesignItemProperty prop = textBlock.Properties[TextBlock.TagProperty];
685+
prop.SetValue(new StaticResourceExtension());
686+
prop.Value.Properties["ResourceKey"].SetValue("res1");
683687

684-
textBlock.Properties[TextBlock.TextProperty].SetValue(new StaticResourceExtension());
685-
DesignItemProperty prop = textBlock.Properties[TextBlock.TextProperty];
686-
prop.Value.Properties["ResourceKey"].SetValue("str1");
688+
string typeName = component.GetType().Name;
687689

688690
string expectedXaml = "<Canvas.Resources>\n" +
689-
" <Controls0:String x:Key=\"str1\">stringresource 1</Controls0:String>\n" +
691+
" <Controls0:" + typeName + " x:Key=\"res1\">" + expectedXamlValue + "</Controls0:" + typeName + ">\n" +
690692
"</Canvas.Resources>\n" +
691-
"<TextBlock Text=\"{StaticResource ResourceKey=str1}\" />";
693+
"<TextBlock Tag=\"{StaticResource ResourceKey=res1}\" />";
692694

693695
AssertCanvasDesignerOutput(expectedXaml, textBlock.Context, "xmlns:Controls0=\"clr-namespace:System;assembly=mscorlib\"");
694696
AssertLog("");
695697
}
698+
699+
[Test]
700+
public void AddStringAsResource()
701+
{
702+
AddNativeTypeAsResource("stringresource 1", "stringresource 1");
703+
}
704+
705+
[Test]
706+
public void AddDoubleAsResource()
707+
{
708+
AddNativeTypeAsResource(0.0123456789d, "0.0123456789");
709+
}
710+
711+
[Test]
712+
public void AddInt32AsResource()
713+
{
714+
const int i = 123;
715+
AddNativeTypeAsResource(i, "123");
716+
}
696717
}
697718

698719
public class MyMultiConverter : IMultiValueConverter

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public XamlPropertyValue CreatePropertyValue(object instance, XamlProperty forPr
198198

199199
XmlElement xml = _xmlDoc.CreateElement(prefix, elementType.Name, ns);
200200

201-
if (hasStringConverter && XamlObject.GetContentPropertyName(elementType) != null) {
201+
if (hasStringConverter && (XamlObject.GetContentPropertyName(elementType) != null || IsNativeType(instance))) {
202202
xml.InnerText = c.ConvertToInvariantString(instance);
203203
} else if (instance is Brush && forProperty != null) { // TODO: this is a hacky fix, because Brush Editor doesn't
204204
// edit Design Items and so we have no XML, only the Brush
@@ -219,8 +219,6 @@ public XamlPropertyValue CreatePropertyValue(object instance, XamlProperty forPr
219219
}
220220
}
221221
}
222-
} else if (instance is string) {
223-
xml.InnerText = (string)instance;
224222
}
225223

226224
return new XamlObject(this, xml, elementType, instance);
@@ -279,5 +277,10 @@ internal string GetPrefixForNamespace(string @namespace)
279277

280278
return prefix;
281279
}
280+
281+
bool IsNativeType(object instance)
282+
{
283+
return instance.GetType().Assembly == typeof(String).Assembly;
284+
}
282285
}
283286
}

0 commit comments

Comments
 (0)