I have the following line of code defined in codebehind in silverlight:
Path path = XamlReader.Load( "<Path Data=\"F1 M 44.207,34.0669C 44.4841,33.7278 44.7612,33.3886 45.0383,33.0494\" />" ) as Path;
No idea why it's happening...
As the exception states, you are missing the default namespace in your XAML document. The <Path> element needs an XML namespace.
Add the attribute xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" to your Path element.
http://msdn.microsoft.com/en-us/library/ms747086.aspx
And for reference, in case you're not familiar with them, as XAML is just XML, here is an introduction to XML namespaces:
http://www.w3schools.com/xml/xml_namespaces.asp
Related
I'm executing a roslyn script that tries to define and open a WPF window.
Amongst other things, my script
defines an attached behavior
defines a XAML string, based on which I create a WPF Window. In this XAML code, I'd like to use the TextBoxCursorPositionBehavior defined in my script.
my script (.csx) file looks similar to
public class TextBoxCursorPositionBehavior : DependencyObject
{
// see http://stackoverflow.com/questions/28233878/how-to-bind-to-caretindex-aka-curser-position-of-an-textbox
}
public class MyGui
{
public void Show()
{
string xaml = File.ReadAllText(#"GUI_Definition.xaml");
using (var sr = ToStream(xaml))
{
System.Windows.Markup.ParserContext parserContext = new System.Windows.Markup.ParserContext();
parserContext.XmlnsDictionary.Add( "", "http://schemas.microsoft.com/winfx/2006/xaml/presentation" );
parserContext.XmlnsDictionary.Add( "x", "http://schemas.microsoft.com/winfx/2006/xaml" );
parserContext.XmlnsDictionary.Add("i","clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity");
// ?? How can i define this properly?
parserContext.XmlnsDictionary.Add("behaviors", "clr-namespace:;assembly=" + typeof(TextBoxCursorPositionBehavior).Assembly.FullName);
var window = (System.Windows.Window)XamlReader.Load(sr, parserContext);
window.ShowDialog();
}
}
}
and assume the GUI_Definition.xaml looks like
<Window x:Class="System.Windows.Window" Height="300" Width="300" >
<Grid>
<!-- how can i attach my behavior here properly? -->
<TextBox behaviors:TextBoxCursorPositionBehavior.TrackCaretIndex="True"/>
</Grid>
</Window>
But the problem is, how can I reference TextBoxCursorPositionBehavior correctly in XAML?
Roslyn doesn't allow to use namespaces in script files, so TextBoxCursorPositionBehavior must be defined outstide of a namespace (i.e. I suppose it will fall into the global namespace).
But then, how can I reference it in XAML? I've tried defining the namespace reference with "clr-namespace:;assembly=" + typeof(TextBoxCursorPositionBehavior).ToString(), but that doesn't work.
Simply "clr-namespace:" (i.e. without assembly reference) doesn't work either.
Is there any way to reference TextBoxCursorPositionBehavior from within the XAML definition?
In your code instead of assembly you use:
typeof(TextBoxCursorPositionBehavior).ToString()
This is not an assembly name. Change it to:
parserContext.XmlnsDictionary.Add("behaviors", "clr-namespace:;assembly=" + Assembly.GetExecutingAssembly().FullName);
And it should work fine (at least works for me, but I don't test with Roslyn script but just regular WPF application).
I think I know what's happening ... Roslyn creates a custom Submission type for scripts, and seems everything - including the definition of TextBoxCursorPointerBehavior - is a sub-class of this submission type. I.e.,
var inst = new TextBoxCursorPositionBehavior();
string typeName = inst.GetType().FullName;
typeName will not be "TextBoxCursorPointerBehavior", but rather "Submission#0+TextBoxCursorPositionBehavior".
At the same time, I can NOT reference this from XAML (e.g. by behaviors:Submission#0+TextBoxCursorPositionBehavior.TrackCaretIndex="True") as it won't parse the name correctly (# is an invalid token there).
In theory, it might be possible to rename Roslyn's submission type to something that is actually referencable via XAML - in my case though, I cannot do that.
Which unfortunately currently means I don't see any solution to my issue, other than possibly outsourcing this code to a separate pre-compiled DLL (but that's not quite the point of scripting either)
I have a Xml file:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:inf="http://www.granfol.com/luna" >
<Employees>
<Employee Department="e" kk="TEST11111111">
<Name>TestName11111</Name>
</Employee>
</Employees>
</ResourceDictionary>
and I want to set the xmlns to
xmlns=""
or
Directly Remove the xmlns default namespace.
I have tried
XElement X = XElement.Load(#"FILEPATH");
IEnumerable<XAttribute> XI = X.Attributes();
foreach (XAttribute x in XI)
Console.WriteLine(x);//Show all namespace in root
X.SetAttributeValue("xmlns","");
foreach (XAttribute x in XI)
Console.WriteLine(x); //Here shows the namespace xmlns has change to ""
X.Save(#"FILEPATH"); //But when save File it throws Exception and failed
When save, it throws exception like "The prefix cannot be redefined from to within the same start element tag"
I also tried to use
X.Attributes("xmlns").Remove();
to replace
X.SetAttributeValue("xmlns","");
above,
It doesn't throw exception when save,
but when I open the xml file, I see the "xmlns" doesn't disappear..
then I test and find
X.Attributes("xmlns")
doesn't contain xmlns, but contain "xmlns:x, xmlns:sys, xmlns:inf"
So is there some way to set value or remove xmlns namespace in Xml ?
I have thought a way is that to reproduce the whole xml file again,
but I'm not sure it's easy to do if the xml file is large.
I don't really know how to do it, but suppose it's not a good idea.
I have this image in a XAML file of my project:
<Image Source="/my.namespace;component/Resources/document_plain.png" Margin="5" />
The image is in a directory /Resources/document_plain.png at the root of my project folder. The settings of the image are:
But, when running a debug instance, I immediately get an XamlParseException:
The string "/my.namespace;component/Resources/document_plain.png" in the attribute "Source" could not be converted to type "System.Windows.Media.ImageSource".
The file or assembly "my.namespace, Culture=neutral" or a dependency could not be found. The system cannot find the file. Error in object "System.Windows.HierarchicalDataTemplate" in markup file "MyProject;component/view/mainwindow.xaml", line 20, position 12.
Which strikes me as strange, because IMHO the project is correctly set up. What am I missing/doing wrong?
Cudos to dlev from the comments:
It looks like the assembly name is "MyProject", so your string should probably be Source="MyProject;component/Resources/document_plain.png"
I'm trying to load a ResourceDictionary object into code so that I can check if it contains a certain value before trying to bind to it, but for some reason it can't find the resource. It might be something basic, ie having the wrong build action or URI but can someone point me in the right direction?
The .cs file and the Images.xaml resource dictionary are both in the same folder and namespace
This is the code that is failing
ResourceDictionary resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("Images.xaml", UriKind.Relative);
Any idea what I'm doing wrong? getting error message
{"Cannot locate resource 'images.xaml'."}
The XAML parser probably adds an application authority when converting URIs, in code you may need to add that yourself. Build action should just be the default for ResourceDictionaries, whatever that may be.
I am trying to load a Silverlight project to read every XAML file by creating an instance using reflection, Activator.CreateInstance, of every XAML class for reading its controls.
C# Code:
string strPath = "SilverlightUI.dll";
StreamResourceInfo sri = Application.GetResourceStream(new Uri(strPath, UriKind.RelativeOrAbsolute));
AssemblyPart assemblyPart = new AssemblyPart();
Assembly assembly = assemblyPart.Load(sri.Stream);
Type[] typeArray = assembly.GetExportedTypes();
foreach (Type type in typeArray)
{
object ctl = (object)Activator.CreateInstance(type);
// Following exception is occurring while creating an instance using above line of code
// Exception "Cannot find a Resource with the Name/Key ComboBoxStyle"
}
Perhaps, reflection is not able to recognize Silverlight style ComboBoxStyle. How can i possibly create an instance to read every control in the XAML file dynamically?
I have managed to find the required solution to my problem after struggling with the Google.
Copy all the Style Resources from Silverlight project (intended to load).
Paste them in the App.xaml of the Master/Caller Silverlight project or application, which is using the reflection code to load the Silverlight Controls information
Following these steps will eliminate the XAML Parse Exception of missing Style.
Cannot find a Resource with the Name/Key ComboBoxStyle
Reference: XAML Parser cannot find resource within dynamically loaded XAP when creating form instance
I was able to load custom controls using the XamlReader class.
I am using plain string that contains the XAML of the control not like your reflection idea.
//string xaml = "<...>";
var content = XamlReader.Load(xaml) as FrameworkElement;
this.scrollViewer.Content = content;
The type XamlReader is in System.Windows.Markup.
If it is possible in your case you can try to get XAML resources from your assembly and read them to string. Then use the provided code. After you have the content variable you can do anything using the Silverlight API to the control.
Hope this will help you.