Cannot Define Object in XAML - c#

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 ... >

Related

WPF: Problem with display xaml view inherits from UserControl

Hello i have this Viw in XAML
<local:JedenViewBase x:Class="Firma.View.FakturaView"
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:local="clr-namespace:Firma.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<ResourceDictionary Source="MainWindowResource.xaml" />
</UserControl.Resources>
<Grid>
.....
</Grid>
</local:JedenViewBase>
And that is class this view
namespace Firma.View
{
public partial class FakturaView : JedenViewBase
{
public FakturaView()
{
InitializeComponent();
}
}
}
And that is JedenViewBase class
namespace Firma.View
{
public class JedenViewBase : UserControl
{
static JedenViewBase()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(JedenViewBase), new FrameworkPropertyMetadata(typeof(JedenViewBase)));
}
}
}
I have problem because view in XAML dont display, i dont know why? JedenViewBase class inherits from UserControl. When i UserControl in view everything works. What i should do?
<UserControl x:Class="Firma.View.FakturaView"
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:local="clr-namespace:Firma.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<ResourceDictionary Source="MainWindowResource.xaml" />
</UserControl.Resources>
<Grid>
...
</Grid>
</UserControl>
View FakutraView when i use UserControl
I try rebuild app etc. and i still have problem
You've created a custom control.
That's unlikely to be a good idea and this should probably just be a user control.
The reason you get no view is this.
static JedenViewBase()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(JedenViewBase), new FrameworkPropertyMetadata(typeof(JedenViewBase)));
}
I recommend you remove that. Change
<local:JedenViewBase
To user control.
Make this just a user control.
Alternatively. Read up on custom controls. Put your ui definition in generic xaml.
I also wonder why this has it's own resources. They will be in memory for each instance. If whatever is in that resource dictionary is unique to this control maybe that's not a bad idea. in which case the naming seems strange.

MarkupExtension derived class name: Using a partial name to reference it later is valid. What's the point?

Using VS 2019. I'm surprised the name of a markup extension can be abbreviated as visible in this code which compiles and runs correctly.
In assembly CustMarkExt, I define the markup extension type as MyExtension:
using System.Windows.Markup;
namespace CustMarkExt {
public class MyExtension : MarkupExtension {
public override object ProvideValue (IServiceProvider serviceProvider) {
return "42/42";
}
}
}
In assembly TestMyExtension I refer to it as My (instead of expected MyExtension):
<Window x:Class="TestMyExtension.MainWindow"
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:local="clr-namespace:TestMyExtension"
xmlns:myex="clr-namespace:CustMarkExt;assembly=CustMarkExt"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{myex:My}" />
</Grid>
</Window>
To run the code two references are to be added:
add reference to system.xaml in the extension assembly
add reference to the extension in the other assembly
So what doesn't that mean? It doesn't seems to be a bug.

Enforce global namespace in XAML auto generated files

My problem is distinct from this other question Enforce Global Namespace in XAML in that I don't have a class with the same name as the enclosing namespace, yet I couldn't think of a more appropriate title that would differentiate the two.
If any part of my XAML object's namespace is also the root of another namespace, Visual Studio seems to prefer my XAML object's namespace, which manifests as a compiler error.
Here is an example that shows how part of my namespace WpfApp1.System.Views clashes with global::System:
<Window x:Class="WpfApp1.System.Views.MainWindow"
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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!-- Amazing content goes here -->
</Grid>
</Window>
There are compilation errors shown in the Errors pane and obj/System/Views/MainWindow.g.i.cs shows this with a squiggly red underline under the System part of System.Windows.Window:
namespace WpfApp1.System.Views {
public partial class MainWindow : System.Windows.Window {
// ...
}
}
Obviously my toy example only stops me from having a System namespace anywhere in my hierarchy, but it also stops me from having any 3rd-party names (like DevExpress) if I'm using one of those 3rd-party components in XAML.
Without renaming either my own namespace (or the referenced components' namespaces) can I ask Visual Studio to use global:: namespaces in its generated XAML code?

XAML can't see a class

My xaml goes like this:
<DataGrid.Resources>
<Helpers:EnumHelper x:Key="EnumHelper" />
</DataGrid.Resources>
And my class is defined like this:
namespace MyProj.View.UserControlHelpers {
public class EnumHelper { }
}
The error I get is this:
The name "EnumHelper" does not exist in the namespace "clr-namespace:MyProj.View.UserControlHelpers".
I've cleaned this project from the studio, then I've deleted every file in bin and obj subdirectories, I've even changed class name, still without success - I'm still getting this error.
EDIT:
My xaml headers are lik this:
<UserControl x:Class="MyProj.View.Partials.TableWindow.Columns"
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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
xmlns:Helpers="clr-namespace:MyProj.View.UserControlHelpers"
xmlns:Model="clr-namespace:MyProj.Model"
xmlns:ModelHelpers="clr-namespace:MyProj.Model.Helpers"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
Your class namespace is
MadMin.View.UserControlHelpers
but you refer to it in xaml as
MyProj.View.UserControlHelpers

WPF assembly reference missing

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,

Categories

Resources