I have created my own control for WPF and in .cs code I want to define method to change margin parameter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public String TagName { get; set; }
public UserControl1(String TagName)
{
this.TagName = TagName;
InitializeComponent();
}
public double TagValue { get; set; }
public void test(double val) {
valueRectangle.Margin.Top(10);
}
}
}
But I'm still getting error while setting any value to Margin.Top
Edit: Errorcode here:
Severity Code Description Project File Line Suppression State
Error CS1955 Non-invocable member 'Thickness.Top' cannot be used like a method. WpfApplication1 X:\07_projects\00_ostatni\CSharp\WpfApplication1\WpfApplication1\UserControl1.xaml.cs 36 Active
As Clemens wrote:
Besides that Top is a property, not a method, you should assign a new
Thickness like valueRectangle.Margin = new Thickness(0, 10, 0, 0);
Related
hey there I've just now started making a engine with tutorials while I was doing so I stumbled upon a error.
Here Is What the Error Told:
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'ProjectBrowserDialg' could not be found (are you missing a using directive or an assembly reference?) Editor C:\Users\abrus\OneDrive\Documents\cpp_files_me\MehroofEngine\src\MehroofEngine For C++\Editor\MainWindow.xaml.cs 37 Active
this is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Editor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += OnMainWindowLoaded;
}
private void OnMainWindowLoaded(object sender, RoutedEventArgs e)
{
Loaded -= OnMainWindowLoaded;
OpenProjectBrowserDialog();
}
private void OpenProjectBrowserDialog()
{
var projectBrowser = new ProjectBrowserDialg();
}
}
}
if I forgot something in the code please reply
if not enough information please reply it helps me make better posts.
I have a WPF window with a combo box. The contents of the combo box should be populated from a column in a SQL table. I created a LINQ To SQL Data Context Class in a models folder and set the ItemsSource of the ComboBox to the class (Customers) generated by the data context.. But when I run the program, the contents of the combobox are the types of the items it pulled..
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using eTouchLV.GatekeeperUI.Models;
namespace eTouchLV.GatekeeperUI
{
public partial class MainWindow : Window
{
SQLDataClassDataContext dc = new SQLDataClassDataContext(Properties.Settings.Default.GatekeeperDBConnectionString);
public MainWindow()
{
InitializeComponent();
AssignDataToComponents();
}
private void AssignDataToComponents()
{
this.CheckDBIsValid();
comboCustomers.ItemsSource = dc.Customers;
}
private void CheckDBIsValid()
{
if (!dc.DatabaseExists())
{
MessageBox.Show("Program started on the machine but can't seem to find the database...", "Error connecting to DB on SQL server.");
}
}
}
}
Program:
I just started with C# at work and I did read like a lot of the questions that are about my error here at StackOverflow. Unfortunately I still don't get what I'm doing wrong.
So, I followed this (http://qafriend.com/c-ui-automation-tutorial/automate-using-c-tutorial-guide-part-5) tutorial that is about C# and UI Automation. Everything went fine until I had to write actual code. So my code is this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Automation;
using Automation = System.Windows.Automation;
namespace AutomateCSharp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
static AutomationElement desktopObject = AutomationElement.RootElement;
static Automation.Condition testWindowNameCondition = new PropertyCondition(AutomationElement.NameProperty, "Demo Window For Csharp Automation");
static Automation.Condition textConditionOne = new PropertyCondition(AutomationElement.AutomationIdProperty, "InputOne");
static Automation.Condition textConditionTwo = new PropertyCondition(AutomationElement.AutomationIdProperty, "InputTwo");
static Automation.Condition textConditionTotal = new PropertyCondition(AutomationElement.AutomationIdProperty, "Total");
static AutomationElement testWindow = desktopObject.FindFirst(TreeScope.Children, testWindowNameCondition);
static AutomationElement textOne = testWindow.FindFirst(TreeScope.Descendants, textConditionOne);
static ValuePattern valuetextOne = textOne.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
valuetextOne.SetValue("4");
}
}
And I get the Error: The name 'valuetextOne' does not exist in the current context.
Could someone please tell me what I'm wrong about? Thanks a lot in advance :)
This problem has to do with the fact that functions can't be called inside class bodies directly. Only fields, properties and methods can be written there. What you need to do, is wrap it inside another function, like so:
public partial class MainWindow : Window
{
// Stuff
public void SetValue() => valuetextOne.SetValue("4");
}
And then you call it as:
public void Foo()
{
MainWindow window = Bar();
window.SetValue();
}
I go into more detail here about this.
Whenever I put a breakpoint in the WPF App.XAML.cs file, the breakpoints are not hitting. As if the file is not executed at all. So weird.... There is no the usual 'yellow breakpoint' indicating the breakpoint is not hit. It just runs the program as if the file is not being executed at all
Anybody know why?
here's my App.Xaml.cs code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Abt.Controls.SciChart;
using Abt.Controls.SciChart.Model.DataSeries;
using Abt.Controls.SciChart.Utility;
using Abt.Controls.SciChart.Visuals;
using Abt.Controls.SciChart.Visuals.Axes;
using Abt.Controls.SciChart.Visuals.Annotations;
using Abt.Controls.SciChart.Visuals.RenderableSeries;
using Abt.Controls.SciChart.ChartModifiers;
using Abt.Controls.SciChart.Numerics.CoordinateCalculators;
namespace Hub
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
CUtilities.WCFCalls = new List<string>();
}
}
}
here's my app.g.cs file
#pragma checksum "..\..\App.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "788CF6544127ABDC6D6160DE3D084FC8"
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18444
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
namespace Client {
/// <summary>
/// App
/// </summary>
public partial class App : System.Windows.Application {
private bool _contentLoaded;
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
#line 4 "..\..\App.xaml"
this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
#line default
#line hidden
System.Uri resourceLocater = new System.Uri("/Client;component/app.xaml", System.UriKind.Relative);
#line 1 "..\..\App.xaml"
System.Windows.Application.LoadComponent(this, resourceLocater);
#line default
#line hidden
}
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public static void Main() {
Client.App app = new Client.App();
app.InitializeComponent();
app.Run();
}
}
}
I seem to remember this happening with older versions of Visual Studio, it's like it would take a second or two for the debugger to hook in to the process or something. I found I could force it manually with Debugger.Break(); In any case try manually deleting the bin/obj folders and doing a rebuild, sometimes it's also the result of the incremental compilation getting itself confused.
I am VS 2012 silverlight-5 beginner. I tried to serialize and de-serialize from a xml file. I have following error while doing this :
The type or namespace name 'Serializable' does not exist in the namespace 'System' (are you missing an assembly reference?)
The type or namespace name 'SerializableAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'DatamemberAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'Datamember' could not be found (are you missing a using directive or an assembly reference?)
My code for serialization is :
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
namespace Model.XML
{
public static class Serialization<T> where T : class
{
public static T DeserializeFromXmlFile(string fileName)
{
if (!File.Exists(fileName))
{
return null;
}
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
using (Stream stream = File.OpenRead(fileName))
{
return (T)deserializer.ReadObject(stream);
}
}
}
}
But i am interested in having each class seperately (not all the 3 classes (parameter,component and attribute) obtained in one class).
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Xml.Serialization.XmlTypeAttribute;
using System.Serializable; //It gives red line under Serializable
namespace Model.XML
{
[Serializable] //It gives red line
public class attribute
{
[Datamember] //It gives red line
public string name { get; set; }
[Datamember] //It gives red line
public string label { get; set; }
[Datamember] //It gives red line
public string unit { get; set; }
[Datamember] //It gives red line
public component thisComponent { get; set; }
}
}
i tried to reference using System.Serializable; but it is not available in the dll list i have in my VS2012.
What is the cause of problem and how to get a rid of it ? Thanks in advance.
EDIT AFTER Comments:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
namespace Model.XML
{
[DataContract]
public class attribute
{
[DataMember]
public string type { get; set; }
[DataMember]
public string displayed { get; set; }
[DataMember]
public string add_remove { get; set; }
[DataMember]
public string ccypair { get; set; }
[DataMember]
public List<int> item { get; set; }
public static void Main()
{
attribute Obj1 = Serialization<attribute>.DeserializeFromXmlFile("C:\\Users\\SHEK\\Desktop\\VannakNew\\DEV_CENTER\\Model\\XML\\XmlParameter.xml");
// Obj1.type = "shekhar";
}
}
}
And serialization.cs is :
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
namespace Model.XML
{
public static class Serialization<T> where T : class
{
public static T DeserializeFromXmlFile(string fileName)
{
if (!File.Exists(fileName))
{
MessageBox.Show("File Path is wrong");
return null;
}
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
using (Stream stream = File.OpenRead(fileName))
{
return (T)deserializer.ReadObject(stream);
}
}
}
}
Xml file is XmlParameter.xml
<?xml version="1.0" encoding="utf-8" ?>
<parameter>
<name>bands_amounts</name>
<label>Bands Amounts</label>
<unit></unit>
<component>
<type>List</type>
<attributes>
<type>Integer</type>
<displayed>4</displayed>
<add_remove>yes</add_remove>
<item>1 000 000</item>
<item>5 000 000</item>
<item>10 000 000</item>
<item>20 000 000</item>
</attributes>
<attributes>
<ccypair>XAUUSD</ccypair>
<item>100</item>
<item>500</item>
<item>1000</item>
</attributes>
</component >
</parameter>
**It is working without error but i wonder why it always null object(on debugging) even the path of XmlParameter.xml file is correct. Is it due to using "[DataContract]" instead of [Serialized] ? because it si silverlight application. Actually i have todisplay the elements inside the nodes like,i have to display "XAUUSD" in my attribute.cs class which present in node <ccypair>XAUUSD</ccypair> **
There is no System.Serializable namespace; the SerializableAttribute class is in the System namespace. You should just write using System;.
I think you're confusing C# using statements with Java import statement. In Java, you import specific classes (or all classes from a package with *). In C#, the using statement imports all types in the specified namespace.
Currently, specifying a class name after using is an error, but in C# 6 it will be possible, and will import all static members from the class.
Other issue: you wrote Datamember instead of DataMember; C# is case sensitive. And anyway, DataMember has no effect on a class that doesn't have the DataContract attribute.
Also make sure you have referenced the System.Runtime.Serialization assembly.
I got the same problem and by adding this resolved the problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
Just add
using System.Runtime.Serialization;
This can also be caused if the application was targeted for .NET 2.0 but your are compiling if for a different .NET version/stack.