How to use Microsoft converters - c#

I have found reach set of converters in Microsoft.TeamFoundation.Controls.WPF.Converters, but i don't know how to use them in xaml. Particularly, i don't how to include this namespace into xaml. Maybe it is only allowed to use it in code?
List of converters
I tried
xmlns:conv="clr-namespace:Microsoft.TeamFoundation.Controls.WPF.Converters;assembly=Microsoft.TeamFoundation.Controls"
and then
<conv:NullToVisibleConverter x:Key="Null2VisConv"/>
but it can't find NullToVisibleConverter in conv.

First you need to target .NET 4.5, then add reference to Microsoft.TeamFoundation.Controls (should be in Assembilies -> Extensions) and then in XAML, as you did
<Window ...
xmlns:conv="clr-namespace:Microsoft.TeamFoundation.Controls.WPF.Converters;assembly=Microsoft.TeamFoundation.Controls">
<Window.Resources>
<conv:NullToVisibleConverter x:Key="Null2VisConv"/>
</Window.Resources>
<!-- ... -->
</Window>

Please make sure that have these pkgs installed.
You must add reference to dll in your project.
MSDN says:
You can find the assemblies in the client object model in Program Files\Microsoft Visual Studio 11.0\Common7\IDE under ReferenceAssemblies\v2.0, ReferenceAssemblies\v4.5, and PrivateAssemblies.
After thar you write in your xaml for example:
xmlns:converters="namespace for converters"
you can choose namespace for converters from the list of namespace you have on your pc

Related

"The name does not exist in the namespace error" in XAML

I know this is a recurring error but I can't seem to get around it.
Intellisense does recognize the name of my custom control and suggests to add the proper using: directives, but not only XAML designer doesn't find the name of the control but I can't get through compilation either.
The custom control is a public class defined as
namespace MyApp.CustomControls
{
public class CustomTextBox : TexBox
{
...
}
}
And in my MainPage.xaml
<Page ...
xmlns:customControls="using:MyApp.CustomControls">
...
<customControls:CustomTextBox/>
...
</Page>
This does not render in design nor compile.
This answer and the ones below are not working for me.
The error message:
Error XDG0008 The name "CustomTextBox" does not exist in the namespace "using:MyApp.CustomControls".
Your code should works well after you build the project, and it works well in my side using your above code. Try to clean your solution or delete the bin and obj folders in your project then rebuild your app again. Also try to restart your Visual Studio. If it still happens, you can provide a reproducible sample to help me look into this issue.
I've seen quite a lot solutions saying that you should rebuild the project, restart Visual Studio or restart the machine.
What worked for me was specifying the assembly in the namespace reference, that is:
xmlns:the_namespace="clr-namespace:the_namespace" - produces the above error.
xmlns:the_namespace="clr-namespace:the_namespace;assembly=the_assembly" - works well.
I got a version of this error in my embedded UserControl when I tried to use the Name property in my XAML instead of using x:Name. In other words, when my XAML code looked like this:
myUserControls="using:MyUserControls"
<myUserControls:GraphCanvas Name="GraphCanvas" />
I got an error that 'The name "GraphCanvas" does not exist in the namespace "using:MyUserControls"'. When I changed one line of code to this:
<myUserControls:GraphCanvas x:Name="GraphCanvas" />
Everything built just fine.
I'm dropping this solution here because it took me about a day and a half to figure out this problem and this was the only stackoverflow page I found when I searched the error string. Hopefully I will save someone else the hassle I went through.

Using Custom Resource in DevExpress WPF Theme

Objective:
I have a WPF project which shall be themed using DevExpress Themes.
There is a Login-UserControl that shall have a themable background image.
Implementation
I made a custom Theme. In that theme I have a Folder "CustomResources" in which there is an Image, let's call it "Background.png" and a "Brushes.xaml" that defines an ImageBrush like this:
<ResourceDictionary ...>
<ImageBrush x:Key="{CustomThemeKeyAssembly:CustomThemeResourcesThemeKey ResourceKey=LoginBackgroundImageBrush, ThemeName=CustomTheme}" ImageSource="Background.png" />
</ResourceDictionary>
Accordingly, I have a shared Assembly CustomThemeKeyAssembly that derives a Custom ResourceThemeKey.
In the Project, I register and set the Theme using ApplicationThemeHelper
var theme = new Theme("CustomTheme")
{
AssemblyName = "DevExpress.Xpf.Themes.CustomTheme.v17.2"
};
Theme.RegisterTheme(theme);
ApplicationThemeHelper.ApplicationThemeName = "CustomTheme";
and I reference the Resource through
Background="{dxci:ThemeResource ThemeKey={CustomThemeKeyAssembly:CustomThemeResourcesThemeKey ResourceKey=LoginBackgroundImageBrush}}"
As advised by DevExpress Knowledgebase / Support.
Problem
The Resource is only found and displayed, if I add a Merged Resource Dictionary like this:
ResourceDictionary loginBackgroundDictionary = new ResourceDictionary
{
Source = new Uri($"pack://application:,,,/{MyProject.Properties.Settings.Default.ThemeAssembly};Component/CustomResources/Brushes.xaml", UriKind.Absolute)
};
//Add LoginBackgroundImageBrush Dictionary
Resources.MergedDictionaries.Add(loginBackgroundDictionary);
No article or example mentions having to do this, though. So my impression is that I either am doing something wrong or I am missing some simple step like merging the Brushes.xaml into some ResourceDictionary.
Without that snippet I get a warning that the resource could not be found.
Question
Has anybody an idea where I am going wrong or what I am missing to get this working without that last snippet?
FYI: I am using DevExpress 17.2.3 and the ResourceKey Assembly is targeted to .net Framework 4.0
EDIT
Meanwhile, I tried adding the Brushes.xaml to Themes/Generic.xaml in the theme assembly like this:
<ResourceDictionary.MergedDictionaries>
<dxt:ResourceDictionaryEx Source="/DevExpress.Xpf.Themes.Office2016WhiteSE.v17.2;component/Themes/ControlStyles.xaml" />
<dxt:ResourceDictionaryEx Source="/DevExpress.Xpf.Themes.Office2016WhiteSE.v17.2;component/CustomResources/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
It didn't make any difference. Same behavior as before.
Problem solved!
The problem was in the CustomThemeKeyAssembly
The wrong implementation was
public class CustomThemeResourcesThemeKey : ThemeKeyExtensionBase
{
public override Assembly Assembly => TypeInTargetAssembly != null ? TypeInTargetAssembly.Assembly : GetType().Assembly;
}
The working implementation is
public class CustomThemeResourcesThemeKey : ThemeKeyExtensionBase<ThemeResourcesThemeKeys> { }
The breaking difference is the override of the Assembly property. The default implementation makes it work. I did that because it was done so in an example. Support told me to stick with the default implementation and it worked.

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.

The tag 'ViewModelLocator' does not exist in XML namespace clr-namespace:XXX

I have tried numerous other solutions without any success. I have a class called ViewModelLocator which is located in my portable class library. It has a property in it called ViewModels, which is of type Dictionay<K, V>
Then I have a Windows Phone 8 project that references the portable class library. I added the following to the WP8 app.xaml:
<Application
x:Class="Kaizen.WP8.Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:test="clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable">
<Application.Resources>
<test:ViewModelLocator x:Key="ViewModelLocator">
<test:ViewModelLocator.ViewModels>
<test:SampleViewModel x:Key="sampleVM"/>
</test:ViewModelLocator.ViewModels>
</test:ViewModelLocator>
</Application.Resources>
</Application>
When I press F12 on the tags, it navigates to the correct class and or property in my pcl. Which indicates that VS knows about the objects, but when I try and build, I receive the following error:
The tag 'ViewModelLocator' does not exist in XML namespace
'clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable'.
The tag 'SampleViewModel' does not exist in XML namespace
'clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable'.
Could anyone please provide some assistance?
[Update]
I reference the pcl version of mvvm light in my pcl project. This is how the ViewModelLocator class looks like:
public class ViewModelLocator
{
public dynamic this[string viewModelName]
{
get
{
if (this.ViewModels.ContainsKey(viewModelName))
{
return this.ViewModels[viewModelName];
}
else
{
return null;
}
}
}
public Dictionary<string, ViewModelBase> ViewModels { get; set; }
public ViewModelLocator()
{
this.ViewModels = new Dictionary<string, ViewModelBase>();
}
}
My WP8 project also makes use of the mvvm light pcl assemblies. I noticed that, if I make use of the ViewModelBase class as the dictionary value, that when I get the errors. It's as there's an issue using the mvvm light pcl between the two projects?!
[Update]
Many thanks in advance!!
Kind regards,
I just had this problem with a .Net 4.5 project.
The solution for me was to change to .Net 4.0, ignore the warnings, and change back to .Net 4.5.
Then the problem was gone.
Don't know if it is a feasible way for others, but it worked for me.
Best regards.
Okay, so I'm not exactly sure what I did wrong in my first attempt, but I recreated the solution and performed more or less the same steps and I didn't receive the error again?! o_O
I know this is a bit late but I had the same problem with a WPF Desktop app and a control library. The library's default Target Framework was .Net 4 but the Desktop app just after I created in in Visual Studio was by default created with .Net 4 client profile. I changed the Desktop app from .Net 4 client profile to .Net 4 and it worked.

View doesn't find ViewModel in different Assembly

I am starting a new project and oriented my projectstructure on the structure recommended in this question.
Now I am seeing strange behaviour. When I am setting the datacontext in the View-XAML, it isn't found at runtime (getting a XamlParseException). When I set it in the constructor in the codebehind-file, everything is working just fine.
Is this official (documented) behaviour when using different assemblies, or am I doing something wrong?
The code:
Not Working:
MainView.xaml:
<UserControl x:Class="ViewsRoot.Views.MainView"
xmlns:baseControls="clr-namespace:BaseControls;assembly=BaseControls"
xmlns:viewModels="clr-namespace:ViewModelsRoot;assembly=ViewModelsRoot">
<UserControl.DataContext>
<viewModels:ShellViewModel />
</UserControl.DataContext>
MainView.xaml.cs
public MainView()
{
InitializeComponent();
// No DataContext set in codebehind-file
}
Working:
MainView.xaml:
<UserControl x:Class="ViewsRoot.Views.MainView"
xmlns:baseControls="clr-namespace:BaseControls;assembly=BaseControls"
xmlns:viewModels="clr-namespace:ViewModelsRoot;assembly=ViewModelsRoot">
<!--<UserControl.DataContext>
<viewModels:ShellViewModel />
</UserControl.DataContext> -->
MainView.xaml.cs:
public MainView()
{
InitializeComponent();
DataContext = new ViewModelsRoot.ShellViewModel();
}
Update:
The Exception-Text is:
{"The file or assembly \" ViewModelsRoot, PublicKeyToken = null \ "or one of its dependencies was not found. The system can not find the file specified."}
And the only inner Exception I can see is a System.IO.FileNotFoundException.
Update 2:
Thanks for the comments, but I haven't forgotten a namespace. I shortened it here for showing the code, but I double- and triplechecked (again). The DataContexts namespace is also filled in by intellisense. The whole <viewModels:ShellViewModel /> is written by intelli-sense. So it is found at designtime... ...so any more ideas?
Update 3:
The xaml is "correctly" parsed as I am able to bind the DataContext to a class in the same assembly.
I have reproduced this error using a three project solution, with the specified dependencies between them:
StartupProject → ViewsRoot
ViewsRoot → ViewModelsRoot
ViewModelsRoot
"StartupProject" has "exe" output type, while the other two have "dll".
In my case, I solved the problem by adding "ViewModelsRoot" to the References list of "StartupProject". It is not a coding problem, but rather a runtime problem, because "ViewModelsRoot.dll" is not copied to "StartupProject" output folder.
When you specify the DataContext in code-behind, Visual Studio notices the need for that "dll" and adds it to the output after compilation. This doesn't happen when setting the DataContext from XAML. It is tricky because "ViewModelsRoot" code is used from XAML with Reflection. Adding it to References list forces Visual Studio to copy the "dll" in both cases.
You can also copy "ViewModelsRoot.dll" to the output folder directly, but it will not be updated when you change the code.
I've often found this error when the project target framework was set to "Client Profile" (this was set by default on VS2010, IIRC), if this is the case, try changing it to 3.5 or 4.0.

Categories

Resources