WPF Basics: namespaces - wpf

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

Related

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.

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

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>

Referencing objects in XAML design data

How can I reference the same object twice (or more often) in a XAML design data file?
I tried to use {x:Reference}, but this does not seem to work.
Here is an example:
The combo box in the cells of the second column of the sample's data grid displays a list of "data types". The list of available data types comes from the Types property of the main window's view model (= the data context). The list of items in the grid comes from the Items property of the view model. Each item has a Name and a Type column, where Type references a data type object.
The sample grid looks like this:
Here is the XAML design data which should show the same grid contents in the Visual Studio designer (but it doesn't):
<?xml version="1.0" encoding="utf-8" ?>
<local:MainWindowViewModel
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridSample"
>
<local:MainWindowViewModel.Types>
<local:DataType Name="String" x:Name="String"/>
<local:DataType Name="Integer" x:Name="Integer"/>
</local:MainWindowViewModel.Types>
<local:MainWindowViewModel.Items>
<local:Item Name="Lorem" Type="{x:Reference String}"/>
<local:Item Name="Ipsum" Type="{x:Reference Integer}"/>
</local:MainWindowViewModel.Items>
</local:MainWindowViewModel>
Above, I am using {x:Reference String} to obtain a reference to the object that was created by <local:DataType Name="String" x:Name="String"/>.
In the Visual Studio designer, the list is empty, and the error message "Errors found in markup: ... DesignData.xaml" is displayed. In the editor for the design data XAML files, I get the error message "Service provider is missing the INameResolver service".
Is there any alternative to {x:Reference} which I could use in design data files to refer to an object?
For completeness, here are the remaining files of my sample:
MainWindow.xaml:
<Window x:Class="DataGridSample.MainWindow"
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"
Title="Sample" Height="300" Width="400"
d:DataContext="{d:DesignData Source=DesignData.xaml}">
<Window.Resources>
<CollectionViewSource x:Key="types" Source="{Binding Types}"/>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="*"/>
<DataGridComboBoxColumn SelectedItemBinding="{Binding Type}"
ItemsSource="{Binding Source={StaticResource types}}"
DisplayMemberPath="Name"
Header="Type" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DataGridSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
readonly MainWindowViewModel _viewModel = new MainWindowViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = _viewModel;
}
}
}
MainWindowViewModel.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace DataGridSample
{
public class MainWindowViewModel
{
private readonly ObservableCollection<DataType> _dataTypes;
private readonly ObservableCollection<Item> _items;
public MainWindowViewModel()
{
DataType typeString = new DataType {Name = "String"};
DataType typeInteger = new DataType {Name = "Integer"};
_dataTypes = new ObservableCollection<DataType> {typeString, typeInteger};
_items = new ObservableCollection<Item>
{
new Item {Name = "Lorem", Type = typeString},
new Item {Name = "Ipsum", Type = typeInteger}
};
}
public ObservableCollection<DataType> Types
{
get
{
return _dataTypes;
}
}
public ObservableCollection<Item> Items
{
get
{
return _items;
}
}
}
public class DataType
{
public string Name { get; set; }
}
public class Item
{
public string Name { get; set; }
public DataType Type { get; set; }
}
}
Background on why x:Reference doesn't work .....
x:Reference is a XAML 2009 feature.
You can't use x:Reference in XAML markup that is compiled according to the MSDN docs.
http://msdn.microsoft.com/en-us/library/ee795380.aspx
It's designed for loose XAML...e.g. if you create a XAML Page (.xaml) and load it via Internet Explorer.
When you use DesignData the Designer effectively creates and compiles a new class whose shape and content is as described in your DesignData file.
There's no support for it in Visual Studio/Blend Designers.
http://www.infoq.com/news/2009/12/XAML-2009-Future
Here's a counter argument.
Here is the explanation from Adam Nathan's WPF 4 unleashed book: "The
x:Reference markup extension is often mistakenly associated with the
XAML2009 features that can only be used from loose XAML at the time
of this writing. Although x:Reference is a new feature in WPF 4, it
can be used from XAML2006 just fine as long as your project is
targeting version 4 or later of the .NET Framework. One glitch is
that the XAML designer in Visual Studio 2010 doesn�t properly handle
x:Reference, so it gives the following design-time error that you can
safely ignore: Service provider is missing the INameResolver service"
http://wpftutorial.net/XAML2009.html
Workaround solution ...

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