1

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

OMG


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?

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • 4
    *Why* do you want to have a class without a namespace, when that's pretty much universally regarded as a bad idea? (It's not as bad in C# as in Java, but it's still a bad idea.) You can use the same class in different projects while still putting it in a namespace... – Jon Skeet Jun 12 '14 at 10:41
  • CLR Namespaces and XAML Namespaces are *not* the same. To define a XAML Namespace, you need to have a CLR Namespace. – Sheridan Jun 12 '14 at 10:48
  • Ok, so there is not solution to the problem which occurs due to having bad idea, which is bad only in wpf, right? – Sinatr Jun 12 '14 at 13:35
  • I *think* you mean adding this class to the *default* XAML namespace `http://schemas.microsoft.com/winfx/2006/xaml/presentation`? Is that what you want? – Federico Berasategui Jun 12 '14 at 13:45
  • @HighCore, no idea, but if it will solve above issue, can I? – Sinatr Jun 12 '14 at 13:48
  • @Sinatr it is not very clear to me what *your issue* is. You mention dlls and copying cs files, which doesn't have anything to do with anything. Can you please explain in detail *why* you're trying to do this? – Federico Berasategui Jun 12 '14 at 13:54
  • @HighCore, see edit (last paragraph), does it makes sense? I'd like to define class without namespace and use it in current and many others projects. Having declared namespace will make it uncomfortable for the use in *current* project (as well as in others): having to declare another `clr-namespace` to use in xaml, having to use `using` in classes. I am looking for the way to make it as simple as possible. If it is possible. – Sinatr Jun 12 '14 at 13:58
  • `What about specifying full name directly in xaml` - you can't do that. And it doesn't make sense. XAML is XML, and XML is not C#. There's no such thing as "fully qualified names" in XML. There's only Xmlns. – Federico Berasategui Jun 12 '14 at 13:59
  • Why don't you simply put this class in a class library with a proper `XmlnsDefinitionAttribute`? - you're overcomplicating yourself too much with this.. – Federico Berasategui Jun 12 '14 at 14:00
  • @HighCore, maybe, but I don't want to have dll, nor copy cs-file (with some class inside). I want to use same cs-file in different projects. Projects will obviously have different namespaces. Using either of them will make it easier to use that cs-file in that specific project, but *harder* in others. Using different namespace will make it *harder* to use in **all** projects. Without `xaml` (or wpf) idea with using *no namespace at all* is brilliant for linking cs-files. In xaml is a problem. – Sinatr Jun 12 '14 at 14:03
  • 1
    @Sinatr sorry, NO. People don't "copy cs files" around. They create proper reusable libraries (dlls) and reference them. That's simply *not* how C# works in general. You seem to have the misconception that referencing a namespace is somehow *difficult*, when it's not. Neither from C# nor XAML. I suggest you rethink your approach entirely. And if all your classes are in the same namespace that's going to be a huge **mess.** Namespaces are precisely intended to provide containers for types to separate them and organize them. Not using them is going back to the dark age of computer science. – Federico Berasategui Jun 12 '14 at 14:10
  • @HighCore, I am not a big fun of namespaces within single project. If you look at linked question, there are peoples who want to reference class from other project without having dll in-between (yes, in-between). Point is, same functionality without encumbrance. Most people will just copy from one project into other. **Only** if they think what this is something worth for dll, they will make dll. – Sinatr Jun 12 '14 at 14:16
  • @Sinatr do what you want. But XAML does not support fully qualified type names. – Federico Berasategui Jun 12 '14 at 14:18
  • @HighCore, ok. You could think about this as referencing peace of code (located in some cs-file) *statically*. It has benefit, what there is only one place to edit content and it's statically linked to every project (without need to have dll in-between). I think, but don't remember already, there is such concept of *static linking* in old days (BC3.1). Why would Visual Studio allow you to make such linking (alt-dragging from one project to another) if it would be prohibited? – Sinatr Jun 12 '14 at 14:26
  • 2
    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. – Leandro Jun 12 '14 at 15:03
  • @LeandroTaset, yes, lol, thanks! Make it an answer please, for downvoters feel ashamed. – Sinatr Jun 12 '14 at 15:07

1 Answers1

3

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...

Leandro
  • 1,560
  • 17
  • 35