Namespace for my project - c#

What namespace i should to add for my project?
At this moment i get this error "Partial declarations of WPFApplication3.method must not specify different base classes"
Below is my code:
Code Method.xaml:
<Window x:Class="WpfApplication3.method"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="method" Height="300" Width="300">
//some code
</Window>
Code Method.cs:
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for method.xaml
/// </summary>
public partial class method : MonoBehaviour
{
public method()
{
InitializeComponent();
}
}
}

Change your Base class in .cs file to Window
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for method.xaml
/// </summary>
public partial class method : Window
{
public method()
{
InitializeComponent();
}
}
}

If you want to have a base class of MonoBehavior in your code-behind, you'll need to change your XAML accordingly, to specify the same base class.
Change this:
<Window x:Class="WpfApplication3.method" ... >
To this:
<Window x:Class="WpfApplication3.MonoBehavior" ... >

Related

c# WPF 'The name xxx does not exist in the namespace - what am i missing?

I am trying to learn WPF, and specifically, understand data-binding. I have started to run through some examples online. I cannot get this one to work. I have created a Simple Person object as follows and it is in the correct namespace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleDataBinding
{
public class Person
{
private string name;
public Person()
{
}
public Person(string value)
{
this.name = value;
}
public string PersonName
{
get { return name; }
set { name = value; }
}
}
}
I have created an object of this type.
namespace SimpleDataBinding
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Person Person = new Person();
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
}
}
and the XAML is as follows - the last line shows the line that it fails on.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:SimpleDataBinding"
SizeToContent="WidthAndHeight"
Title="Simple Data Binding Sample">
<Window.Resources>
<src:Person x:Key="myDataSource" PersonName="Joe"/>
The error from the build is.
Severity Code Description Project File Line Suppression State
Error The name "Person" does not exist in the namespace "clr-namespace:SimpleDataBinding". SimpleDataBinding ...\repos\SimpleDataBinding\SimpleDataBinding\MainWindow.xaml
But, Person does exist, and is in the shared namespace. Looking around here at similar issues all i get are recommendations to rebuild, or clean and rebuild - none of which seem to help anyone (or me).
As i'm new to this, it's likely, i just don't understand something and it's super-frustrating as i should surely be able to get this to work!
I've pushed the project to github here if it helps anyone...
You appear to be missing the x:Class directive on your window definition. Update your window element in the XAML:
<Window
x:Class="SimpleDataBinding.MainWindow"
....

deriving a class from another class causes Com registration error

I have a project that follows WPF/MVVM pattern. I have tried to derive my ViewModel class from PropertyChangedBase so that I can Notify of data changes to the view. As soon as I derive the class from PropertyChangeBase and compile, I get error saying
Error 1 Cannot register assembly "D:\Source\ArcOnline\bin\Debug\ArcOnline.dll". Could not load file or assembly 'Major, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
here is how my class is defined
using ArcOnline.WPF;
using System;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ArcOnline.ViewModels
{
/// <summary>
/// The class handles the template for a single
/// </summary>
public class TemplatesViewModel : PropertyChangedBase
{
/// <summary>
///
/// </summary>
public ArcOnlineViewModel ArcManager { get; set; }
private string _layerID;
/// <summary>
/// Layer ID
/// </summary>
public string LayerID
{
get { return _layerID; }
set { _layerID = value; NotifyOfPropertyChange(() => LayerID); }
}
private string _title;
/// <summary>
/// Title of the layer
/// </summary>
public string Title
{
get { return _title; }
set { _title = value; NotifyOfPropertyChange(() => Title); }
}
............. More properties as above
}
and here is the how my PropertyChangedBase is defined
using Component.Linq.Expressions;
using System;
using System.Collections;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Xml.Serialization;
#endregion
namespace Component.ComponentModel
{
/// <summary>
/// A base class from which ViewModels can inherit to improve the quality of
/// patterns such as INotifyPropertyChanged.
/// </summary>
[DataContract]
[Serializable]
public class PropertyChangedBase : INotifyPropertyChanged, INotifyDataErrorInfo, IDataErrorInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="PropertyChangedBase"/> class.
/// </summary>
public PropertyChangedBase()
{
ErrorsChanged += Notify_ErrorChanged;
}
void Notify_ErrorChanged(object sender, DataErrorsChangedEventArgs e)
{
NotifyOfPropertyChange(() => Error);
}
...... More class definition continues
}
This PropertyChangedBase is actually defined in a different Project so that is why it has all the header files included. If I copy the code of this PropertyChangedBase class and create a new class in my own Project where TemplatesViewModel is defined and then use it, it works perfectly fine.
I just want to know what is the reason for it not working in the first case?? This wasted my whole day!!
Thanks in advance

UserControls Base Class not found in NameSpace

Hi I'm currently stuck with this weird behaviour of Visual Studio
I've got this Base Class here:
namespace IntelliListDemo.Controls
{
public class NodeControlBase : UserControl
{
public NodeControlBase(NotYetGenericOutputNode internalNode)
{
this._internalNode = internalNode;
}
public NodeControlBase() { }
}
}
And i have some controls deriving from it. An Example:
namespace IntelliListDemo.Controls
{
/// <summary>
/// Interaktionslogik für Source.xaml
/// </summary>
public partial class SourceControl : NodeControlBase//, ISourceControl, INodeControl
{
public SourceControl(IntelliListLibrary.Nodes.SourceNode internalNode) : base(internalNode)
{
InitializeComponent();
}
}
And the XAML:
<Controls:NodeControlBase x:Class="IntelliListDemo.Controls.SourceControl"
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:Controls="clr-namespace:IntelliListDemo.Controls"
mc:Ignorable="d"
>
</Controls:NodeControlBase>
So I always get compiler Errors saying
The name "NodeControlBase" does not exist in Namespace
"clr-namespace:IntelliListDemo.Controls".
Found this old question when I had the same problem. Turned out I had a syntax error in my XAML code in another file that was preventing the build completing, and so the reference to the base class could not be resolved.
The way I tracked it down was commenting out the code related to the base UserControl class and making sure I had a clean compile - and being sure to check all the derived views when I re-introduced the new code!

How to bind button content to the static readonly field

I have a class MainWindow.xaml.cs:
namespace HomeSecurity {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged {
public static readonly string START = "start", RESET = "RESET";
.....
}
There is a button in MainWindow.xaml:
<Button x:Name="AcceptCamerasButton" Content="{x:Static local:MainWindow.START}" Grid.Row="1" Click="AcceptCamerasButton_Click"></Button>
How to set content of that button to MainWindow.Start? Current version does not work.
EDIT:
I have declared:
xmlns:local="clr-namespace:HomeSecurity"
but still when I use:
<Button x:Name="AcceptCamerasButton" Content="{x:Static local:MainWindow.START}" Grid.Row="1" Click="AcceptCamerasButton_Click"></Button>
I get:
Error 1 The member "START" is not recognized or is not accessible.
You cannot bind to fields. Bindings only work on properties. So you can either change the definition of START to a property, or create a property wrapper that returns the value of START and bind to that instead.
public static string START
{
get { return "start"}
}
public static string RESET
{
get { return "RESET"; }
}
Or, if you prefer to keep the readonly backing field:
private static readonly string startField = "start";
public static string START
{
get { return startField}
}
Also, I'm assuming that you've already done this, but I'm including this anyway, make sure you include the namespace declaration in the XAML file for the local namespace to point to the local assembly and appropriate namespace.
xmlns:local="clr-namespace:YourProjectAssemblyName..."

How can a WPF UserControl inherit a WPF UserControl?

The following WPF UserControl called DataTypeWholeNumber which works.
Now I want to make a UserControl called DataTypeDateTime and DataTypeEmail, etc.
Many of the Dependency Properties will be shared by all these controls and therefore I want to put their common methods into a BaseDataType and have each of these UserControls inherit from this base type.
However, when I do that, I get the error: Partial Declaration may not have different base classes.
So how can I implement inheritance with UserControls so shared functionality is all in the base class?
using System.Windows;
using System.Windows.Controls;
namespace TestDependencyProperty827.DataTypes
{
public partial class DataTypeWholeNumber : BaseDataType
{
public DataTypeWholeNumber()
{
InitializeComponent();
DataContext = this;
//defaults
TheWidth = 200;
}
public string TheLabel
{
get
{
return (string)GetValue(TheLabelProperty);
}
set
{
SetValue(TheLabelProperty, value);
}
}
public static readonly DependencyProperty TheLabelProperty =
DependencyProperty.Register("TheLabel", typeof(string), typeof(BaseDataType),
new FrameworkPropertyMetadata());
public string TheContent
{
get
{
return (string)GetValue(TheContentProperty);
}
set
{
SetValue(TheContentProperty, value);
}
}
public static readonly DependencyProperty TheContentProperty =
DependencyProperty.Register("TheContent", typeof(string), typeof(BaseDataType),
new FrameworkPropertyMetadata());
public int TheWidth
{
get
{
return (int)GetValue(TheWidthProperty);
}
set
{
SetValue(TheWidthProperty, value);
}
}
public static readonly DependencyProperty TheWidthProperty =
DependencyProperty.Register("TheWidth", typeof(int), typeof(DataTypeWholeNumber),
new FrameworkPropertyMetadata());
}
}
Ensure that you have changed the first tag in the xaml to also inherit from your new basetype
So
<UserControl x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
>
becomes
<myTypes:BaseDataType x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:myTypes="clr-namespace:TestDependencyProperty827.DataTypes"
>
So, to summarise the complete answer including the extra details from the comments below:
The base class should not include a xaml file. Define it in a single (non-partial) cs file and define it to inherit directly from Usercontrol.
Ensure that the subclass inherits from the base class both in the cs code-behind file and in the first tag of the xaml (as shown above).
public partial class MooringConfigurator : MooringLineConfigurator
{
public MooringConfigurator()
{
InitializeComponent();
}
}
<dst:MooringLineConfigurator x:Class="Wave.Dashboards.Instruments.ConfiguratorViews.DST.MooringConfigurator"
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:dst="clr-namespace:Wave.Dashboards.Instruments.ConfiguratorViews.DST"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</dst:MooringLineConfigurator>
I found the answer in this article: http://www.paulstovell.com/xmlnsdefinition
Basically what is says is that you should define an XML namespace in the AssemlyInfo.cs file, which can the be used in the XAML. It worked for me, however I placed the base user control class in a separate DLL...
There is partial class definition created by designer, you can open it easy way via InitializeComponent() method definition.
Then just change partial class iheritence from UserControl to BaseDataType (or any you specified in class definition).
After that you will have warning that InitializeComponent() method is hidden in child class.
Therefore you can make a CustomControl as base clas instead of UserControl to avoid partial definition in base class (as described in one comment).
I ran into the same issue but needed to have the control inherit from an abstract class, which is not supported by the designer. What solved my problem is making the usercontrol inherit from both a standard class (that inherits UserControl) and an interface. This way the designer is working.
//the xaml
<local:EcranFiche x:Class="VLEva.SIFEval.Ecrans.UC_BatimentAgricole"
xmlns:local="clr-namespace:VLEva.SIFEval.Ecrans"
...>
...
</local:EcranFiche>
// the usercontrol code behind
public partial class UC_BatimentAgricole : EcranFiche, IEcranFiche
{
...
}
// the interface
public interface IEcranFiche
{
...
}
// base class containing common implemented methods
public class EcranFiche : UserControl
{
... (ex: common interface implementation)
}

Categories