dotnet / dotnet/wpf

Enable Top-Level Namespaces for Xaml Classes in Visual Basic

Open
#4,014 6 comments 1 reaction 0 assignees View on GitHub
Feature Request
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

**Targeting:** Visual Basic + XAML

**Abstract**
Being able to create a xaml class under a top-level namespace other than the project’s Root Namespace in Visual Basic.
For example: The desired is: x:Class="SomeNamespace.MyClass" creates 'SomeNamespace' as a top-level (global) namespace.
What happens for now: x:Class="SomeNamespace.MyClass" creates 'SomeNamespace' as a nested namespace under the project's RootNamespace.

**Why do we need this?**
Consider the following scenario, you have reusable code files that are added to your Visual Studio projects as links, these files contains xaml files that define some controls, some of these controls are used within the others. That means you need to declare clr-namespace of some of these controls within xaml code of the others. Here, if the clr-namespace contains the root-namespace of current project (and it should, as long as you are using VB), then the reusable file cannot be shared across projects anymore. The solution for this is to be able to define xaml classes under another top-level namespace so it can be reusable across different projects when referencing its file as link.

**A solution, hard on developers:**
Is to erase the ‘Root namespace’ field found in the project properties page. In this case any namespace declared within x:Class declaration will be top-level (Global). Anyhow in this case a namespace declaration should be added manually to all code files, which is a minus in productivity.

**Another impractical solution:**
In your xaml, define x:class like this: x:Class=“SomeNamespac.ClassName”.
In related code-behind file, include the class into Global.SomeNamespace namespace.
Go to path obj\Debug and change namespace in files ClassName.g.i.vb and ClassName.g.vb to Global.SomeNamespace.
Done! now the xaml class is in a separate top-level namespace, but that will last only until the next time the IDE generates its auto generated files (when you clean project or rebuild it).

**The perfect solution**:
Is to make the Visual Basic's XAML interpreter acts as follows:
* x:Class="SomeNamespace.MyClass" means generating SomeNamespace as a top-level namespace not nesting it under the root namespace of the project, this is the major idea in this feature suggestion.
* x:Class="MyClass" means generating 'MyClass' under the root namespace of the project (this is the case already), and make that equivalent to: x:Class="RootNamespace.MyClass" (this is not the case now).
* x:Class="RootNamespace.SomeNamespace.MyClass" puts 'SomeNamespace' as a nested namespace under the project's root namespace.

Many Thanks!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.