UserControl content can't be set more than once - c#

having some troubles with UserControl. I cannot get rid of "Content is set more than once error"
From what I've read, the common cause is that .. well, content is set more than once. For example having more than one child or setting content via Content=".." and then specifying another content between the tags.
However, this is UserControl generated by VisualStudio and I made no changes to the xaml.
<UserControl x:Class="TMEGadget.View.Toolbox"
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>
Got any tips?
P.S: Can anyone tell me why, when I try to type "Hey,\n\nSome text...", the "Hey,\n\n" is deleted?
Edit: Restarting VS fixed the problem , thanks #Bolu

Related

Edit Xaml files on debuging Visual studio 2019, WPF

I want to Edit UI XAML files with my program running and see these changes without to stop my program...
When I change these files right now It just o nothing until I close my program and start again.
For exmaple:
this is my xaml code:
<Window x:Class="WpfApp4.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:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
http://prntscr.com/nwiw0o
I see an empty white screen
When I change it (on debug) to
<Window x:Class="WpfApp4.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:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Background="Blue">
<Grid>
</Grid>
</Window>
http://prntscr.com/nwiw7q
Nothing is change
But when I restart my program
It runs this blue screen...
http://prntscr.com/nwiwiw
I think its something about debugging option?
this is my page open list
http://prntscr.com/nwiwtu
You probably don't have "Edit and continue" enabled, because you can just edit xaml and see the result immediately if you do.
From the Debug menu, choose Options.
On the left Debugging should be selected for you and the default will be the first option - General. If that isn't selected, do so.
Scroll the right panel down.
Tick the Enable UI debugging tools for XAML and at least Enable XAML Edit and Continue.
You should then be able to change simple properties in XAML and see your changes reflected immediately in the running UI. It does not work for everything though.
https://devblogs.microsoft.com/visualstudio/ui-development-made-easier-with-xaml-edit-continue/

WPF Can't find local resource when executed

I'm defining an application resource in my app that stores my ViewModels with a navigation element to navigate pages. This is working fine in the xaml editor of visual studio since all the data bindings are working there. However when I try to run the application in debugger it trows an Exception with the message Cannot find source with the name ViewModelLocator. Does anyone know what is going wrong?
I have a local resource defined in my App.xaml like this:
<Application.Resources>
<viewmodel:ViewModelLocator x:Key="ViewModelLocator"/>
</Application.Resources>
Which i try to use like this:
<Page x:Class="QardPrint.PageEmployeesList"
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:QardPrint"
xmlns:viewmodel="clr-namespace:QardPrint.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="PageEmployeesList"
DataContext="{Binding EmployeesListViewModel, Source={StaticResource ViewModelLocator}}">
My ViewModelLocator class looks like this
public class ViewModelLocator
{
public EmployeesListViewModel EmployeesListViewModel => new EmployeesListViewModel(App.Navigation);
}
Try to use ResourceDictionary:
App.xaml:
<Application xmlns:viewmodel="clr-namespace:QardPrint.ViewModel">
<Application.Resources>
<ResourceDictionary>
<viewmodel:ViewModelLocator x:Key="Locator" />
QardPrint.PageEmployeesLis.Xaml:
<Page x:Class="QardPrint.PageEmployeesList"
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:QardPrint"
xmlns:viewmodel="clr-namespace:QardPrint.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="PageEmployeesList"
DataContext="{Binding EmployeesListViewModel, Source={StaticResource Locator}}">
I Found the problem. It was in the App.xaml I deleted the startup parameter. After adding it again the problem was solved.
I did this because I made another window in the startup function of app.cs. But this is probably bad design anyway so going to figure out how to do it better.

Adding Icon property to xaml

I'm trying to add an icon to a wpf window, but I'm getting an xaml parsing exception whenever I add the following line to my code:
Icon="myIcon.ico"
My window tag looks like this (and runs fine) without the Icon property:
<Window x:Class="MyProject.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:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204">
If I add Icon="myIcon.ico" before the >, I get an error on the W in Width="1058.204"
Exception thrown: 'System.Windows.Markup.XamlParseException' in
PresentationFramework.dll
Additional information: 'Provide value on
'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an
exception.' Line number '8' and line position '58'.
So, the code erroring out looks like this:
<Window x:Class="MyProject.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:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204"
Icon="myIcon.ico">
I feel like I must be missing something very simple here. Based off of other posts here (How to change title bar image in WPF Window?) I feel like I'm doing it right.
Can anyone help? Thanks!
I solved this with a little bit of help from m.rogalski's suggestion and using information in here:How to reference image resources in XAML?
After importing my image to the project, I changed my code to look like this, and it worked:
<Window x:Class="MyProject.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:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204"
Icon="pack://application:,,,/my folder/myIcon.ico">
If I add Icon="myIcon.ico" before the >, I get an error on the W in Width="1058.204"
Add the myIcon.ico file to your project and set its Build Action property to Resource in the properties pane in Visual Studio.
You can then either set the Icon property of the window to a relative URI or a pack URI or you could specify the the icon as the default icon of your application under Project->Properties->Application->Icon and manifest.

VSIX CS0426 The type name Views does not exist in the type 'ABC'

I'm creating a VSIX plug-in and just added a WPF control like this:
<UserControl x:Class="ABC.Views.Configuration.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"
xmlns:local="clr-namespace:ABC.Views.Configuration"
mc:Ignorable="d"
Loaded="UserControl_Loaded"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
When I compile the project I am getting the error:
CS0426 The type name Views does not exist in the type 'ABC'
I've checked the projects' default namespace and other relevant configurations and everything looks good. In fact, I have other User Controls in the same directory that are compiling just fine.
What am I doing wrong?
The root cause of this problem was this line in XAML above:
Loaded="UserControl_Loaded"
For some reason VSIX doesn't like wiring events in XAML. I simply removed this and wired it up in Code-behind

Can't access Windows 8 Store user control from code

I'm writing a Windows 8 Store application and within that I've designed my own user control.
Here is the code for my usercontrol (This is a dummy control but the problem exists with this):
<UserControl
x:Class="Windows8StoreTest.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows8StoreTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="70"
Height="40">
<StackPanel>
<Button Content="Hello" Foreground="Pink" BorderBrush="Pink"/>
</StackPanel>
</UserControl>
I've dropped the user control onto my page and give it a name:
<Page
x:Class="Windows8StoreTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows8StoreTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:TestUserControl Name="testControl"/>
</Grid>
</Page>
However, when I go to the code behind I can't access the control by that name. It doesn't seem to exist! What is weird is that the control doesn't exists within InitializeComponent() method for the MainPage class which will be why it does exist.
What am I missing from my user control?
I'm using Windows 8 Store, XAML, c#.
Thanks in advance
Try to use this:
<local:TestUserControl x:Name="testControl"/>
Should work...
hello i don't know what is wrong but it should work.i have just made a sample example of it..i am putting it here hope you have done the same way.
<Page
x:Class="App12.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App12"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:MyUserControl1 x:Name="hellousercontrol" />
</Grid>
in my mainpage.cs.. i have just use it like this..
public MainPage()
{
this.InitializeComponent();
hellousercontrol.Height = 100;
}
one more this..have build your solution ?
I had the same issue in c++ environment. I observed, I didn't had default constructor in my class, as soon as I added the default constructor, I could use the defined UserControl in my project through XAML file. However without default constructor I was able to use it from within c++ code.

Categories

Resources