I'm struggling a bit with UI Automation for an WindowsStore app. I get an UITestControlNotAvailableException as soon as I implement IValueProvider. If I remove the implementation it works.
Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotAvailableException (The control is not available or not valid.)
InnerException: Points to a call to testControl.Name (Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_Name())
InnerException: An event was unable to invoke any of the subscribers (Exception from HRESULT: 0x80040201)
The StackTrace looks like this:
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaUtility.MapAndThrowException(Exception e, IUITechnologyElement element, Boolean useRethrowException)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaUtility.GetAutomationPropertyValue[T](AutomationElement element, AutomationProperty property)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.GetAutomationPropertyValue[T](AutomationProperty automationProperty)
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.get_NativeWindowHandle()
at Microsoft.VisualStudio.TestTools.UITest.Extension.Uia.UiaElement.get_WindowHandle()
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_IsRefetchRequired()
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.EnsureValid(Boolean waitForReady, Boolean refetch)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValuePrivate(String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyPrivate(String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyOfType[T](String propertyName)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_Name()
at UITest.Framework.Windows.TestGround.GetElementString(UITestControl element, Int32 level) in ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) in ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.WalkTree(UITestControl element, Int32 level) ...
at UITest.Framework.Windows.TestGround.PrintTree(Modes mode, XamlWindow window) in ...
On the app side I use this code to get information about the control:
public class CanvasAP : FrameworkElementAutomationPeer, IValueProvider
{
public CanvasAP(Windows.UI.Xaml.Controls.Canvas owner) : base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return "Canvas";
}
protected override object GetPatternCore(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Value)
{
return this;
}
return base.GetPattern(patternInterface);
}
#region Implementation of IValueProvider
bool IValueProvider.IsReadOnly => true;
string IValueProvider.Value
{
get
{
var str = "Test";
var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;
foreach (var child in owner.Children)
{
str += $"{child.GetType()}";
}
return str;
}
}
void IValueProvider.SetValue(string value)
{
}
#endregion
}
On the UI Automation client side I use this code to get information about the control:
private static string GetElementString(UITestControl element, Int32 level = 0)
{
var xaml = element as XamlControl;
var str = "";
for (var i = 0; i < level; i++)
str += " ";
str += $"{element.ControlType} {element.ClassName} {element.Name} {xaml?.AutomationId ?? ""}\n";
return str;
}
Found the bug that I had made - it was in calling base.GetPattern instead of base.GetPatternCore.
protected override object GetPatternCore(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Value)
return this;
return base.GetPatternCore(patternInterface);
}
Related
I want to binding a byte[] to difference label in grid view but it is weird when I change the value in a timer event,the onPropertyChanged() don't call,turns out the label didn't update with the byte[].Pls help me out where is the mistake in my code.I can binding a byte with the code I list,but seems not work with byte[].Thanks!
System.Timers.Timer testtimer;
Grid gridView;
byte[] _recvdata;
byte testbyte;
public byte[] RecvData
{
get
{
return _recvdata;
}
set
{
if (_recvdata != value)
{
_recvdata = value;
OnPropertyChanged("RecvData");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected override void OnPropertyChanged(string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public DevicePage(IDevice currDevice)
{
device = currDevice;
this.Title = "Status";
testtimer = new System.Timers.Timer();
testtimer.Interval = 1000;
testtimer.Elapsed += OnTimedEvent;
testtimer.Enabled = true;
gridView = new Grid();
RecvData = new byte[40];
for (byte i = 0; i < 40;i++)
{
string st;
var lb = new Label ();
st = "RecvData[" + i + "]";
lb.SetBinding(Label.TextProperty, st);
lb.BindingContext = this;
gridView.Children.Add(lb, 0, i);
}
this.Content = gridView;
}
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
byte i;
testbyte += 1;
for (i = 0; i < 40; i++)
{
RecvData[i] = testbyte;
}
}
The setter of RecvData property is not called when you just change it using [indexer]. You have two options I see:
1) manually call OnPropertyChanged after you change the byte array using indexer:
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
byte i;
testbyte += 1;
for (i = 0; i < 40; i++)
{
RecvData[i] = testbyte;
}
OnPropertyChanged("RecvData");
}
2) create a wrapper for byte array which would call OnPropertyChanged automatically when you change an array using the indexer:
public class ByteArrayWrapper : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
byte[] _array;
public byte[] Array
{
get
{
return _array;
}
set
{
if (_array != value)
{
_array = value;
OnPropertyChanged("Array");
}
}
}
public byte this[int index]
{
get
{
return _array[index];
}
set
{
_array[index] = value;
OnPropertyChanged("Array");
}
}
protected void OnPropertyChanged(string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
then instead of operating on your byte array like this:
public byte[] RecvData
{
// get/set
}
you should use an instance of above class:
public ByteArrayWrapper RecvData
{
// get/set
}
Other code should be just fine. If the binding doesn't work, maybe you'd need to bind to RecvData.Array[i] instead of RecvData[i], please let me know if that works as I'm not able to test right now.
I'm not able to override GetChildrenCore correctly. I use this for a Canvas to get information about it's children (Line, Rectangle).
The output correctly indicates the first child but misses the second. Even though the Canvas already contains both.
Custom Canvas
Custom Line Childs of Canvas parent: 2
Instead it should be like this:
Custom Canvas
Custom Line Childs of Canvas parent: 2
Custom Rectangle Childs of Canvas parent: 2
App side side:
public class ElementAP : FrameworkElementAutomationPeer
{
private FrameworkElement Owner = null;
private Int32 Count = 0;
public ElementAP(FrameworkElement owner, Int32 count) : base (owner)
{
Owner = owner;
Count = count;
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return $"{Owner.GetType().Name} Childs of Canvas parent: {Count}";
}
}
public class CanvasAP : FrameworkElementAutomationPeer
{
public CanvasAP(Windows.UI.Xaml.Controls.Canvas owner) : base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return "Canvas";
}
protected override IList<AutomationPeer> GetChildrenCore()
{
var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;
var list = new List<AutomationPeer> ();
foreach (var child in owner.Children)
{
var peer = new ElementAP(child as FrameworkElement, owner.Children.Count);
list.Add(peer);
}
return list;
}
}
UI Testing side:
private static string WalkTree(UITestControl element, Int32 level = 0)
{
var children = element.GetChildren();
var str = "";
foreach (var c in children)
{
str += GetElementString(c, level);
str += WalkTree(c, level + 1);
}
return str;
}
private static string GetElementString(UITestControl element, Int32 level = 0)
{
var xaml = element as XamlControl;
var str = "";
for (var i = 0; i < level; i++)
str += " ";
str += $"{element.ControlType} {element.ClassName} {element.Name} {xaml?.AutomationId ?? ""}\n";
return str;
}
I finally found an answer. When using a cache for the children`s AutomationPeers it works perfectly.
public class ElementAP : FrameworkElementAutomationPeer
{
public UIElement Element { get { return Owner; } }
public ElementAP(FrameworkElement owner) : base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return Owner.GetType().Name;
}
}
public class CanvasAP : FrameworkElementAutomationPeer
{
private List<ElementAP> _cachedAutomationPeers = new List<ElementAP>();
public CanvasAP(Windows.UI.Xaml.Controls.Canvas owner) : base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return "Canvas";
}
protected override IList<AutomationPeer> GetChildrenCore()
{
var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;
if (owner.Children.All(c => c is CanvasA))
return base.GetChildrenCore();
var list = new List<ElementAP>();
foreach (var child in owner.Children)
{
var peer = _cachedAutomationPeers.FirstOrDefault(p => p.Element == child) ?? new ElementAP(child as FrameworkElement);
list.Add(peer);
}
_cachedAutomationPeers = list;
return list.Cast<AutomationPeer>().ToList();
}
}
C# allows the following functionality to display a user-friendly version of enums. The type converter takes the description attribute and uses it to generate a string. Can this be done in C++/CLI? From what I'm seeing, a 'public enum class' cannot have attributes on the enum members. This means I can't define the description attribute content for each enum. How can the friendly names be defined?
[TypeConverter(typeof(EnumDescriptionConverter))]
public enum MyEnum
{
[Description("Item1")]
Item1,
[Description("Item2")]
Item2,
}
class EnumDescriptionConverter : EnumConverter
{
private Type _enumType;
public EnumDescriptionConverter(Type type)
: base(type)
{
_enumType = type;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
{
return destType == typeof(string);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
{
String ReturnString = "";
if (_enumType.GetCustomAttributes<FlagsAttribute>().Any())
{
foreach (var val in EnumExtensions.GetIndividualFlags((Enum)value))
{
FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, val));
DescriptionAttribute dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
if (ReturnString != "")
ReturnString += " | ";
if (dna != null)
ReturnString += dna.Description;
else
ReturnString += val.ToString();
}
}
else
{
FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, value));
DescriptionAttribute dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
if (ReturnString != "")
ReturnString += " | ";
if (dna != null)
ReturnString += dna.Description;
else
ReturnString += value.ToString();
}
return ReturnString;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType)
{
return srcType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
foreach (FieldInfo fi in _enumType.GetFields())
{
DescriptionAttribute dna =
(DescriptionAttribute)Attribute.GetCustomAttribute(
fi, typeof(DescriptionAttribute));
if ((dna != null) && ((string)value == dna.Description))
return Enum.Parse(_enumType, fi.Name);
}
return Enum.Parse(_enumType, (string)value);
}
}
You've broken the cardinal rule of Stack Overflow questions: You didn't actually include real source code or the exact error message.
This code compiles just fine for me in Visual Studio 2013 in a brand new C++/CLI Console Application project:
using System::ComponentModel::DescriptionAttribute;
public enum class MyEnum
{
[Description("Item1")]
Item1,
[Description("Item2")]
Item2,
};
After researching this more, I resorted to the following type converter. It simply replaces an underscore in the enum member with a space. It can also handle flags.
ref class EnumDescriptionConverter : public System::ComponentModel::EnumConverter
{
private:
System::Type^ _enumType;
public:
EnumDescriptionConverter(System::Type^ type) : System::ComponentModel::EnumConverter(type)
{
_enumType = type;
}
bool CanConvertTo(System::ComponentModel::ITypeDescriptorContext^ context, System::Type^ destType)override
{
return destType == System::String::typeid;
}
System::Object^ ConvertTo(System::ComponentModel::ITypeDescriptorContext^ context, System::Globalization::CultureInfo^ culture, System::Object^ value, System::Type^ destType)override
{
return value->ToString()->Replace("_", " ");
}
bool CanConvertFrom(System::ComponentModel::ITypeDescriptorContext^ context, System::Type^ srcType)override
{
return srcType == System::String::typeid;
}
System::Object^ ConvertFrom(System::ComponentModel::ITypeDescriptorContext^ context, System::Globalization::CultureInfo^ culture, System::Object^ value)override
{
return System::Enum::Parse(_enumType, ((System::String^)value)->Replace(" ", "_")->Replace(",_", ", "));
}
};
Enums are defined like this:
[System::ComponentModel::TypeConverter(typeof(EnumDescriptionConverter))]
public enum class MyEnum
{
Item_1, // Appears as 'Item 1' when data bound or in a property grid
Item_2 // Appears as 'Item 2' when data bound or in a property grid
}
I have a search text-box in my app. In my database there are two column named English and Bangla. I can search either Bangla or English. there is a button beside search text-box.By default English search is activated. I can change the search option by clicking the button. It works correctly but problem is that the search is very slow.
search option selection code by clicking button is:
private void button5_Click(object sender, RoutedEventArgs e)
{
if (SearchMood % 2 != 0)
{
//search bangla column from the database
button5.Content = "Eng";
}
else {
//search english column from the database
button5.Content = "Bng";
}
SearchMood++;
}
Code for searching is:
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
List<dataLists> mylist = new List<dataLists>();
string word = textBox1.Text;
try
{
if (SearchMood % 2 == 0)// for english search
{
// show 5 words in listbox matched with entered text
var contacts = (from m in db.Dics where m.English.StartsWith(word) select new { m.English, m.Bangla }).Take(5);
string s1, s2;
try
{
foreach (var a in contacts)
{
s1 = a.English;
s2 = a.Bangla;
mylist.Add(new dataLists() { Eng = s1, Bng = s2 });
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
listBox1.ItemsSource = mylist;
}
else // for bangla search
{
// show 5 words in listbox matched with entered text
var contacts = (from m in db.Dics where m.Bangla.StartsWith(word) select new { m.English, m.Bangla }).Take(5);
string s1, s2;
try
{
foreach (var a in contacts)
{
s1 = a.English;
s2 = a.Bangla;
mylist.Add(new dataLists() { Eng = s1, Bng = s2 });
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
listBox1.ItemsSource = mylist;
}
}
catch { }
}
How can I increase the performance of searching??? Can anyone give any solution|???
N:B: My table creation script looks like
public System.Data.Linq.Table<Dic> Dics
{
get
{
return this.GetTable<Dic>();
}
}
public System.Data.Linq.Table<Learn_table> Learn_tables
{
get
{
return this.GetTable<Learn_table>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dic")]
public partial class Dic : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _Serial;
private string _English;
private string _Bangla;
private System.Nullable<int> _Fav;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnSerialChanging(int value);
partial void OnSerialChanged();
partial void OnEnglishChanging(string value);
partial void OnEnglishChanged();
partial void OnBanglaChanging(string value);
partial void OnBanglaChanged();
partial void OnFavChanging(System.Nullable<int> value);
partial void OnFavChanged();
#endregion
public Dic()
{
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="serial", Storage="_Serial", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int Serial
{
get
{
return this._Serial;
}
set
{
if ((this._Serial != value))
{
this.OnSerialChanging(value);
this.SendPropertyChanging();
this._Serial = value;
this.SendPropertyChanged("Serial");
this.OnSerialChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="english", Storage="_English", DbType="NVarChar(2000)")]
public string English
{
get
{
return this._English;
}
set
{
if ((this._English != value))
{
this.OnEnglishChanging(value);
this.SendPropertyChanging();
this._English = value;
this.SendPropertyChanged("English");
this.OnEnglishChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="bangla", Storage="_Bangla", DbType="NVarChar(2000)")]
public string Bangla
{
get
{
return this._Bangla;
}
set
{
if ((this._Bangla != value))
{
this.OnBanglaChanging(value);
this.SendPropertyChanging();
this._Bangla = value;
this.SendPropertyChanged("Bangla");
this.OnBanglaChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="fav", Storage="_Fav", DbType="Int")]
public System.Nullable<int> Fav
{
get
{
return this._Fav;
}
set
{
if ((this._Fav != value))
{
this.OnFavChanging(value);
this.SendPropertyChanging();
this._Fav = value;
this.SendPropertyChanged("Fav");
this.OnFavChanged();
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Your problem appears in using TextChanged event Handler. Place a breakpoint there and you will see it firing twice and hence causing the slow performance for you. It seems a bug in WP7 TextBox control.
Use KeyUp event handler, instead of textBox1_TextChanged
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
//your code
}
Hope this solves your problem. !!
You can use of AutoCompleteBox rather than use of TextBox. AutoCompleteBox available in Microsoft.Phone.Control.Toolkit.
Execute you select query at once when you select language on buttonClick and assign result of your query to AutoCompleteBox.Itemsource. It should really increase search performance.
<toolkit:AutoCompleteBox x:Name="AutoBoxFood" Width="440" SelectionChanged="txtFodd_SelectionChanged" FilterMode="StartsWith" HorizontalAlignment="Left" Height="70"/>
Add indexes to the columns in the database file.
I have a type which has a default sort order as it implements IComparable<T> and IComparable. I'm not getting the results I expect from LINQ , basically it looks as if the IComparable<T> which the type implements is not being applied.
I thought I would get the result I want with an expression in the form:
var result = MyEnumerable<T>.OrderBy(r => r);
where T itself implements IComparable<T>. It's not happening.
I can see related questions where specific IComparable<T> classes are specified for the sort, but I can't find one which uses the default IComparable<T> implemented by T itself.
My syntax is clearly incorrect. What is the correct syntax please?
Thanks in advance.
OrderBy uses the default comparer Comparer<T>.Default which in turn will default to use the IComparable<T> implementation for T, or the non-generic IComparable if the former does not exist.
This code works:
public class Program
{
static void Main(string[] args)
{
var list = new List<Stuff>
{
new Stuff("one"),
new Stuff("two"),
new Stuff("three"),
new Stuff("four")
};
var sorted = list.OrderBy(x => x);
foreach (var stuff in sorted)
{
Console.Out.WriteLine(stuff.Name);
}
}
}
public class Stuff : IComparable<Stuff>
{
public string Name { get; set; }
public Stuff(string name)
{
Name = name;
}
public int CompareTo(Stuff other)
{
return String.CompareOrdinal(Name, other.Name);
}
}
public static class GenericSorter
{
public static IOrderedEnumerable<T> Sort<T>(IEnumerable<T> toSort, Dictionary<string, SortingOrder> sortOptions)
{
IOrderedEnumerable<T> orderedList = null;
foreach (KeyValuePair<string, SortingOrder> entry in sortOptions)
{
if (orderedList != null)
{
if (entry.Value == SortingOrder.Ascending)
{
orderedList = orderedList.ApplyOrder<T>(entry.Key, "ThenBy");
}
else
{
orderedList = orderedList.ApplyOrder<T>(entry.Key, "ThenByDescending");
}
}
else
{
if (entry.Value == SortingOrder.Ascending)
{
orderedList = toSort.ApplyOrder<T>(entry.Key, "OrderBy");
}
else
{
orderedList = toSort.ApplyOrder<T>(entry.Key, "OrderByDescending");
}
}
}
return orderedList;
}
private static IOrderedEnumerable<T> ApplyOrder<T>(this IEnumerable<T> source, string property, string methodName)
{
ParameterExpression param = Expression.Parameter(typeof(T), "x");
Expression expr = param;
foreach (string prop in property.Split('.'))
{
expr = Expression.PropertyOrField(expr, prop);
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), expr.Type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, param);
MethodInfo mi = typeof(Enumerable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), expr.Type);
return (IOrderedEnumerable<T>)mi.Invoke(null, new object[] { source, lambda.Compile() });
}
}