Reputation: 22008
I have a class without namespace (should I say, declared in global namespace?)
public static class Test
{
public static string Something { get; set; }
}
It can be accessed from other classes of the project without specifying any namespace, like it's located in the same namespace
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
Test.Something = "bla"; // no problem
InitializeComponent();
}
}
}
Now I am trying to access such class from xaml. And I have problems.
Typing its name like this
<TextBlock Text="{Binding Source={x:Static Test.Something}}"/>
gives intellisence error
Test is not supported in WPF project.
Trying to add clr-namespace
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
...
<TextBlock Text="{Binding Source={x:Static local:Test.Something}}"/>
will produce compiler error
Cannot find the type 'Test'. Note that type names are case sensitive.
Strangely enough, intellisence think everything is ok this time
Putting Test
into WpfApplication1
namespace will make it working ofc. But I want to have it without namespace.
Question: How to declare global namespace as prefix in xaml? Is it really that bad to have some classes without namespace? My idea was to use same class in different projects (as a link) without making dll (one exe-file, I know it is possible to embed dll into exe, but seriously ...) or copying cs-files (would required simultaneous editing multiple cs-files if class has to be updated, very bad).
I though about one more possibility to demonstrate problem. Imagine we have something declared in different namespace (to example, project namespace is WpfApplication1
, but we create class and declare it in SomeNamespace
namespace). This will required to use using
or specify full name (including namespace, but excluding assembly) when accessing it inside project classes. However, in xaml
we have only option similar to using
- it's clr-namespace
. What about specifying full name directly in xaml right at the place where it's needed and without using of clr-namespace
. Is it possible?
Upvotes: 1
Views: 1775
Reputation: 1555
Totally awkward, but you can actually declare the XAML namespace like xmlns:funky="clr-namespace:"
(note the empty declaration) and then refer to the class as funky:Test
. It whines all over at design time, but it runs without exceptions and produces the intended behavior (so far as to what I have been able to test).
PS: This answer brings about some other questions...
Upvotes: 3