Enforce global namespace in XAML auto generated files - c#

My problem is distinct from this other question Enforce Global Namespace in XAML in that I don't have a class with the same name as the enclosing namespace, yet I couldn't think of a more appropriate title that would differentiate the two.
If any part of my XAML object's namespace is also the root of another namespace, Visual Studio seems to prefer my XAML object's namespace, which manifests as a compiler error.
Here is an example that shows how part of my namespace WpfApp1.System.Views clashes with global::System:
<Window x:Class="WpfApp1.System.Views.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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!-- Amazing content goes here -->
</Grid>
</Window>
There are compilation errors shown in the Errors pane and obj/System/Views/MainWindow.g.i.cs shows this with a squiggly red underline under the System part of System.Windows.Window:
namespace WpfApp1.System.Views {
public partial class MainWindow : System.Windows.Window {
// ...
}
}
Obviously my toy example only stops me from having a System namespace anywhere in my hierarchy, but it also stops me from having any 3rd-party names (like DevExpress) if I'm using one of those 3rd-party components in XAML.
Without renaming either my own namespace (or the referenced components' namespaces) can I ask Visual Studio to use global:: namespaces in its generated XAML code?

Related

Control Does Not Exist in Namespace

I'm attempting to use the MediaUriElement in the custom (WPFMediaKit).
I've added it into the namespace like so;
xmlns:controls="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
and then I have tried to access it;
<Grid>
<controls:MediaUriElement x:Name="mediaUriElement" />
</Grid>
however I just get an error: The name MediaUriElement does not exist in this namespace. VS does give me a suggestion to use it when I am typing though;
How can I correctly add this into my project?
I've tried and it is worked. What I've done:
Switched project from Framework 4.5 to Framework 4.0
Added references such as DirectShowLib-2005.dll and WPFMediaKit.dll to the project
Declared xaml namespace xmlns:controls="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit" like that:
<Window x:Class="ExcelExportWpfApplication.MainWindow"
... The code omitted for the brevity...
xmlns:controls="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
xmlns:vm="clr-namespace:ExcelExportWpfApplication.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="525"
WindowStartupLocation="CenterScreen">
And just written in xaml:
<controls:MediaUriElement Grid.Row="2"/>
No errors

WPF user control inheritance issues

I made a WPF control in a library project and would like to extend it with a new one.
<UserControl x:Class="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI.EmbeddingUserControl"
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>
I have tried to extend it like this:
<src:EmbeddingUserControl x:Class="Interaxalab.Desktop.Modules.PrototipoCable.CustomViews.InteractionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI"
Name="InteractionWorksheetView" Height="321.613" Width="471.396"
>
<Grid>
<WindowsFormsHost x:Name="windowsFormsHost1" HorizontalAlignment="Left" Height="284" VerticalAlignment="Top" Width="471"/>
</Grid>
</src:EmbeddingUserControl>
However, I get an error message saying that the name "EmbeddingUserControl" does not exist in namespace "Genesyslab.Desktop.Modules.ExtensionUtils85.GUI".
The name clearly does exist, since the xaml.cs can find it, but for some reason the xaml cannot.
Any help would be appreciated.
Long story short - you cannot inherit control with xaml by another control with xaml (and does it makes sense even to do so?). In your case, EmbeddingUserControl does not contain any visual tree elements (just empty grid), so you can just do:
public class EmbeddingUserControl : UserControl {
// some methods, properties of your control
}
Then you can inherit exactly like you do already in your question (don't forget to inherit from EmbeddingUserControl both in xaml file and in code-behind file).
You can also inherit from user control with xaml, if your inherited control does not have xaml itself (so you can add\override logic).
If you need to inherit some visual stuctures - you have to switch from inheritance to composition. That is: your base control provides some placeholders where other controls may be placed, or even better allows to provide templates to format data items (like for example ItemsControl and ItemTemplate property). Then you just fill those placeholders with other controls if necessary.

WPF Resource Dictionary using code behind file can’t be found

When I include an assembly containing a ResourceDictionary using the following pack syntax:
"pack://application:,,,/WpfCore;component/ResourceDictionaries/ThemedControls.xaml"
It works as expected, but as soon as I add a code behind file to the XAML of the ResourceDictionary, the following error is thrown:
“An error occurred while finding the resource dictionary”
The code behind is added to the XAML in the usual way:
< ResourceDictionary x:Class="com.mycompany.WpfCore.ResourceDictionaries.ThemedControls"
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">
</ResourceDictionary>
and looks like this:
namespace com.mycompany.WpfCore.ResourceDictionaries
{
public partial class ThemedControls : ResourceDictionary
{
public ThemedControls ()
{
InitializeComponent();
}
}
}
Intuition tells me this is a namespace problem, but all the variations I've tried fail. What am I doing wrong or is this a limitation of WPF ResourceDictionaries?
Edit:
Seems the question detail was called out and found to be wanting.
The initial example had the namespace simplified. The default namespace for the WpfCore project is com.mycompany.WpfCore which I have now added into the code examples above.
The ThemedControls.xaml and ThemedControls.xaml.cs files are located in a subfolder called ResourceDictionaries within the WpfCore project folder.
The resulting assembly is used as a referenced assembly in another solution and this is where the Pack URI is being used.
Edit 2:
After playing around with the build action for the xaml files (changing from page to resource and back again) things started working. Marking Sheridan's answer as correct.
I don't think that you have declared your ResourceDictionary quite correctly... the application name really should be in the namespace. This should work... at least it works for me:
<ResourceDictionary
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"
x:Class="WpfCore.ResourceDictionaries.ThemedControls">
</ResourceDictionary>
Code behind:
namespace WpfCore.ResourceDictionaries
{
public partial class ThemedControls : ResourceDictionary
{
public ThemedControls()
{
InitializeComponent();
}
}
}

Cannot Define Object in XAML

All, I have a user control. In the XAML markup for this control I want to define a resource (instatiate an object called cellColorConverter of the class CellColorConverter which is defined in the same namespace as the control. I have
<UserControl x:Class="ResourceStudio.Resource.Resource"
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" Height="331.2" Width="340">
<UserControl.Resources>
<ResourceStudio.Resource:CellColorConverter x:Key ="cellColorConverter"/> // <- Error.
</UserControl.Resources>
This is giving a compile-time error saying
The namespace prefix ResourceStudio.Res is not defined.
What am I doing wrong here?
Thanks for your time.
Update: I now have
xmlns:local="clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio"
mc:Ignorable="d" Height="331.2" Width="340">
<UserControl.Resources>
<local:CellColorConverter x:Key ="cellColorConverter"/>
</UserControl.Resources>
The CellColorConverter class is in the name space ResourceStudio.Resource, defined as
namespace ResourceStudio.Resource
{
public class CellColorConverter : IMultiValueConverter
{
// ...
}
}
I still get the following error
The name "CellColorConverter" does not exist in the namespace "clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio". F:\Camus\ResourceStudio\ResourceStudio\ResourceStudio\Resource\Resource.xaml
In the XAML:
<ResourceStudio.Resource:CellColorConverter />
The ResourceStudio.Resource is an XML namespace for CellColorConverter.
You need to map this XML namespace to a .NET namespace:
<UserControl xmlns:ResourceStudio.Resource="clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio" ... />
The actual namespace and assembly name depend upon what you've called them in your code.
This article provides more information.
Also, you can make the namespace shorter:
xmlns:local="clr-namespace:..."
<local:CellColorConverter ... >

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