VS2013 : can't locate custom control resource on xaml page - wpf

I just installed vs2013 and I carried a WPF project, previous developed with VS2010, under this environment. Happens to me a very strange thing that obviously does not happen with 2010 .... I have my own custom control that defines a runtime URI to a dictionary ... the strangeness lies in the fact that in the code of xaml page, when i define the use of my control, i've got the error reported in subject, but if i start the dubug, everything works fine and the style is correctly applied .... someone got an idea?
the code look like that :
the custom control
using System;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
using System.Windows.Data;
namespace myListView
{
public class myListView : ListView
{
// ... some dependency property
protected override void OnInitialized(EventArgs e)
{
Uri uri = new Uri("Skins\\myDictionary.xaml", UriKind.Relative);
dictionary = Application.LoadComponent(uri) as ResourceDictionary;
//... rest of code
base.OnInitialized(e);
}
}
}
the xaml page
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xlw="clr-namespace:myListView;assembly=myListView"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xlw:myListView>
<!-- the editor tell me that can't find the Skins/myDictionary.xaml -->
</xlw:myListView>
</Grid>
</Window>

Related

2wpf .net5 error The name 'InitializeComponent' does not exist in the current context

I create a new project in Visual Studio 2019 and select "WPF Application" .net5 and tries to build the generated application, I get the tow errors
The name 'InitializeComponent' does not exist in the current context.
Program does not contain a static 'Main' method suitable for an entry point.
I see all similar questions and try all solutions
What is wrong?
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
I created an empty Solution and archived it on Google Disk Empty Application
Download it and try to open it in Visual Studio.
If it opens, it means that you either have the wrong type of project, or there is an error in the templates for creating the Solution.
If it does not open, then there is a problem with the Studio itself.
Report the result in order to understand in which direction to look for a solution to your problem.

KeyNotFoundException during create region in Shell.xaml

I just started with PRISM, and encounter a exception I can't resolve.
<Window x:Class="Workplace.Shell"
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:prism="http://www.codeplex.com/prism"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid Name="Header" prism:RegionManager.RegionName="Header">
</Grid>
</Grid>
</Window>
.
using Autofac;
using Prism.Autofac;
using Prism.Modularity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Workplace
{
class Bootstrapper : AutofacBootstrapper
{
protected override DependencyObject CreateShell()
{
return new Shell();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window) this.Shell;
Application.Current.MainWindow.Show();
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
}
}
}
After run I get exception:
KeyNotFoundException: The IRegionAdapter for the type
System.Windows.Controls.Grid is not registered in the region adapter
mappings. You can register an IRegionAdapter for this control by
overriding the ConfigureRegionAdapterMappings method in the
bootstrapper.
Okey but AutofacBootstrapper class doesn't have any metod named ConfigureRegionAdapterMappings to override.
First I think something is wrong with AutofacBootstrapper, but even if I change it to UnityBootstrapper problem still exist. However second one allow me to override ConfigureRegionAdapterMappings
A Grid isn't a useful host for a region. Try using a ContentControl instead.
Of course, you can create and register a custom region adapter if you absolutely want to use the Grid, but I cannot really see any benefit.

WPF Basics: namespaces

I have been scratching my head for a bit and I'm sure this is a obvious one but I just can't see it.
I made a new application "WpfApplication3"
I add a class "Person.cs":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication3
{
public class Person
{
public string Name { get { return "Jonas"; } }
}
}
And then I try this:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<local:Person x:Key="somePerson"/>
</Grid.Resources>
<!--<TextBlock Text="{Binding {StaticResource somePerson.Name}}"/>-->
</Grid>
</Window>
And I get the error "The name "Person" does not exist in the namespace "clr-namespace:WpfApplication".
What am I doing wrong?
Using VS Express 2012 for windows desktop (trial).
This seems strange but I tested and sure enough it would not find Person.
Then I added a public ctor to Person
public class Person
{
public Person() { }
}
And it would compile
Now I can remove the ctor and it will (still) compile
Try (multiple) clean and rebuild
I know makes no sense but it happened to me

Silverlight: Does UserControl Have a Background Property?

I created a BarMenuItem UserControl in a Silverlight class library, and try to used in my main Silverlight application.
BarMenuItem.xaml:
<UserControl x:Class="ButtonControlLibrary.BarMenuItem"
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"
d:DesignHeight="300" d:DesignWidth="400">
</UserControl>
BarMenuItem.xmal.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ButtonControlLibrary
{
public partial class BarMenuItem : UserControl
{
public BarMenuItem()
{
InitializeComponent();
}
}
}
So in the MainPage, I have xmlns:blib="clr-namespace:ButtonControlLibrary;assembly=ButtonControlLibrary" defined
Tried to use BarMenuItem:
<blib:BarMenuItem Width="100" Height="150" Background="Red"/>
Compile and run it, and I expected to see a Red background, but I see nothing.
What happenend?
I'm so confused. I googled a lot, finding there is a workaround, but very ugly: Put a Grid container inside the UserControl, and then binding its Background property to the UserControl's Background:
Background="{Binding Background, ElementName=guiUserControl}"
But this is not the solution. Please advice.
In Adobe Flex, it's so natural to change a UserControl's background like what I do in the above code.
Am I missing anything?
Thanks.
The property is there but it doesn't seem to work in Silverlight. Your next best bet is the solution you said you already know. Binding layoutroot's background with usercontrol's background.

Designer rejecting DataTemplate.DataType

I am try to fit some WPF into my current Windows Forms application. When I use this simple user control, the designer for that control does not reload.
This only happens in this application. If I make a clean Windows Forms project, add these files, the designer works fine.
I have tried a reload of Visual Studio, and cleans / rebuilds of the application.
Any ideas? (These are for the items in a ListBox, so x:Key is not an option.)
P.S. How do I get rid of all those trailing blank lines in my code listing?
DETAILS:
MyClasses.cs
namespace MyNamespace {
internal class MyClassInternal {}
public class MyClassPublic {}
}
MyUserControl.xaml
<UserControl x:Class="MyNamespace.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
Height="300" Width="300">
<UserControl.Resources>
<DataTemplate DataType="{x:Type local:MyClassInternal}"/> <!--OK-->
<ObjectDataProvider x:Key="ClassPublicKey" ObjectType="{x:Type local:MyClassPublic}"/> <!--OK-->
<!-- Type reference cannot find public type named 'MyClassPublic' -->
<DataTemplate DataType="{x:Type local:MyClassPublic}"/> <!--FAILS-->
</UserControl.Resources>
<TextBlock>Hello World</TextBlock>
</UserControl>
MyUserControl.xaml.cs
using System.Windows.Controls;
namespace MyNamespace {
public partial class MyUserControl :UserControl {
public MyUserControl() {
InitializeComponent();
}
}
}
It was caused by having a space in the Assembly name.

Resources