Call instance of class in Xaml - silverlight

Help me understand.
I create template of SilverlightApplication1 in VS2012. Create in my solution folder Models and add class EmployeeTypes.cs. I need to call up the instance of EmployeeTypes.cs in Xaml file (Home.xaml).
I do so in Home.xaml:
<navigation:Page x:Class="SilverlightApplication1.Home"
xmlns:local="clr-namespace:SilverlightApplication1.Models"
....
....
Then i do so ( all this according tutorial http://weblogs.asp.net/psheriff/archive/2012/04/09/silverlight-tree-view-with-multiple-levels.aspx):
<local:EmployeeTypes x:Key=" employeeTemplate"/>
Here VS2012 says: 'local:EmployeeTypes' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Where i am wrong???
EmployeeTypes.cs
namespace SilverlightApplication1.Models
{
public class EmpolyeeTypes : List<EmployeeType>
{
public EmpolyeeTypes()
{
EmployeeType type;
type=new EmployeeType("Manager");
type.Employees.Add(new Employee("Michael"));
type.Employees.Add(new Employee("Paul"));
this.Add(type);
type = new EmployeeType("Project Managers");
type.Employees.Add(new Employee("Tim"));
type.Employees.Add(new Employee("John"));
type.Employees.Add(new Employee("David"));
this.Add(type);
}
}
}
<navigation:Page x:Class="SilverlightApplication1.Home"
xmlns:local="clr-namespace:SilverlightApplication1.Models"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
Title="Home"
Style="{StaticResource PageStyle}">
<local:EmployeeTypes x:Key=" employeeTemplate"/>
.......
.......
<Grid x:Name="LayoutRoot">
</Grid>

Try to move it to Resources of one UI control, for example as Resources of Grid :
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<local:EmployeeTypes x:Key=" employeeTemplate"/>
</Grid.Resources>
</Grid>
or as Resources of the page :
<navigation:Page
......
>
<navigation:Page.Resources>
<local:EmployeeTypes x:Key=" employeeTemplate"/>
</navigation:Page.Resources>
</navigation:Page>
PS: my answer came from Windows Phone and WPF background, could be different in silverlight. Not sure.

Related

How can I set region Manager for my dialog window in Prism?

I write my app without using Shell. So I created my own Window using IDialogService and opened in one of my modules.
As far, as I am concerned, Region Manager is attached to Shell, but due to the fact I don't have it, region manager doesn't work when I try to navigate from one view to another.
I know that Region Navigation works fine with the shell (I tested it) and the same code stops working when I substitute the shell with IDialogService.
Here is what I have
<Window x:Class="TechDocs.Views.MainSettingsWindowView"
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://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainSettingsWindow" Height="400" Width="750">
<Grid>
</Grid>
</Window>
Content for the first region. When I click the button, it should navigate to the second region.
<UserControl x:Class="TechDocs.Views.SettingsView"
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:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button Command="{Binding NodeSelectedCommand}" Name="Button"/>
<ContentControl prism:RegionManager.RegionName="region"/>
</Grid>
</UserControl>
In module I connect my root window with UserControl which holds the button and Content control for the second region.
public class SettingsModule : IModule
{
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
var dialogService = _containerProvider.Resolve<IDialogService>();
containerRegistry.RegisterDialog<MainSettingsWindow>("MyWindow");
containerRegistry.RegisterDialog<SettingsView>("customView");
containerRegistry.RegisterForNavigation<MyView>();
dialogService.Show("customView");
}
}
And when I click the button I get in this code
public void SelectedNode()
{
regionManager.RequestNavigate("region", "MyView");
}
RequestNavigate doesn't give any exceptions, but still nothing appears on the screen.
Could you please explain how I should register region manager with my window?
Try to initialize the region manager explicitly in your custom window using the static methods below:
RegionManager.SetRegionName(cc, "region");
RegionManager.SetRegionManager(cc, regionManager);
XAML:
<ContentControl x:Name="cc" prism:RegionManager.RegionName="region"/>

Partial declarations, must not specify different base classes?

When I changed this:
public partial class FrmCategories : UserControl
to this:
public partial class FrmCategories : MyUserControl
Where MyUserControl inherits from UserControl.
I got this error:
Error 2 Partial declarations of 'WpfTest.FrmCategories' must not
specify different base classes
\Projects\WpfTest\WpfTest\FrmCategories.xaml.cs 21 26 WpfTest
XAML:
<UserControl x:Class="WpfTest.FrmCategories"
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:we="clr-namespace:WpfTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Background="Azure" Height="131" Width="229">
<StackPanel Margin="5,24,5,0" Name="catFrm" Height="75" VerticalAlignment="Top">
I'm beginning WPF (as the name of the project implies), so I expect a trivial error here
You need to change the root element of the XAML file:
<we:MyUserControl x:Class="WpfTest.FrmCategories"
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:we="clr-namespace:WpfTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Background="Azure" Height="131" Width="229">
<StackPanel Margin="5,24,5,0" Name="catFrm" Height="75" VerticalAlignment="Top">
</we:MyUserControl>
partial only means that you can split the class definition into different files. But still all the partial definitions define only one class which as such may only have one base class.
If you want to inherit from MyUserControl, you have to change the xaml code, too:
<we:MyUserControl x:Class="WpfTest.FrmCategories"
...>
...
</we:MyUserControl>
Also, notice that you only have to specify the base class once, i.e. you could even change the C# code to
public partial class FrmCategories
because the base class has been defined in the xaml.

XAML Namespace Beginning with a Number

I have inheritted Button and am trying to add it to my main window.
It doesn't show up in the Toolbox. I have tried rebuilding several times.
I have tried adding xmlns:dc="clr-namespace:123Letters and xmlns:dc="clr-namespace:123Letters;assembly=123Letters to my MainWindow.
My MainWindow is:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="MainWin"
Title="123 Letters">
</Window>
My WeatherStationButton is:
<Button x:Class="WeatherStationButton"
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>
</Button>
and
Public Class WeatherStationButton
Inherits Button
Public Property WeatherStation As tblWeather
End Class
This is super simple stuff. I believe it's because XAML doesn't allow numbers in the first part of namespaces, but I can not find any reference to that anywhere, so I am asking if I am doing something wrong or is this one of XAML's "features"?
Just realized I could add my answer.
If your project name is 123Letters, you need to add an underscore before the first number when referencing it in XAML like, _123Letters.
Your clr-namespace declaration would then be:
xmlns:dc="clr-namespace:_123Letters"
and you could add your inherited button to your MainWindow like this:
<dc:WeatherStationButton x:Name="btnWS" />

local wpf binding How should be done?

guys I wanna use my own "usercontrol" in the main window, but i have problem with this line <local:texttoolbar x:Name="toolbar" DockPanel.Dock="Top">
The name of project is wpfin8 so i put it in the line with namespace
xmlns:local="clr-namespace:wpfin8">
main user control xaml code looks like this
<UserControl x:Class="TextEditor.TextEditorToolbar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded">
and the name is texttoolbar.xaml
The piece of mainwindow code is
<Window x:Class="wpfin8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:wpfin8">
<Menu x:Name="menu"
DockPanel.Dock="Top"></Menu>
<local:texttoolbar x:Name="toolbar" DockPanel.Dock="Top">
<ToolBar>
<Button >open
</Button>
<Button Margin="10,0,0,0">
save
</Button>
</ToolBar>
</local:texttoolbar>
I think i did everything properly and it dont want work and it still underlined with this "the name texttoolbar does not exist in the namespace "clr-namespace:wpfin8" " , I've also tried with the "TextEditorToolbar" , "TextEditor"
U Need to add assembly name also to your UserControl namespace used in MainWindow
like->
xmlns:local="clr-namespace:wpfin8;assembly=wpfin8"
assembly name may be different .. U can check that in your UserControl Properties named under AssemblyNamespace..
Hope it helps..:)

How Replace Xaml Text with Text based on MarkUp Extensions automatically

We have some translation mark up extension which looks like this:
TextBlock Text="{l:Translate 'My string'}"
and we want (because we could use some other tool for xaml translation) to replace text tags inside of all project xamls automatically.
Is there any way to find out all nodes or attributes with regex or with xml reader/write to implement this case?
Essentially, XAML meets the standard XML, but to work with it, you need external libraries. As an example: Microsoft XAML Toolkit CTP (download). Simple example, which displays a list of items:
// Previously adding the library
using Microsoft.Xaml.Tools.XamlDom;
XamlDomObject rootObject = XamlDomServices.Load("MainWindow.xaml");
foreach (XamlDomObject domObject in rootObject.DescendantsAndSelf())
{
MessageBox.Show(domObject.Type.ToString());
}
Set Background on every Control in your document:
XamlDomObject rootObject = XamlDomServices.Load("MainWindow.xaml");
foreach (XamlDomObject objectNode in
from control in rootObject.DescendantsAndSelf(typeof(Control))
where !control.HasMember("Background")
select control)
{
objectNode.SetMemberValue("Background", "Red");
}
XamlDomServices.Save(rootObject, "NewFile.xaml");
For replace value of Text property, I use example:
private void Window_ContentRendered(object sender, EventArgs e)
{
XamlDomObject rootObject = XamlDomServices.Load("MainWindow.xaml");
foreach (XamlDomObject objectNode in
from control in rootObject.DescendantsAndSelf(typeof(TextBlock))
where control.HasMember("Text")
select control)
{
objectNode.SetMemberValue("Text", "MyInsertedText");
}
XamlDomServices.Save(rootObject, "NewFile.xaml");
}
File Input:
<Window x:Class="XAMLdom.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" ContentRendered="Window_ContentRendered">
<Grid>
<TextBlock Text="SomeText" Width="100" Height="30" />
</Grid>
</Window>
File Output:
<?xml version="1.0" encoding="utf-8"?>
<Window xml:base="file:///C:/Documents and Settings/Kanc/мои документы/visual studio 2010/Projects/XAMLdom/XAMLdom/bin/Debug/MainWindow.xaml" x:Class="XAMLdom.MainWindow" Title="MainWindow" Height="350" Width="525" ContentRendered="Window_ContentRendered" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBlock Text="MyInsertedText" Width="100" Height="30" />
</Grid>
</Window>
We have used regular expressions which is not the best way but we can leave with it.

Resources