Can not locate x:Shared in wpf - wpf

Please look into this question.
Issue with MVVM view first approach
But I'm unable to find "x:Shared" attribute.

here is the msdn link. it works in 3.5 also see below.
From the Book WPF Unleased:
Assuming the Conventional x Namespace Prefix.
x:Shared --> Attribute on any element in a ResourceDictionary, but only works if XAML is compiled!
//ResourceDictionary
<ResourceDictionary xmlns:x="http://schema.microsoft.con/winfx/2006/xaml>
<Image x:Shared="False" x:Key="zoom" Height="21" Source="zoom.png" />
...

Related

How can i make a prefix so i can address a folder in xaml

I always have problems making new prefixes in xaml. Most of the time, i get the message that the URI cannot be found in the assembly. My setup:
I have a WPF project (in a solution with class libs and asp.NET projects) with a MainWindow.xaml file. The XAML starts with : Window x:Class="MainWindow" ... .
So as default, there's no namespace given to it. In that same project i made a folder "Folder". In that folder, i have resx-files. What i need to do is make a prefix in xaml so i can address those files. I was thinking of :
xmlns:p="clr-namespace:WpfApplication.Folder"
and then for my controls
<Label Content="{x:Static p:NameResxFile.KeyName></Label>
However, the prefix generates the "URI cannot be found in the assembly" error. I'm i just failing at making prefixes?
Thanks in advance.
EDIT
If you cannot make a namespace ref to a folder, what is happening here?
xmlns specifies namespaces, it does not bother with folders or files, if you need access to an external resource you can load it into your control's resources via ResourceDictionary.
There was something like this i think:
<Window.Resources>
<ResourceDictionary x:Key="ExternalRes" Source="Folder/File.xaml"/>
....
</Window.Resources>
To reference an element of resource dictionary you should add that dictionary to your control's Resources collection or register it in the App.xaml file. After that you could just use StaticResource extension to get access to the element. Your code will look like this:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Folder/NameResxFile.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<!-- ... -->
<Label Content="{StaticResource KeyName}"/>
Things get trickier if you want to put the resource dictionary to another assembly and reference it. For this purpose refer to PackURIs in WPF article.

How do I reference my FlowDocument

I've defined a FlowDocument in a WPF control library (Add New Item..., FlowDocument -- is the root element of the file). I intend this to be used in several contexts such as in a user control or on a window, referenced in code for data-binding and export to xps, etc. But I can't figure out how to get a reference to an instance of this document. It doesn't seem to create an object in the compiled assembly.
More specifically, this is my problem
<MyUserControl ........ >
<FlowDocumentScrollViewer>
<!-- doesn't work --><namespaceRef:MyFlowDocument />
<FlowDocumentScrollViewer>
</MyUserControl>
The easiest solution is probably to put your FlowDocument inside a Resource Dictionary and then use the x:Key like this
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<FlowDocument x:Key="myFlowDocument" ..>
</FlowDocument>
</ResourceDictionary>
<FlowDocumentScrollViewer Name="flowDocumentScrollViewer">
<StaticResource ResourceKey="myFlowDocument"/>
</FlowDocumentScrollViewer>
Otherwise you'll have to set the FlowDocument to build action Embedded Resource and lode it in code behind with something like this
Stream flowDocumentStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DocumentNameSpace.FlowDocumentName.xaml");
FlowDocument flowDocument = (FlowDocument)XamlReader.Load(flowDocumentStream);
flowDocumentScrollViewer.Document = flowDocument;
Update
I think it might be possible to use an ObjectDataProvider to load the FlowDocument if you want to look into it. Anyway, a ResourceDictionary seems like the easy way out.

PlaneProjection is not working well in silverlight

in silverlight project using name attribute in planeprojection gives Error 1 The type or namespace name 'PlaneProjection' could not be found (are you missing a using directive or an assembly reference?)
code i used for that
<Image Name="blabla.jpg" Height="200" Width="200" >
<Image.Projection>
<PlaneProjection Name="pp" />
</Image.Projection>
</Image>
It is a strange error in that it appears to be complaining about the PlaneProjection itself but that could be a red-herring.
The xaml is not correct because the PlaneProjection does not have a Name property. However you should be able to x:Name. My guess is you are trying to ensure you have a field called pp that you can manipulator in code. You can now use pp as a field in code or find the PlaneProjection using FindName on the control.
This is what I think your Xaml should look like:-
<Image x:Name="MyImage" Source="blabla.jpg" Height="200" Width="200" >
<Image.Projection>
<PlaneProjection x:Name="pp" />
</Image.Projection>
</Image>
TBH, I would simply access the projection as MyImage.Projection rather than add an x:Name to it.

Problem Inserting Report Viewer into WPF Application using WindowsformsHost

I am trying to create a popup from my WPF application which shows a report viewer hosted in a WindowsFormsHost
however I am having problems with the following Xaml
<Page x:Class="FIS3.ReportViewer.ReportViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Title="MapViewer">
<Grid>
<my:WindowsFormsHost>
</my:WindowsFormsHost>
<Label Name="exampleText" Content="this is the Report Viewer ..." />
</Grid>
I am getting a build error notifying me that
"The type 'my:WindowsFormsHost' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built"
have I made an error in my XAML
I have added WindowsFormsIntegration as a reference to my project.
Thanks for your help
Col
There is a second solution to this that people seem to have overlooked. I scratched my head on this one for a good 20 minutes. No solution, including this one, would work. Turns out the solution for me was to add:
WindowsFormsIntegration
to my references
after I realized that based on:
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx
that this uses the WindowsFormsIntegration.dll. It seemed to have been missing from my references.
Hope this helps!
According to the MSDN documentation, WindowsFormsHost is included in the default Xaml namespace ("http://schemas.microsoft.com/winfx/2006/xaml/presentation") - though you do need to reference WindowsFormsIntegration, as you have done. Have you tried referencing WindowsFormsHost with no namespace prefix?
<Page x:Class="FIS3.ReportViewer.ReportViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MapViewer">
<Grid>
<WindowsFormsHost>
<WindowsFormsHost>
<Label Name="exampleText" Content="this is the Report Viewer ..." />
</Grid>

XamlParseException when databinding a ComboBox to a statically-defined array in XAML

I've been trying to solve this problem for over an hour now, and can't figure it out. Hopefully someone can see what I'm doing wrong.
I have two separate projects, both of which populate a combobox with an array of Doubles in the UserControl.Resources section, then databind to it in the GUI. What I'm doing is essentially just this, which works fine in kaxaml and in one of my two projects.
<Page>
<Page.Resources>
<x:Array x:Key="Increments" Type="sys:Double">
<sys:Double>0.01</sys:Double>
<sys:Double>0.02</sys:Double>
<sys:Double>0.03</sys:Double>
<sys:Double>0.04</sys:Double>
</x:Array>
</Page.Resources>
<Grid>
<ComboBox ItemsSource="{StaticResource Increments}" />
</Grid>
</Page>
The other project gives me the following error:
Cannot convert the value in attribute
'ItemsSource' to object of type
'System.Collections.IEnumerable'.
'System.Windows.Markup.ArrayExtension'
is not a valid value for property
'ItemsSource'. Error at object
'System.Windows.Controls.ComboBox' in
markup file ...
I cannot figure out why this is happening. I've tried looking at the schemas referenced in both XAML files, but they are the same... I don't have any errors or messages in the Output window. I got desperate and ran it through FxCop to see if it would catch something related, and although it has caught several valid errors, none of them were related.
I had to wrap this in an ObjectDataProvider to get it to work, and replace the StaticResource with a binding to the StaticResource:
<!-- Resources -->
<ObjectDataProvider x:Key="Incs2">
<ObjectDataProvider.ObjectInstance>
<x:Array Type="sys:Double">
<sys:Double>0.01</sys:Double>
<sys:Double>0.02</sys:Double>
<sys:Double>0.03</sys:Double>
<sys:Double>0.04</sys:Double>
</x:Array>
</ObjectDataProvider.ObjectInstance>
</ObjectDataProvider>
<!-- Page content -->
<ComboBox ItemsSource="{Binding Source={StaticResource Incs2}}" />
EDIT: I've also found that if I move the x:Array resource to the top of my Resources section, before any other resource declaration, I can use your original ItemsSource="{StaticResource ...}" and I no longer get the exception (or need the ObjectDataProvider). This would seem to be a bug in WPF.

Resources