WPF DesignData not working - wpf

I have a MVVM WPF application with a simple ListView. The ViewModel currently just contains a List property. I added a DesignData/SampleData.xaml file, set its Build Action to "DesignData" and referenced it in the Window's XAML code
<Window x:Class="..."
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="..."
mc:Ignorable="d"
Title="MainWindow" Height="483" Width="932">
<Grid d:DataContext="{d:DesignData Source=./DesignData/SampleData.xaml}">
But nothing shows up in the designer and VS2010 just gives me the following warning:
The file './DesignData/SampleData.xaml' is not part of the project or its 'Build Action' is not set to 'Resource'.
No matter what the "Build Action" it still gives me the same warning.
SampleData.xaml:
<MainWindowViewModel xmlns="clr-namespace:..."
xmlns:local="clr-namespace:...">
<MainWindowViewModel.Orders>
<local:Order OrderId="1000654321"/>
</MainWindowViewModel.Orders>
</MainWindowViewModel>
Any ideas on how to fix that?
Edit:
Project structure

The DesignData path is wrong as I thought it was relative to the solution/project whereas it seems it is relative to the containing XAML file.

Related

Why the SVG image does not display correctly in the program?

I wanna to display SVG file in WPF and I have found that many people recommend by using the sharpvectors.
I used it with the XAML as below:
<Window x:Class="KongGamLung.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:KongGamLung"
xmlns:SVG="http://sharpvectors.codeplex.com/svgc/"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<SVG:SvgViewbox Source="Create.svg" AutoSize="True" OptimizePath="False" TextAsGeometry="True"></SVG:SvgViewbox>
</Grid>
</Window>
Well, the SVG displayed in the editor correctly:
However, after I ran the program, the SVG has gone.
Why it turns out to be this? What's more, I have tried several SVG packages in NUGET. But they are worse than this that cannot display even in the editor.
How can I solve this? And if there is a better SVG package than this, please recommend it to me. Thank you.
You have 2 options.
If you set the svg file's Build Action to "Content", then you should set Copy to Output Directory to "Copy Always" or "Copy if newer".
If you set the svg file's Build Action to "Resource", it goes as assembly resource that can be accessed by a Resource File Pack URI.
Then you can use it in XAML like below and you can set Copy to Output Directory to "Do not Copy".
<SVG:SvgViewbox Source="Create.svg"
AutoSize="True" OptimizePath="False" TextAsGeometry="True"/>

Why am I getting an error loading resources/resources.xaml in the designer when I instantiate my WPF user control on a page?

I only get this error in the designer; my game runs just fine.
The error is "Cannot locate resource resources/resources.xaml". It only occurs on views where I reference my user control, and only in the designer - not when I design the user control itself, and not at runtime; I'm able to compile my game just fine even though I have this error.
Here is where I declare my user control instance:
<local:StatsView Grid.ColumnSpan="2" x:Name="stats"/>
And here are the relevant parts of my user control:
<UserControl x:Class="Wormholes.Views.StatsView"
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:Wormholes.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<ResourceDictionary Source="/Resources/Resources.xaml" />
</UserControl.Resources>
<Grid>
...
</Grid>
</UserControl>
I'm also referencing this same Resources.xaml in each of my pages, in addition to from the user control; could this be causing the error? If so, how do I work around it?
You have used a Relative URI, so it will looks for your resource file in a somewhere that you used your UserControl. It can't find you resource file because your resource file is not in the AbsolutePath.
AbsolutePath = CurrentPath (r.g Where you used your UserControl) + RelativePath
so Use an AbsolutePath:
<ResourceDictionary Source="pack://application:,,,/{YourAssemblyName};component/Resources/Resources.xaml" />

"Live" data in XAML editor window in Visual Studio and running application

I am trying implementing an addin for SparxEA with the MVVM Light. One thing that I found interesting is seeing “live” data in a window as is mentioned in the course of MVVM Light. So, I would like to do the same. As I have Class Library project I can’t use App.XAML.
In XAML I have this code:
<Window x:Class="GoatJira.View.About"
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:GoatJira.View"
xmlns:viewmodel="clr-namespace:GoatJira.ViewModel"
mc:Ignorable="d"
Title="{Binding Path=AboutTitle}" Height="322.613" Width="573.608" ResizeMode="NoResize" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" Initialized="Window_Initialized"
DataContext="{Binding Source={StaticResource ResourceKey=AboutData}}"
>
<Window.Resources>
<ResourceDictionary>
<viewmodel:AboutViewModel x:Key="AboutData"/>
</ResourceDictionary>
</Window.Resources>
…
This perfectly works within Visual Studio IDE. When I run the app and want to instantiate the window, I obtain this exception (translated from Czech): Source marked as AboutData was not found. Names of sources are case sensitive.
When I remove the 10th line with DataContext, I can’t see the bind data within the VS, on the other hand, application works fine when I assign the DataContext in code. My understanding is, that there is a way when it works in VS and in running app without changing anything.
Do you have any idea what I am doing wrong?
If needed, the whole code is at https://github.com/SlavekRydval/GoatJira.
What happens if you move the DataContext to after the resource section i.e.
<Window blah=for>
<Window.Resources>
<ResourceDictionary>
<viewmodel:AboutViewModel x:Key="AboutData"/>
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<StaticResourceExtension ResourceKey="AboutData"/>
</Window.DataContext>

Inconsistency between XAML intellisense and compiler for a control

I am converting some Silverlight XAML into WPF. I currently have a user control (MyControl) that is trying to include a couple of other controls that are custom buttons (MyButton1) that are within the same assembly. I have the following XAML that compiles and works in SL:
MyButton1
<UserControl
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"
x:Class="MyCompany.MyNamespace.MySubnamespace.MyButton1"
d:DesignWidth="640" d:DesignHeight="480">
...
</UserControl>
MyControl
<UserControl
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:somename="clr-namespace:MyCompany.MyNamespace.MySubnamespace;assembly=MyCompany.MyAssemblyName"
mc:Ignorable="d"
x:Class="MyCompany.MyNamespace.MySubnamespace.MyControl"
d:DesignWidth="640" d:DesignHeight="480">
<somename:MyButton1 />
</UserControl>
When I try to compile this code in WPF, I get the following error:
The tag 'MyButton1' does not exist in XML namespace 'clr-namespace:MyCompany.MyNamespace.MySubnamespace;assembly=MyCompany.MyAssemblyName.'
The weird thing is, if I comment out the <somename:MyButton1 /> line of code and compile and then type in <somename: IntelliSense gives me the option to autocomplete MyButton1. Which suggests that the control itself is in the assembly but for some reason it is not being seen when the MyControl XAML is being compiled.
For some context, I took the SL csproj file and made some modifications to it manually to make it a WPF csproj file. If there is a possibility that this caused this funkiness to happen, I'd be glad to share relevant portions of the project file.
Found the answer on an MSDN forum. Turns out that the assembly=MyCompany.MyAssemblyName line in my xmlns was screwing things up. Once I removed that line, I was able to reference the controls.
Related Link: http://social.msdn.microsoft.com/Forums/en/wpf/thread/807c9b80-81c7-4f75-aa2f-8427e17b1a90
To reference the current assembly you must not type its name but leave it blank, i.e. assembly=, the other option is of course to drop it completely. (MSDN -> Mapping to Current Assemblies)

The type toolkit:BusyIndicator was not found

I'm working on a WPF project with the beginning of a UserControl defined as:
<UserControl x:Class=""
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:converters="clr-namespace:.Modules.Converters"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
mc:Ignorable="d">
Within the user control I have this:
<toolkit:BusyIndicator IsBusy="{Binding IsBusy}" BusyContent="Please wait...">
I have the WPFToolkit.Extended referenced within my project and that reference appears to be valid (does not have a red underline).
However, I'm getting this error and don't know why:
The type 'toolkit:BusyIndicator' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Any ideas?
Navigate to the folder that conatins the DLL. Right click on the DLL and select properties. Under the general tab near the bottom click the "Unblock" button. You should be good to go.

Resources