Namespace prefix src not found [duplicate] - c#

This question already has an answer here:
the namespace prefix "utilities" is not defined
(1 answer)
Closed 3 years ago.
While entering some XAML code found at https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.combobox?view=netframework-4.8 the src: throws an error. Does anyone know why or is there a work-around?
<Grid.Resources>
<src:IEvents x:Key="myIEvents"/>
</Grid.Resources>
Visual Studio Error: XDG0006 The namespace prefix "src" is not defined.
Note: I modified the example replacing StackPanel with Grid.
This is the updated XAML code snippet:
<Window x:Class="Task_Logger.Window1"
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:src="clr-namespace:Task_Logger"
mc:Ignorable="d"
Title="Idle Time Category" Height="246" Width="376">
<Grid>
<Grid.Resources>
<src:IEvents x:Key="myIEvents"/>
</Grid.Resources>
<ComboBox ItemsSource="{StaticResource myIEvents}"
HorizontalAlignment="Left" Height="37" Margin="31,20,0,0"
VerticalAlignment="Top" Width="311" Name="comboBox1" />
<TextBlock Text="{Binding ElementName=comboBox1, Path=SelectedItem}"/>
This is the class in the .cs file
namespace Task_Logger
class iEvents : ObservableCollection<string>
{
public iEvents()
{
Add("Clog -­ Resin Pot Valves");
Add("Clog -­ MVP Valves");
.
.

IEvents seems to be a custom type. You need to import it at the top of your page. src: is the placeholder, or, the local defined namespace alias.
It most likely you need something like this at the top of your page:
<Page x:Class="Project.Pages.Page"
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"
<!-- here is your src definition -->
xmlns:src="clr-namespace:Something.Namespace.WhereIEventIsDefined"/>

Related

Use user control name without namespace prefix

Lets say I have a user control such as this:
<UserControl
x:Class="WPFUserControl.MyUserControl"
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">
<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtName" />
<Button
x:Name="btnClickMe"
Content="Greet Me" />
</StackPanel>
</UserControl>
and a Window like this:
<Window
x:Class="XAMLUserControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:control="clr-namespace:WPFUserControl"
Title="MainWindow" Height="600" Width="800">
<Grid>
<control:MyUserControl/>
</Grid>
</Window>
Is there a way I can refer to the user control without using the control: prefix?
So, instead of writing
<control:MyUserControl/>
I would use
<MyUserControl/>
You can use the XmlnsDefinitionAttribute to make the namespace containing MyUserControl "belong" to the same xml namespace (something like this : http://schemas.microsoft.com/winfx/2006/xaml/presentation, basically the value assigned to xmlns itself in your UserControl's xaml) as all native wpf components. That should let you use your UserControl without having to use a namespace prefix.
Usage for your example would be in AssemblyInfo.cs, doing [assembly:XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "WPFUserControl")].
The first paramater is the xml namespace that will be used and the second parameter is the c# namespace that contains the things you want to use without prefix in xaml.
You can find more info about how XAML and C# namespaces work together here

Merge xaml files using resource dictionaries

The information on merging two XAML files is kind of confusing, could someone point me in the right direction? (W10, VS2017, UAP, Midi)
I have app.xaml, MainPage.xaml, and A.xaml. I also have an xaml resource dictionary. I want to insert A.xaml into MainPage.xaml.
(A is actually called MidiWrapper.xaml)
app.xaml
<Application
x:Class="Kawai_ES110_Midi.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
Following How can i combine multiple XAML files using C# in WPF? I get access violation when I put first solution into app.xaml
MainPage.xaml
<Page
x:Class="Kawai_ES110_Midi.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kawai_ES110_Midi"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="XAMLWrapper.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
</Grid>
</Page>
A.xaml (aka MidiWrapper.xaml)
<ResourceDictionary
x:Class="Kawai_ES110_Midi.MidiWrapper"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kawai_ES110_Midi"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListBox x:Name="midiInPortListBox"/>
<ListBox x:Name="midiOutPortListBox"/>
</ResourceDictionary>
Resource Dictionary xaml aka (XAMLWrapper.xaml)
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
Thanks for your help in advance. Maybe app.xaml needs reference the resource dictionary and/or put "merged" tags into MainPage and A. Or perhaps MainPage and A have to also be/act like dictionaries?
I've updated my code, but I don't see the listboxes showing up still
I'm not sure that what you are trying to do is possible using a resource dictionary. A resource dictionary is exactly that, a list of things that are resources for your XAML so you might define styles in there or templates, etc. These are then referenced by your XAML that uses the resource. I've never tried to define elements in one but I don't think you can initialise a grid with it (which is what I think you are trying to do?).
Edit:
Re-reading your original post in combination with the comment i don't think you want resource dictionaries for this at all. What you want is a separate user control. You can find information on this here but as a short example i created a WPF project and added 2 user controls, 1 that contains a list and one that contains a button.
Mainwindow.xaml
<Window x:Class="test.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:test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<local:ListUserControl Grid.Column="0" Grid.Row="0"/>
<local:ButtonUserControl Grid.Column="0" Grid.Row="1"/>
</Grid>
</Window>
ListUserControl.xaml
<UserControl x:Class="test.ListUserControl"
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:local="clr-namespace:test"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Background="White">
<StackPanel>
<ListBox>
<ListBoxItem>ListBox Item #1</ListBoxItem>
<ListBoxItem>ListBox Item #2</ListBoxItem>
<ListBoxItem>ListBox Item #3</ListBoxItem>
</ListBox>
</StackPanel>
</UserControl>
ButtonUserControl.xaml
<UserControl x:Class="test.ButtonUserControl"
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:local="clr-namespace:test"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Background="DarkRed">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,10,10" VerticalAlignment="Top" Width="75"/>
</Grid>
</UserControl>
This achieves the separation between the elements that i think you are trying to achieve. So this should allow you to compose a view of several user controls where each control is a collection of controls (potentially other user controls but this can get messy). This could also be achieved using data templates as described here where you can see the content is being changed based on which template is applicable. You could define the templates in your resource dictionary and reference them that way. I think this could also be worth referencing as whilst not directly related to your question it outlines the various ways you can segment content within your application and has some relevant discussion.

Trying to set root element to SynchronousMode but says does not exist in xaml namespace

I am trying to load a loose xaml using LoadAsync method
but in xaml file when setting the root element to x:SynchronousMode="Async"
it complains. It says SynchronousMode does not exist in xaml namespace
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" x:SynchronousMode="Async" >
<Grid>
<Button x:Name="okButton" x:FieldModifier="public" >OK</Button>
</Grid>
</Window>

XAML: The property "Content" can only be set once - though already only set once

I'm trying to set up a material design in a C# XMAL WPF application.
I added "Material Design In XAML" to my project, after which I got the following error: The property 'Content' can only be set once where would be the problem in my mark up? I've provided the file which gives the error below:
<Window x:Class="e621_fetch.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:e621_fetch"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="13"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="{DynamicResource MaterialDesignFont}">
<Grid>
<materialDesign:Card Padding="32" Margin="16"> //error on this line
      
<TextBlock Style="{DynamicResource MaterialDesignTitleTextBlock}">My First Material Design App</TextBlock> //error on this line
    
</materialDesign:Card> //error on this line
</Grid>
already fixed it, it was because of the blank lines in between the tags...

User Controls and XML Namespaces

When using xml namespaces (xmlns:) in WPF, is there a different way you need to specify a namespace? I was trying to implement a User Control, but it kept giving me the following error: "The name '' does not exist in the namespace 'clr-namespace:myNamespace' ."
Any tips concerning xml namespaces (especially when trying to access classes) would be greatly appreciated.
UPDATE:
I keep getting the following error when attempting to add a custom user control to my main window: "The name 'customControl' does not exist in the namespace 'clr-namespace:MovableDataGridRows_test.userControls'. "
Below is the XAML code to my main window:
<Window x:Class="MovableDataGridRows_test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls_c="clr-namespace:MovableDataGridRows_test.userControls"
Title="MainWindow" Height="350" Width="525">
<Controls_c:customControl width="300"/>
</Window>
Below is the XAML code for my user control that I wish to include in my main window(NOTE: The user control is in a folder in the project named, "userControl". The project name is, "MovableDataGridRows_test"):
<UserControl x:Class="MovableDataGridRows_test.userControls.UserControl1"
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>
<Button Margin="223,0,0,263">Submit</Button>
<TextBox HorizontalAlignment="Left" Height="37" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="223"/>
</Grid>
</UserControl>

Categories

Resources