WPF assembly reference missing - c#

I have a class call BinaryImageConverter implement IValueConverter in Common.Tools.
How can I call it in WPF?
<Window.Resources>
<CollectionViewSource x:Key="ProductsSource"/>
<local:BinaryImageConverter x:Key="ImgConverter" />
</Window.Resources>
Thank you.

You have to add prefix definition to the Window. For example if, the namespace of BinaryImageConverter is KevNameSapce and its assembly is KevAssembly then your xmal code would looks similar to below.
<Window xmlns:local="clr-namespace:KevNameSapce;assembly=KevAssembly">
<Window.Resources>
...
<local:BinaryImageConverter x:Key="ImgConverter" />
</Window.Resources>
</Window>
You don't have to specify the assembly if the converter class is in the same project,

Related

How to get the actual object of StaticResource from codebehind without using TryFindResource?

I have used a StaticResource in XAML file. I have named the StaticResource using x:Name. Now I want to access the actual object of the StaticResource.
Here are the files:
In XAML file:
<Window x:Class="MyProject.MainWindow"
...
Title="MainWindow" Height="350" Width="525">
<Grid>
<StaticResource ResourceKey="MyButtonResource" x:Name="MyResource" />
</Grid>
</Window>
In Code-behind CS file:
Button buttonFromStaticResource = MyResource.SomeProperty as Button;
Here, I need something like SomeProperty or any method to get the actual object (in this case, it is a Button object).
Edit:
A way to get the object is to use TryFindResource:
Button buttonFromStaticResource = this.TryFindResource("MyButtonResource") as Button;
But this solution involves a string parameter. Any better solution than this so that I can use MyResource directly (by leveraging x:Name in XAML file), without using any string?
How to read static Resource in c#, Check a below code
<Window x:Class="MyProject.MainWindow"
...
Title="MainWindow" Height="350" Width="525">
<Grid>
<StaticResource ResourceKey="MyButtonResource" x:Name="MyResource" />
</Grid>
</Window>
c#
var currentResources= this.Resources["MyButtonResource"];

c# WPF AlternateContent doesn't exist in the Namespace [...]

I am facing the need to have XAML Code only in design-time. I have found a nice Solution to this, which can be found here. It seems like there are a few guys having a Problem with the parse-timing of XmlnsDefinitionAttribute which is solved here.
In my case the issue is really, that I cannot compile my code, because AlternateContent can't be found in the namespace xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006". I haven't found any documentation to this namespace indeed and it seems strange, that the line mc:Ignorable="d"doesn't fail to build, which means I have at least one assembly containing the above namespace.
This is my Code:
#if DEBUG
[assembly: XmlnsDefinition("debug-mode", "MyNamespace")]
#endif
<Window x:Class="MyNamespace.SomeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:debug="debug-mode"
mc:Ignorable="d"
... >
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<mc:AlternateContent>
<mc:Choice Requires="debug">
<ResourceDictionary Source="pack://application:,,,/Styles;component/Generic.xaml" />
</mc:Choice>
</mc:AlternateContent>
...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
...
</Window>
My guess is, that I'm missing an Assembly-Reference, but I've not yet found a Documentation specifying a containing Assembly for AlternateContent. Do you have any ideas how I might get this to work?
Edit:
It seems like this is a common VS Problem which can be solved by this mc:Ignorable="d mc". This just doesn't work in my case, because I would need this to include Resources at Design-Time, which should be available to the VS-Designer :)
It seems like this is a common VS problem which can be solved by this code
mc:Ignorable="d mc"
This must be added to the root element of the view.
Note: that you already should have mc:Ignorable="d" in your root element, so you have to just add the mc to it.

Control Does Not Exist in Namespace

I'm attempting to use the MediaUriElement in the custom (WPFMediaKit).
I've added it into the namespace like so;
xmlns:controls="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
and then I have tried to access it;
<Grid>
<controls:MediaUriElement x:Name="mediaUriElement" />
</Grid>
however I just get an error: The name MediaUriElement does not exist in this namespace. VS does give me a suggestion to use it when I am typing though;
How can I correctly add this into my project?
I've tried and it is worked. What I've done:
Switched project from Framework 4.5 to Framework 4.0
Added references such as DirectShowLib-2005.dll and WPFMediaKit.dll to the project
Declared xaml namespace xmlns:controls="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit" like that:
<Window x:Class="ExcelExportWpfApplication.MainWindow"
... The code omitted for the brevity...
xmlns:controls="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
xmlns:vm="clr-namespace:ExcelExportWpfApplication.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="525"
WindowStartupLocation="CenterScreen">
And just written in xaml:
<controls:MediaUriElement Grid.Row="2"/>
No errors

Cannot Define Object in XAML

All, I have a user control. In the XAML markup for this control I want to define a resource (instatiate an object called cellColorConverter of the class CellColorConverter which is defined in the same namespace as the control. I have
<UserControl x:Class="ResourceStudio.Resource.Resource"
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" Height="331.2" Width="340">
<UserControl.Resources>
<ResourceStudio.Resource:CellColorConverter x:Key ="cellColorConverter"/> // <- Error.
</UserControl.Resources>
This is giving a compile-time error saying
The namespace prefix ResourceStudio.Res is not defined.
What am I doing wrong here?
Thanks for your time.
Update: I now have
xmlns:local="clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio"
mc:Ignorable="d" Height="331.2" Width="340">
<UserControl.Resources>
<local:CellColorConverter x:Key ="cellColorConverter"/>
</UserControl.Resources>
The CellColorConverter class is in the name space ResourceStudio.Resource, defined as
namespace ResourceStudio.Resource
{
public class CellColorConverter : IMultiValueConverter
{
// ...
}
}
I still get the following error
The name "CellColorConverter" does not exist in the namespace "clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio". F:\Camus\ResourceStudio\ResourceStudio\ResourceStudio\Resource\Resource.xaml
In the XAML:
<ResourceStudio.Resource:CellColorConverter />
The ResourceStudio.Resource is an XML namespace for CellColorConverter.
You need to map this XML namespace to a .NET namespace:
<UserControl xmlns:ResourceStudio.Resource="clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio" ... />
The actual namespace and assembly name depend upon what you've called them in your code.
This article provides more information.
Also, you can make the namespace shorter:
xmlns:local="clr-namespace:..."
<local:CellColorConverter ... >

Provide value on 'System.Windows.StaticResourceExtension

Inside a XAML Page I'm trying to use an IValueConverter, it's throwing an error.
The IValueConverter is in another assembly, I have added a reference
There are no design-time errors
I have assigned the StaticResource with a ResourceKey
At the top of my page I have this:
xmlns:converters="clr-namespace:Converters;assembly=Converters"
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/DialogStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<converters:NoWhiteSpaceConverter x:Key="NoWhiteSpaceConverter" />
</ResourceDictionary>
</Page.Resources>
Then I try to use it later on like this:
<TextBox Text="{Binding SomeText, Converter={StaticResource NoWhiteSpaceConverter}}" />
Can anyone see what the problem is?
Make sure that the resources are defined before the usage (in Xaml parsing order). The easiest way is to place it into App.xaml
See also here for a similar issue: https://paulkiddie.com/the-importance-of-the-position-of-window-resources-element-in-wpf-xaml-markup/
In my case the resource was correctly defined before it is used but there was a wrong reference to a converter.
The problematic line was
...{Binding MyProperty, Converter={StaticResource local:MyConverter}}
and it should be without the namespace alias
...{Binding MyProperty, Converter={StaticResource MyConverter}}
where local is a namespace alias containig the converter class and MyConverter is the key defined in a resource in the XAML.

Categories

Resources