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.
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 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);
Regards,
I am trying to call a webservice from within the library of windows phone application that I am creating, however I have a weird problem and I hope that you can help me.
First of all I have the following code on my Application main page:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Library;
using System.Threading;
namespace Host
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
Class1.AsyncCall test = new AsyncCall();
test.Call("0002145", 51);
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
}
}
}
My AsyncCall class is in my library and it has the following code:
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.Collections.Generic;
using System.Threading;
using Library;
using Library.WebserviceRemote;
namespace Library
{
public class AsyncCallWS
{
WebserviceCall com = new WebserviceCall();
com.methodOne("methodOne", "1221", 122);
}
}
My WebserviceCall is another class in library and the method that is called looks like this:
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.ServiceModel;
using Library.WebserviceRemote;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace Library
{
//[DebuggerNonUserCode]
public class WebserviceCall
{
public void methodOne(String Method, String param1, int param2)
{
WebserviceRemote client = new WebserviceRemote ();
System.Diagnostics.Debug.WriteLine("I am in methodOne " + param1 + param2);
client.methodOneOnServiceCompleted += new EventHandler<methodOneOnServiceCompletedEventArgs>(client_methodOneOnServiceCompleted);
client.methodOneOnServiceAsync("1221", 122);
System.Diagnostics.Debug.WriteLine("After call to webservice ");
}
private void client_methodOneOnServiceCompleted(object sender, methodOneOnServiceCompletedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("This is the response " + e.Result);
}
}
}
Well the problem is that the method client_methodOneOnServiceCompleted is never read, the application never gets in it so I cannot get the response from webservice. I think it has something due to the EventHandler since this is all done in library not the applications page.
However, I am not aware why it doesn't get in that method. Furthermore, there are no errors and everything is there on output except this:
System.Diagnostics.Debug.WriteLine("This is the response " + e.Result);
Another notice: this code works like a charm when the call is made from within application itself but I really need to make webservice call from library, so please help :)
PS: this is all tested in emulator 7.0
Thank you.
I am trying to connect to R using C#. I installed R.Net and referenced it my project. This is my first attempt at C#. Any ideas what I am doing wrong?
This is the sample C# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RDotNet;
namespace RNet_Calculator
{
public partial class Form1 : Form
{
// set up basics and create RDotNet instance
// if anticipated install of R is not found, ask the user to find it.
public Form1()
{
InitializeComponent();
string dlldir = #"C:\Users\R\R-2.15.2\bin\x64";
bool r_located = false;
while (r_located == false)
{
try
{
REngine.SetDllDirectory(dlldir);
REngine.CreateInstance("RDotNet");
r_located = true;
}
catch
{
MessageBox.Show(#"Unable to find R installation's \bin\i386 folder.
Press OK to attempt to locate it.");
}
}
}
}
}
this is the Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Form1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
This is actually nothing to do with R. You have probably overwritten the namespace in one place and not in another. You have the code
namespace RNet_Calculator
in your form code. If you open Form1.designer.cs you will probably see
namespace Form1
Just change the namespace from Form1 to RNet_Calculator and your errors should disappear.
EDIT
In response to your edit, you should either change the single RNET_Calculator namespace back to Form1 or you should (but don't have to) change the Form1 namespace in your Program.cs file (and any other files in your project) as well. Doing this means you also should change the namespace in your project properties. Right-click your project, select Properties, and in the Application tab (should be the first one to open), change the "Default namespace" textbox to RNET_Calculator.