c# - Enforce Global Namespace in XAML -


in xaml, custom namespace can imported xmlns directive:

xmlns:custom="clr-namespace:greatstuff" 

in c#, namespace can imported with

using greatstuff; 

moreover, evaluation of namespace based on global (root) namespace can enforced follows:

using global::greatstuff; 

how can enforce evaluation based on global namespace in xaml?


background:

i facing (admittedly obscure) situation there such namespace global::greatstuff, contains wpf control class named ... greatstuff (i.e. qualified, that's global::greatstuff.greatstuff in c#). in same namespace, want use control in wpf window.

interestingly, in constellation, cannot use name/x:name attribute on controls of type global::greatstuff.greatstuff in xaml file window:

the type name 'greatstuff' not exist in type 'greatstuff.greatstuff'. (cs0426)

note same file compiles fine if not specify name/x:name attribute!

now, why should compiler assume setting name/x:name attribute, trying access called greatstuff.greatstuff.greatstuff?

the answer can found examining .g.cs file generated window xaml file. in file, xaml snippet

<custom:greatstuff x:name="stuff"/> 

gets compiled following c# snippet:

internal greatstuff.greatstuff stuff; 

that is, fully-qualified name used, without explicit global namespace marker.

of course, field in class in namespace global::greatstuff, of wrapped in

namespace greatstuff { 

and so, poor c# compiler cannot assume stuff supposed of type global::greatstuff.greatstuff.greatstuff. avoided if in

xmlns:custom="clr-namespace:greatstuff" 

i enforce mentions of namespace prefix converted while enforcing global namespace.

for reasons external question, changing namespace and/or class names not option here.

this issue occurs when of following true:

  1. you have class identical name namespace containing it.
  2. within same namespace, have xaml file instance of object declared within namespace.
  3. that specific object instance given name or x:name attribute.

if cannot modify make 1 of conditions false, working serious restrictions.

as far know, limitation of xaml compiler, , need contact microsoft resolving it. other thing can think of try work around custom build process builds xaml, allows edit generated files, builds code. require research figure out how set up.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -