WPF: XAML namespace - c#

I have one assembly XAML registered in AssemblyInfo.cs:
[assembly: XmlnsDefinition("http://schemas.mysite.es/wpf", "SWC.ViewModels")]
And In my namespace I hace these classes:
namespace SWC.ViewModels
{
public class MenuViewModel
: ObservableCollection<MenuViewModel>
{
public MenuViewModel()
: base()
{
}
}
}
If I use the namespace in a user control XAML,
<UserControl x:Class="SWC.UserControls.UserMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:customMenu="http://schemas.mysite.es/wpf">
I can use the class in my XMLNS namespace,
<UserControl.Resources>
<customMenu:MenuViewModel x:Key="MenuItemsSource">
But, when I execute the application, the compiler said
The label 'MenuViewModel' dosen't exist in the namespace XML 'http://schemas.mysite.es/wpf'
All, can help me?? I'm crazy with this problem!!
Best regards,

I think you need to specify the assembly in which your customMenu controls exist. The assembly must also be referenced from the project (in the References section).
xmlns:customMenu="clr-namespace:customMenuNamespace;assembly=customMenuLibrary"
Otherwise I don't see how the compiler can find your implementation only through "http://schemas.mysite.es/wpf". What's at that address? The two microsoft schemas
http://schemas.microsoft.com/winfx/2006/xaml/presentation
http://http://schemas.microsoft.com/winfx/2006/xaml
work like some identifiers for the xaml compiler, there is nothing at those addresses.

Related

Generic base class for Pages/Views in UWP Windows 10 App

In a UWP-Windows 10 C#/XAML app using Template10, I'm currently trying to have my pages/views inherit from a base class that inherits from Windows.UI.Xaml.Controls.Page.
This has been working correctly, however when I try to make the base class generic, and include a type argument in the child class and the XAML in the following way, it does not work:
namespace App.Views
{
public abstract class InfoListViewBase<M> : Page where M : InfoListViewModelBase
{
public InfoListViewBase() { }
}
public sealed partial class ModelPage : InfoListViewBase<Model>
{
public Model()
{
InitializeComponent();
NavigationCacheMode = NavigationCacheMode.Disabled;
}
}
}
<local:InfoListViewBase
x:Class="App.Views.ModelPage"
x:TypeArguments="l:Model"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Behaviors="using:Template10.Behaviors"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:controls="using:Template10.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:App.Views"
xmlns:l="using:Library"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
</local:InfoListViewBase>
The error I receive is:
The name "InfoListViewBase`1" does not exist in the namespace "using:App.Views".
The error shows up every time I add the line x:TypeArguments="l:Model" to the XAML.
Multiple rebuilds, cleans, etc., in visual studio have not solved the problem.
Is there something I am doing wrong in the implementation of the generic in XAML?
Unfortunately, generic parameters in XAML still aren't supported in UWP app. You cannot use x:TypeArguments in XAML currently. But you may reference this thread to try to have a workaround.
If you still want this feature ,you can also submit this feature request to UserVoice.

namespace not found in xaml, but can be used in C# code

I have a WPF Project Home,it will reference a library project which defines namespace 'LibraryProjectExample', I want to use it in the Home Project,the namespace can not be found:
xmlns:view="clr-namespace:LibraryProjectExample"
But when I use using namespace LibraryProjectExample in C# code,I can use it normally.
I have checked that the LibraryProject has been referenced by the Home Project, I don't know why i can't use the namespace in xaml.Anyone can tell me,thanks!
You have to use as below in you Xaml page.
xmlns:object="clr-namespace:**namespace**;assembly=*assembly*"
here namespace is your packagename.classname and assembly is your packagename
For Example, xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
Thank You.
It looks like you're not adding the assembly to your xaml reference. Look in your LibraryProjectExample project and find it's package name, then you can append it to your namespace declarationlike so
xmlns:view="clr-namespace:LibraryProjectExample;assembly=YOUR_PACKAGE_NAME"
Besides the namespace you should also specify the name of the library project/assembly where the namespace is defined:
xmlns:view="clr-namespace:LibraryProjectExample;assembly=LibraryProject"
You need to change "LibraryProject" in the above sample markup to the actual name of the referenced project where the "LibraryProjectExample" namespace is defined.

Namespace error when adding viewmodel

I come to you with a very strange problem.
At the first look it's an easy thing but trust me it's make me crazy !
I just want to specify wich file I want to associate to xaml page in DataContext, I already did it on other xaml page so I don't understand why !
<UserControl x:Class="FinalMediaPlayer.Onglet.Musique.Home.HomeMusique"
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"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home"
xmlns:tools="clr-namespace:FinalMediaPlayer.Tools"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<viewmodel:HomeViewModelMusique></viewmodel:HomeViewModelMusique>(error here)
</UserControl.DataContext>
The name "HomeViewModelMusique" doesn't exist "clr-namespace:FinalMediaPlayer.Onglet.Musique.Home".
namespace FinalMediaPlayer.Onglet.Musique.Home
{
public class HomeViewModelMusique : NotifyPropertyChanged
{
As you can see I put the path in viewmodel but apparently the file don't exist in the namespace ...
Thanks by advance guys, Im gonna suicide very soon...
Are all the namespaces in the same assembly? If they are not you will need to specify the assembly in the namespace declaration.
E.g:
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home;assembly=AssemblyName"
Check that the namespace is actually correct, then clean\rebuild your solution. Please consider also to use a tool like Resharper that will fix these problems automatically
Thanks for your fast answers guys !
I already try that :
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home;assembly=FinalMediaPlayer"
It doesn't work, "FinalMediaPlayer" is the name of my project, is there what is expected in the field assembly ?
I Get this on my AssemblyInfos.cs :
[assembly: AssemblyTitle("FinalMediaPlayer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FinalMediaPlayer")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
However Im gonna try to use Resharper but this is not a real solution :/.
If I find the solution I will post it.
Yes sorry guys I didn't answer that question, of course I did it.
Tryed tot clean/regenerate/generate. Whith no success.
Really I can't believe it, I have spend all my afternoon on this little stupid problem, maybe it's a bug coming from visual studio.
Im gonna try on my other computer later after work.
Again, Thanks to search with me.
namespace FinalMediaPlayer.Onglet.Musique.Home
{
public class HomeViewModelMusique : NotifyPropertyChanged
{

Is this namespace collision due to a bug in the XAML to .NET code generation?

Below I describe how to reproduce an error I'm receiving. It behaves the same in VS 2010, 2012, and 2013. Breaking it into multiple projects, as I indicate below, is important.
Steps to reproduce the error:
Create a solution.
Create a C# class library called Common, containing one file named Handler.cs:
using System;
namespace Common
{
public delegate void Handler(object sender, EventArgs args);
}
Create a WPF user control library project called MyControlLibrary, referencing Common. In it, create a user control called MyControl.xaml.
MyControl.xaml:
<UserControl x:Class="ControlNamespace.MyControl"
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>
MyControl.xaml.cs:
using System.Windows.Controls;
using Common;
namespace ControlNamespace
{
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
public event Handler MyEvent;
}
}
Create a WPF Application project called MyWpfApplication, referencing Common and MyControlLibrary. In it, create WindowNamespace.Common.cs as well as a window called MyWindow.xaml.
WindowNamespace.Common.cs:
namespace WindowNamespace.Common
{
}
MyWindow.xaml:
<Window x:Class="WindowNamespace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:ControlNamespace;assembly=WpfControlLibrary1"
Title="MyWindow" Height="300" Width="300">
<Grid>
<c:MyControl MyEvent="MyControl_MyEvent" />
</Grid>
</Window>
MyWindow.xaml.cs:
using System;
using System.Windows;
namespace WindowNamespace
{
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
}
void MyControl_MyEvent(object sender, EventArgs args)
{
}
}
}
Build the solution.
You should receive the following error, pointing to line 7 of MyWindow.xaml:
The type or namespace name 'Handler' does not exist in the namespace
'WindowNamespace.Common' (are you missing an assembly reference?)
If you open the .g.i.cs file generated for MyWindow.xaml, you should see the following in the IComponentConnector.Connect method:
#line 7 "..\..\MyWindow.xaml"
((ControlNamespace.MyControl)(target)).MyEvent += new Common.Handler(this.MyControl_MyEvent);
The source of the issue is that it is trying to find Common.Handler in WindowNamespace. This could be resolved by having it generated as:
#line 7 "..\..\MyWindow.xaml"
((ControlNamespace.MyControl)(target)).MyEvent += new global::Common.Handler(this.MyControl_MyEvent);
Or by adding a using to the top of the file:
using Common;
...
#line 7 "..\..\MyWindow.xaml"
((ControlNamespace.MyControl)(target)).MyEvent += new Handler(this.MyControl_MyEvent);
Note that if all these source files are bundled into a single project, the error goes away, because the .g.i.cs file is generated differently (i.e. it doesn't explicitly add the handler to the event).
Is this actually a bug in the XAML->.NET translation, or is there something I am doing wrong?
There's a clue as to what's happening if you redeclare the Handler delegate in your WindowNamespace.Common namespace declaration in MyWpfApplication and try to compile:
Error 1 Cannot implicitly convert type 'WindowNamespace.Common.Handler' to 'Common.Handler' c:\Dev\YourSolution\MyWpfApplication\MyWindow.xaml 7 63 MyWpfApplication
The Common project is declaring a global namespace called "Common", which your control library is also using. But when your application explicitly declares "WindowNamespace.Common" it creates a local namespace which just so happens to be the same namespace that the parent control resides in. This effectively creates the ambiguity problem described in the Visual Studio documentation for Compiler Error CS0433.
Change your common namespace declaration in MyWpfApplication to just be "Common" and the problem will go away.

Error when trying to use markup extension

I have a small window that I am trying to load when my application starts. Here is the (loose) XAML:
<ctrl:MainWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:Controls;assembly=Controls">
<Grid>
<ctrl:ConnectionStatusIndicator/>
<TextBlock Grid.Row="2" Text="{Resx ResxName=MyApp.MainDialog, Key=MyLabel}"/>
</Grid>
</ctrl:MainWindow>
Notice the custom control called ConnectionStatusIndicator. The code for it is:
using System.Windows;
using System.Windows.Controls;
namespace Controls
{
public class ConnectionStatusIndicator : Control
{
public ConnectionStatusIndicator()
{
}
static ConnectionStatusIndicator()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ConnectionStatusIndicator),
new FrameworkPropertyMetadata(typeof(ConnectionStatusIndicator)));
IsConnectedProperty = DependencyProperty.Register("IsConnected", typeof(bool), typeof(ConnectionStatusIndicator), new FrameworkPropertyMetadata(false));
}
public bool IsConnected
{
set { SetValue(IsConnectedProperty, value); }
get { return (bool)GetValue(IsConnectedProperty); }
}
private static DependencyProperty IsConnectedProperty;
}
}
Now, here is where it gets weird (to me, at least). With the XAML as it appears above, my application will build and run just fine. However, if I remove the following line:
<ctrl:ConnectionStatusIndicator/>
or event move it one line down, I get the following error:
Additional information: 'Cannot create unknown type
'{http://schemas.microsoft.com/winfx/2006/xaml/presentation}Resx'.'
Line number '13' and line position '33'.
What is really strange to me is that, if I replace ConnectionStatusIndicator with another custom control from the same assembly, I get the error. The other custom control is very similar, but has a few more properties.
Can anyone explain what is going on here?
The Resx markup extension belongs to the Infralution.Localization.Wpf namespace but also does something a bit hackish and attempts to register itself to the http://schemas.microsoft.com/winfx/2006/xaml/presentation xml namespace to allow developers to use it as {Resx ...} instead of having to declare the namespace in XAML and use the extension with a prefix {resxNs:Resx ...}.
I believe that if you clean up your solution and possible delete your *.sou file the project will build as expected, but a sure way to solve this will be to add an xmlns declaration for Infralution.Localization.Wpf and use the extension with the xmlns prefix:
<ctrl:MainWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:Controls;assembly=Controls"
xmlns:loc="clr-namespace:Infralution.Localization.Wpf;assembly=Infralution.Localization.Wpf">
<Grid>
<ctrl:ConnectionStatusIndicator/>
<TextBlock Grid.Row="2" Text="{loc:Resx ResxName=MyApp.MainDialog, Key=MyLabel}"/>
</Grid>
</ctrl:MainWindow>
Also, for anyone interested, the "hack" is in these lines in the localization library:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Infralution.Localization.Wpf")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2007/xaml/presentation", "Infralution.Localization.Wpf")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2008/xaml/presentation", "Infralution.Localization.Wpf")]

Categories

Resources