How to add comments into a Xaml file in WPF? - c#

I used this syntax as I found online but it throws an error:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<!-- Cool comment -->
xmlns:System="clr-namespace:System;assembly=mscorlib"
'Name cannot begin with the '<' character, hexadecimal value 0x3C.
Line 4, position 5.' XML is not valid.

I assume those XAML namespace declarations are in the parent tag of your control? You can't put comments inside of another tag. Other than that, the syntax you're using is correct.
<UserControl xmlns="...">
<!-- Here's a valid comment. Notice it's outside the <UserControl> tag's braces -->
[..snip..]
</UserControl>

Found a nice solution by Laurent Bugnion, it can look something like this:
<UserControl xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:comment="Tag to add comments"
mc:Ignorable="d comment" d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Width="100"
comment:Width="example comment on Width, will be ignored......">
</Button>
</Grid>
</UserControl>
Here's the link:
http://blog.galasoft.ch/posts/2010/02/quick-tip-commenting-out-properties-in-xaml/
A commenter on the link provided extra characters for the ignore prefix in lieu of highlighting:
mc:Ignorable=”ØignoreØ”

You can't insert comments inside xml tags.
Bad
<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<!-- Cool comment -->
xmlns:System="clr-namespace:System;assembly=mscorlib">
Good
<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<!-- Cool comment -->

Just a tip:
In Visual Studio to comment a text, you can highlight the text you want to comment, and then use Ctrl + K followed by Ctrl + C. To uncomment, you can use Ctrl + K followed by Ctrl + U.

You cannot put comments inside UWP XAML tags. Your syntax is right.
TO DO:
<xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"/>
<!-- Cool comment -->
NOT TO DO:
<xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<!-- Cool comment -->
xmlns:System="clr-namespace:System;assembly=mscorlib"/>

For anyone learning this stuff, comments are more important, so drawing on Xak Tacit's idea (from User500099's link) for Single Property comments, add this to the top of the XAML code block:
<!--Comments Allowed With Markup Compatibility (mc) In XAML!
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ØignoreØ="http://www.galasoft.ch/ignore"
mc:Ignorable="ØignoreØ"
Usage in property:
ØignoreØ:AttributeToIgnore="Text Of AttributeToIgnore"-->
Then in the code block
<Application FooApp:Class="Foo.App"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ØignoreØ="http://www.galasoft.ch/ignore"
mc:Ignorable="ØignoreØ"
...
AttributeNotToIgnore="TextNotToIgnore"
...
...
ØignoreØ:IgnoreThisAttribute="IgnoreThatText"
...
>
</Application>

Related

Adding Icon property to xaml

I'm trying to add an icon to a wpf window, but I'm getting an xaml parsing exception whenever I add the following line to my code:
Icon="myIcon.ico"
My window tag looks like this (and runs fine) without the Icon property:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204">
If I add Icon="myIcon.ico" before the >, I get an error on the W in Width="1058.204"
Exception thrown: 'System.Windows.Markup.XamlParseException' in
PresentationFramework.dll
Additional information: 'Provide value on
'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an
exception.' Line number '8' and line position '58'.
So, the code erroring out looks like this:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204"
Icon="myIcon.ico">
I feel like I must be missing something very simple here. Based off of other posts here (How to change title bar image in WPF Window?) I feel like I'm doing it right.
Can anyone help? Thanks!
I solved this with a little bit of help from m.rogalski's suggestion and using information in here:How to reference image resources in XAML?
After importing my image to the project, I changed my code to look like this, and it worked:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204"
Icon="pack://application:,,,/my folder/myIcon.ico">
If I add Icon="myIcon.ico" before the >, I get an error on the W in Width="1058.204"
Add the myIcon.ico file to your project and set its Build Action property to Resource in the properties pane in Visual Studio.
You can then either set the Icon property of the window to a relative URI or a pack URI or you could specify the the icon as the default icon of your application under Project->Properties->Application->Icon and manifest.

c# WPF AlternateContent doesn't exist in the Namespace [...]

I am facing the need to have XAML Code only in design-time. I have found a nice Solution to this, which can be found here. It seems like there are a few guys having a Problem with the parse-timing of XmlnsDefinitionAttribute which is solved here.
In my case the issue is really, that I cannot compile my code, because AlternateContent can't be found in the namespace xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006". I haven't found any documentation to this namespace indeed and it seems strange, that the line mc:Ignorable="d"doesn't fail to build, which means I have at least one assembly containing the above namespace.
This is my Code:
#if DEBUG
[assembly: XmlnsDefinition("debug-mode", "MyNamespace")]
#endif
<Window x:Class="MyNamespace.SomeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:debug="debug-mode"
mc:Ignorable="d"
... >
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<mc:AlternateContent>
<mc:Choice Requires="debug">
<ResourceDictionary Source="pack://application:,,,/Styles;component/Generic.xaml" />
</mc:Choice>
</mc:AlternateContent>
...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
...
</Window>
My guess is, that I'm missing an Assembly-Reference, but I've not yet found a Documentation specifying a containing Assembly for AlternateContent. Do you have any ideas how I might get this to work?
Edit:
It seems like this is a common VS Problem which can be solved by this mc:Ignorable="d mc". This just doesn't work in my case, because I would need this to include Resources at Design-Time, which should be available to the VS-Designer :)
It seems like this is a common VS problem which can be solved by this code
mc:Ignorable="d mc"
This must be added to the root element of the view.
Note: that you already should have mc:Ignorable="d" in your root element, so you have to just add the mc to it.

UserControl content can't be set more than once

having some troubles with UserControl. I cannot get rid of "Content is set more than once error"
From what I've read, the common cause is that .. well, content is set more than once. For example having more than one child or setting content via Content=".." and then specifying another content between the tags.
However, this is UserControl generated by VisualStudio and I made no changes to the xaml.
<UserControl x:Class="TMEGadget.View.Toolbox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
Got any tips?
P.S: Can anyone tell me why, when I try to type "Hey,\n\nSome text...", the "Hey,\n\n" is deleted?
Edit: Restarting VS fixed the problem , thanks #Bolu

WPF / Xaml: tag not available. I just don't get it. Who can explain the compiler message?

I am new to WPF / Xaml. I tried to run a demo program to test WPF with NHibernate. I used the VmWrapperDemo but i cant get it compiled!
<Window x:Class="VmWrapperDemo.View.WindowMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:VmWrapperDemo.ViewModel;assembly=VmWrapperDemo"
Title="VM Collection Demo" Height="325" Width="500" WindowStartupLocation="CenterScreen">
<Window.Resources>
<!-- Value Converters -->
<vm:ContactConverter x:Key="ContactConverter" />
<vm:AmountConverter x:Key="AmountConverter" />
<vm:DateConverter x:Key="DateConverter" />
</Window.Resources>
</Window>
It is the
xmlns:vm ...
definition which i dont understand and gives me headaches. What does it mean i why do i get the following compiler error:
"The tag ContactConverter is not available"
I am lost! Please help!
Try this
change assembly=VmWrapperDemo to assembly=VmWrapperDemo.ViewModel
Assembly name in the link you gave is VmWrapperDemo.ViewModel not VmWrapperDemo

Adding x:Name to a user control gives error in generated code of page its used in

I am basically using a user control for the first time, so hopefully it's just a dumb mistake.
I have a simple user control
<UserControl x:Class="TestProject.WebApp.myUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid>
<sdk:AutoCompleteBox Name="myACB" ItemsSource="{Binding Data}" FilterMode="StartsWith" MinimumPrefixLength="2" >
</sdk:AutoCompleteBox>
</Grid>
</UserControl>
I am using the control in another page (in a stack panel):
<navigation:Page x:Class="TestProject.WebApp.myPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:TestProject.WebApp"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">
<Grid>
<StackPanel Orientation="Horizontal">
<local:myUserControl x:Name="myUC" />
</StackPanel>
</Grid>
</navigation:Page>
The control is in the same namespace as page. Removing the x:Name allows the project to compile and the control functionally works. When x:Name is specified I get an error in the generated code of the page:
Type 'TestProject.WebApp.myUserControl' is not defined.
It happens on these lines of code:
internal TestProject.WebApp.myUserControl myUC;
this.myUC == (TestProject.WebApp.myUserControl)this.FindName("myUC")
I tried with two different controls. Same thing. The generated code has "using TestProject.WebApp;" so I don't know why there is an error only when I have a named instance of the control.
I also cannot seem to use just the Name property. I even tried overloading the NameProperty in the control.
I was able to find a post about this here:
.g.vb file claims that Type X is not defined, even though it is!
Apparently, there is an issue when a project has a service reference (in my case I am using RIA services) and the user control resides in an assembly with the same root namespace as the one you are trying to use it in.
So for me it was,
TestProject.WebApp
Then I tried creating a new project for just the control
TestProject.Controls
Same problem.
Then, when I renamed the project and changed the namespace to
CustomControls
it worked.
Hopefully this saves someone else from some frustration and I really hope this problem can be fixed by MS soon.
What you are showing here should work. What is the default namespace in your project property's? Also did you rename your user control after you created it. Both of those issues could be causing you problems.

Categories

Resources