Using DesignData in WPF without including it into build - c#

I have some Design time view models bound to d:DataContext like this:
<UserControl x:Class="MyProject.Views.CallRecordView"
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:vm="clr-namespace:MyProject.ViewModels.DesignTime"
d:DataContext="{d:DesignInstance Type=vm:MyDesignViewModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d vm">
I changed Build Action of every ViewModel from Compile to DesignDataWithDesignTimeCreatableTypes
and it's working on design time, but when I try to compile I get following errors:
Error 20519 The type or namespace name 'DesignTime' does not exist in the namespace 'MyProject.ViewModels' (are you missing an assembly reference?) ...\obj\Debug\Views\MyView.g.cs 13 35 MyProject
how can I prevent my sample data from being attached to an assembly? Or maybe this is not neccessary and the compiler wouldn't include those files into executable by default even on Compile setting?
// edit:
also - everything is working and compiling when I create an empty class into DesignTime namespace:
namespace MyProject.ViewModels.DesignTime {}
but can I do it without changing my namespace or providing an empty one for compile time?

Related

error MC3066: The type reference cannot find a public type named 'StoreDb'. Line 9 Position 94

I download the source code of book from github here.
I am building DataBinding project in chapter 19 but it doesn't build and I get this compilation error.
error MC3066: The type reference cannot find a public type named
'StoreDb'. Line 9 Position 94.
<Window x:Class="DataBinding.BindToObjectDataProvider"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BindToObjectDataProvider" Height="450" Width="400"
xmlns:data="clr-namespace:StoreDatabase;assembly=StoreDatabase"
>
<Window.Resources>
<ObjectDataProvider ObjectType="{x:Type data:StoreDb}"
MethodName="GetProductsSlow" x:Key="products" IsAsynchronous="True"></ObjectDataProvider>
</Window.Resources>
Can anyone see why it doesn't build? Thank you!
I am using Visual Studio 2019.
I had copied the chapter 19 folder (where the project was) to another folder to build/run/play with so I don't modify the original but it required StoreDatabase project which was in root folder.
I tried to build the project on another PC that gave me a clue, it had an error that one project was not loaded. Copied that project to root fixed it.
This was the error loading solution.
C:\Projects\StoreDatabase\StoreDatabase.csproj : error : The project
file could not be loaded. Could not find a part of the path
'C:\Projects\StoreDatabase\StoreDatabase.csproj'.
C:\Projects\StoreDatabase\StoreDatabase.csproj

Namespace error when adding viewmodel

I come to you with a very strange problem.
At the first look it's an easy thing but trust me it's make me crazy !
I just want to specify wich file I want to associate to xaml page in DataContext, I already did it on other xaml page so I don't understand why !
<UserControl x:Class="FinalMediaPlayer.Onglet.Musique.Home.HomeMusique"
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:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home"
xmlns:tools="clr-namespace:FinalMediaPlayer.Tools"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<viewmodel:HomeViewModelMusique></viewmodel:HomeViewModelMusique>(error here)
</UserControl.DataContext>
The name "HomeViewModelMusique" doesn't exist "clr-namespace:FinalMediaPlayer.Onglet.Musique.Home".
namespace FinalMediaPlayer.Onglet.Musique.Home
{
public class HomeViewModelMusique : NotifyPropertyChanged
{
As you can see I put the path in viewmodel but apparently the file don't exist in the namespace ...
Thanks by advance guys, Im gonna suicide very soon...
Are all the namespaces in the same assembly? If they are not you will need to specify the assembly in the namespace declaration.
E.g:
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home;assembly=AssemblyName"
Check that the namespace is actually correct, then clean\rebuild your solution. Please consider also to use a tool like Resharper that will fix these problems automatically
Thanks for your fast answers guys !
I already try that :
xmlns:viewmodel="clr-namespace:FinalMediaPlayer.Onglet.Musique.Home;assembly=FinalMediaPlayer"
It doesn't work, "FinalMediaPlayer" is the name of my project, is there what is expected in the field assembly ?
I Get this on my AssemblyInfos.cs :
[assembly: AssemblyTitle("FinalMediaPlayer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FinalMediaPlayer")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
However Im gonna try to use Resharper but this is not a real solution :/.
If I find the solution I will post it.
Yes sorry guys I didn't answer that question, of course I did it.
Tryed tot clean/regenerate/generate. Whith no success.
Really I can't believe it, I have spend all my afternoon on this little stupid problem, maybe it's a bug coming from visual studio.
Im gonna try on my other computer later after work.
Again, Thanks to search with me.
namespace FinalMediaPlayer.Onglet.Musique.Home
{
public class HomeViewModelMusique : NotifyPropertyChanged
{

I am trying to give a path for my icon file but it shows an error?

<Window x:Class="AFIC.View.WizardDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:AFIC_Controller.View"
xmlns:res="clr-namespace:AFIC_Controller.Resources"
Title="{x:Static res:Strings.WizardWelcomeWindow_Title}"
ShowInTaskbar="True"
Width="800"
Height="600"
WindowStartupLocation="CenterScreen"
WindowStyle="SingleBorderWindow"
BorderBrush="#003B7B"
BorderThickness="0"
ResizeMode="NoResize"
Icon="/AFIC_Controller;component/Resources/Images/att_icon.ico"
>
<view:WizardView Loaded="WizardView_Loaded_1"/>
</Window>
I am giving path for my icon file as Icon="/AFIC_Controller;component/Resources/Images/att_icon.ico"
but after debugging it shows an error "Error 1 Could not find a part of the path 'C:\AFIC_Controller;component\Resources\Images\att_icon.ico'
Make sure the Icon file is included in your project and also change the Build Action of that icon file as Resources. For Build Action, go to the properties of that file.
Looking at your code, I have noticed something:
<Window x:Class="AFIC.View.WizardDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:AFIC_Controller.View"
xmlns:res="clr-namespace:AFIC_Controller.Resources"
Title="{x:Static res:Strings.WizardWelcomeWindow_Title}"
ShowInTaskbar="True"
Width="800"
Height="600"
WindowStartupLocation="CenterScreen"
WindowStyle="SingleBorderWindow"
BorderBrush="#003B7B"
BorderThickness="0"
ResizeMode="NoResize"
Icon="/AFIC_Controller;component/Resources/Images/att_icon.ico"
>
This class seems to be in a namespace named AFIC.View, but you're trying to access an image from the AFIC_Controller namespace. When trying to access a content file from a referenced assembly, you need to use the following syntax:
Referenced Assembly Resource File
The pack URI for a resource file that is compiled into a referenced assembly uses the following authority and path:
• Authority: application:///.
• Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format:
AssemblyShortName[;Version][;PublicKey];component/Path
◦ AssemblyShortName: the short name for the referenced assembly.
◦ ;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.
◦ ;PublicKey [optional]: the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded.
◦ ;component: specifies that the assembly being referred to is referenced from the local assembly.
◦ /Path: the name of the resource file, including its path, relative to the root of the referenced assembly's project folder.
So having a guess at your correct path, you could try this:
pack://application:,,,/AFIC_Controller;component/Resources/Images/att_icon.ico
If that still doesn't work, then please take a look at the many examples in the Pack URIs in WPF page on MSDN... one of them must fit your situation.
Add att_icon.ico in to Images folder through visual studio or any IDE you use. Copy and paste does not work.

Is this namespace collision due to a bug in the XAML to .NET code generation?

Below I describe how to reproduce an error I'm receiving. It behaves the same in VS 2010, 2012, and 2013. Breaking it into multiple projects, as I indicate below, is important.
Steps to reproduce the error:
Create a solution.
Create a C# class library called Common, containing one file named Handler.cs:
using System;
namespace Common
{
public delegate void Handler(object sender, EventArgs args);
}
Create a WPF user control library project called MyControlLibrary, referencing Common. In it, create a user control called MyControl.xaml.
MyControl.xaml:
<UserControl x:Class="ControlNamespace.MyControl"
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>
</UserControl>
MyControl.xaml.cs:
using System.Windows.Controls;
using Common;
namespace ControlNamespace
{
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
public event Handler MyEvent;
}
}
Create a WPF Application project called MyWpfApplication, referencing Common and MyControlLibrary. In it, create WindowNamespace.Common.cs as well as a window called MyWindow.xaml.
WindowNamespace.Common.cs:
namespace WindowNamespace.Common
{
}
MyWindow.xaml:
<Window x:Class="WindowNamespace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:ControlNamespace;assembly=WpfControlLibrary1"
Title="MyWindow" Height="300" Width="300">
<Grid>
<c:MyControl MyEvent="MyControl_MyEvent" />
</Grid>
</Window>
MyWindow.xaml.cs:
using System;
using System.Windows;
namespace WindowNamespace
{
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
}
void MyControl_MyEvent(object sender, EventArgs args)
{
}
}
}
Build the solution.
You should receive the following error, pointing to line 7 of MyWindow.xaml:
The type or namespace name 'Handler' does not exist in the namespace
'WindowNamespace.Common' (are you missing an assembly reference?)
If you open the .g.i.cs file generated for MyWindow.xaml, you should see the following in the IComponentConnector.Connect method:
#line 7 "..\..\MyWindow.xaml"
((ControlNamespace.MyControl)(target)).MyEvent += new Common.Handler(this.MyControl_MyEvent);
The source of the issue is that it is trying to find Common.Handler in WindowNamespace. This could be resolved by having it generated as:
#line 7 "..\..\MyWindow.xaml"
((ControlNamespace.MyControl)(target)).MyEvent += new global::Common.Handler(this.MyControl_MyEvent);
Or by adding a using to the top of the file:
using Common;
...
#line 7 "..\..\MyWindow.xaml"
((ControlNamespace.MyControl)(target)).MyEvent += new Handler(this.MyControl_MyEvent);
Note that if all these source files are bundled into a single project, the error goes away, because the .g.i.cs file is generated differently (i.e. it doesn't explicitly add the handler to the event).
Is this actually a bug in the XAML->.NET translation, or is there something I am doing wrong?
There's a clue as to what's happening if you redeclare the Handler delegate in your WindowNamespace.Common namespace declaration in MyWpfApplication and try to compile:
Error 1 Cannot implicitly convert type 'WindowNamespace.Common.Handler' to 'Common.Handler' c:\Dev\YourSolution\MyWpfApplication\MyWindow.xaml 7 63 MyWpfApplication
The Common project is declaring a global namespace called "Common", which your control library is also using. But when your application explicitly declares "WindowNamespace.Common" it creates a local namespace which just so happens to be the same namespace that the parent control resides in. This effectively creates the ambiguity problem described in the Visual Studio documentation for Compiler Error CS0433.
Change your common namespace declaration in MyWpfApplication to just be "Common" and the problem will go away.

WPF: XAML namespace

I have one assembly XAML registered in AssemblyInfo.cs:
[assembly: XmlnsDefinition("http://schemas.mysite.es/wpf", "SWC.ViewModels")]
And In my namespace I hace these classes:
namespace SWC.ViewModels
{
public class MenuViewModel
: ObservableCollection<MenuViewModel>
{
public MenuViewModel()
: base()
{
}
}
}
If I use the namespace in a user control XAML,
<UserControl x:Class="SWC.UserControls.UserMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:customMenu="http://schemas.mysite.es/wpf">
I can use the class in my XMLNS namespace,
<UserControl.Resources>
<customMenu:MenuViewModel x:Key="MenuItemsSource">
But, when I execute the application, the compiler said
The label 'MenuViewModel' dosen't exist in the namespace XML 'http://schemas.mysite.es/wpf'
All, can help me?? I'm crazy with this problem!!
Best regards,
I think you need to specify the assembly in which your customMenu controls exist. The assembly must also be referenced from the project (in the References section).
xmlns:customMenu="clr-namespace:customMenuNamespace;assembly=customMenuLibrary"
Otherwise I don't see how the compiler can find your implementation only through "http://schemas.mysite.es/wpf". What's at that address? The two microsoft schemas
http://schemas.microsoft.com/winfx/2006/xaml/presentation
http://http://schemas.microsoft.com/winfx/2006/xaml
work like some identifiers for the xaml compiler, there is nothing at those addresses.

Categories

Resources