WPF ItemsControl doesnt update when I add a new item in ItemsSource [duplicate] - wpf

I've got a WCF service that passes around status updates via a struct like so:
[DataContract]
public struct StatusInfo
{
[DataMember] public int Total;
[DataMember] public string Authority;
}
...
public StatusInfo GetStatus() { ... }
I expose a property in a ViewModel like this:
public class ServiceViewModel : ViewModel
{
public StatusInfo CurrentStatus
{
get{ return _currentStatus; }
set
{
_currentStatus = value;
OnPropertyChanged( () => CurrentStatus );
}
}
}
And XAML like so:
<TextBox Text="{Binding CurrentStatus.Total}" />
When I run the app I see errors in the output window indicating that the Total property cannot be found. I checked and double checked and I typed it correctly. The it occurred to me that the errors specifically indicate that the 'property' cannot be found. So adding a property to the struct made it work just fine. But this seems odd to me that WPF can't handle one-way binding to fields. Syntactically you access them the same in code and it seem silly to have to create a custom view model just for the StatusInfo struct. Have I missed something about WPF binding? Can you bind to a field or is property binding the only way?

Binding generally doesn't work to fields. Most binding is based, in part, on the ComponentModel PropertyDescriptor model, which (by default) works on properties. This enables notifications, validation, etc (none of which works with fields).
For more reasons than I can go into, public fields are a bad idea. They should be properties, fact. Likewise, mutable structs are a very bad idea. Not least, it protects against unexpected data loss (commonly associated with mutable structs). This should be a class:
[DataContract]
public class StatusInfo
{
[DataMember] public int Total {get;set;}
[DataMember] public string Authority {get;set;}
}
It will now behave as you think it should. If you want it to be an immutable struct, that would be OK (but data-binding would be one-way only, of course):
[DataContract]
public struct StatusInfo
{
[DataMember] public int Total {get;private set;}
[DataMember] public string Authority {get;private set;}
public StatusInfo(int total, string authority) : this() {
Total = total;
Authority = authority;
}
}
However, I would first question why this is a struct in the first place. It is very rare to write a struct in .NET languages. Keep in mind that the WCF "mex" proxy layer will create it as a class at the consumer anyway (unless you use assembly sharing).
In answer to the "why use structs" reply ("unknown (google)"):
If that is a reply to my question, it is wrong in many ways. First, value types as variables are commonly allocated (first) on the stack. If they are pushed onto the heap (for example in an array/list) there isn't much difference in overhead from a class - a small bit of object header plus a reference. Structs should always be small. Something with multiple fields will be over-sized, and will either murder your stack or just cause slowness due to the blitting. Additionally, structs should be immutable - unlesss you really know what you are doing.
Pretty much anything that represents an object should be immuatable.
If you are hitting a database, the speed of struct vs class is a non-issue compared to going out-of-process and probably over the network. Even if it is a bit slower, that means nothing compared to the point of getting it right - i.e. treating objects as objects.
As some metrics over 1M objects:
struct/field: 50ms
class/property: 229ms
based on the following (the speed difference is in object allocation, not field vs property). So about 5x slower, but still very, very quick. Since this is not going to be your bottleneck, don't prematurely optimise this!
using System;
using System.Collections.Generic;
using System.Diagnostics;
struct MyStruct
{
public int Id;
public string Name;
public DateTime DateOfBirth;
public string Comment;
}
class MyClass
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
public string Comment { get; set; }
}
static class Program
{
static void Main()
{
DateTime dob = DateTime.Today;
const int SIZE = 1000000;
Stopwatch watch = Stopwatch.StartNew();
List<MyStruct> s = new List<MyStruct>(SIZE);
for (int i = 0; i < SIZE; i++)
{
s.Add(new MyStruct { Comment = "abc", DateOfBirth = dob,
Id = 123, Name = "def" });
}
watch.Stop();
Console.WriteLine("struct/field: "
+ watch.ElapsedMilliseconds + "ms");
watch = Stopwatch.StartNew();
List<MyClass> c = new List<MyClass>(SIZE);
for (int i = 0; i < SIZE; i++)
{
c.Add(new MyClass { Comment = "abc", DateOfBirth = dob,
Id = 123, Name = "def" });
}
watch.Stop();
Console.WriteLine("class/property: "
+ watch.ElapsedMilliseconds + "ms");
Console.ReadLine();
}
}

I can only guess why they only support properties: perhaps because it seems to be a universal convention in the .NET framework never to expose mutable fields (probably to safeguard binary compatibility), and they somehow expected all programmers to follow the same convention.
Also, although fields and properties are accessed with the same syntax, data binding uses reflection, and (so I've heard) reflection must be used differently to access fields than to access properties.

Related

How to find a specific field from a genetic List object in wpf

I am trying to create a generic class for finding duplicate values for different class.
I am casting each list to an object and passing it as parameter like this
List<StudentModel1> a = new List<StudentModel1>();
List<StudentModel2> b = new List<StudentModel2>();
List<StudentModel3> c = new List<StudentModel3>();
List<object> obj = a.Cast<object>().ToList();//like this
public bool Duplicate(List<object> obj, string Fieldname, string Name)
{
if (obj.Any(x => x.Fieldname.Contains(Name))) { return true; } else { return false; }
}/// something like this;
here i am passing fieldname propery ,string name and object for finding duplicate and return a bool.How to access field name in linq.
please help how to acheive this.
thanks.
If I understand your question correctly, you have 3 different versions of a class: StudentModel1, StudentModel2 and StudentModel3 and you want to be able to compare lists of them. You are casting them to Object so that you can pass any any version of that class to your Duplicate method.
Assuming the above is correct, what you need is inheritance. If that's not something your familiar with you should definitely read up on it.
Consider the following:
class StudentModelBase
{
public string SomeProperty { get; set; }
}
class StudentModel1 : StudentModelBase
{
}
class StudentModel2 : StudentModelBase
{
}
class StudentModel3 : StudentModelBase
{
}
If your Duplicate method should be able to handle any of the "StudentModel" classes, then that means the information needed to tell if there are duplicates must be common to all three versions of that class. The properties and fields which store that information should be moved into StudentModelBase. Then, instead of casting to Object you should cast to StudentModelBase.
Your cast becomes:
List<StudentModelBase> base = a.Cast<StudentModelBase>().ToList();
And your Duplicate method becomes something like the following:
public bool Duplicate(List<StudentModelBase> obj, string Name)
Notice I left out Fieldname because if this is implemented correctly you shouldn't need to pass that as a parameter. You should just be able to access the need members of StudentModelBase like any normal variable, since they should be the same for all versions of "StudentModel".

Dapper - two columns to be switched over time (controlled by config value)

I have the following problem:
A several tables with "data", "token_data" columns that switch their values over time
Phases:
In the current phase 0, there is only "data" column (clear data).
In phase 1 there will be "data", "token_data" columns.
In the phase 2, there will be "token_data", "clear_data" columns.
In the last phase 3, there should be only "data" column (by that
time it should be tokenized).
We currently have all dapper/db models with phase 0 in mind.
Is there a way to prepare Dapper models for all 4 phases? I was looking for OptionalColumn attribute, but couldn't find one.
Ideally there would be a global config switch that would control which Dapper model property represents the tokenized "data" column.
Like:
// Not good
[Column("Name")]
public string Name
{
get { return AppSettings.TokenizationEnabled ? this.TokenName : _name; }
set { _name = value; }
}
It's not 100% clear what you need to do. For example, why you can'y just created a class with all properties, and, depending on the phase, return the correct data for that phase. Something like:
class MyData {
public int Phase;
public String Data { private get; public set; }
public String Token_Data { private get; public set; }
public String Clean_Data { private get; public set; }
public String GetData()
{
switch(Phase): {
case 1: return Token_Data; break;
case 2: return Clean_data; break;
default: return Data; break
}
}
Aside from that, anyway, I think the feature called "Type Switching per Row" can help you: https://github.com/StackExchange/Dapper#type-switching-per-row

Binding with the same name in different windows xaml MVVM

I think is possible, but is it to be good practice to have Data Binding with the same name in different windows? Or every binding is need to be with unique name in all application, not only in the scope of window. In case we used MVVM.
<DatePicker SelectedDate="{Binding StartMeasureDateRestoreSelect}" />
This code in the Window1.xaml and in the Window2.xaml is problem to be with the same name like here is "StartMeasureDateRestoreSelect" or good practice is to be with the different name.
Sorry for my bad English and not very good explaining.
It is important to have various names as it gives cleaner and nicer look for other developers who will see you program.
Read about Naming Convention.
As Wiki says:
In computer programming, a naming convention is a set of rules for
choosing the character sequence to be used for identifiers which
denote variables, types, functions, and other entities in source code
and documentation.
Reasons for using a naming convention (as opposed to allowing
programmers to choose any character sequence) include the following:
to reduce the effort needed to read and understand source code;1
to enable code reviews to focus on more important issues than arguing over syntax and naming standards.
to enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.
to enhance source code appearance (for example, by disallowing overly long names or unclear abbreviations).
Syntax for naming properties in C#:
private DataTable employeeDataTable;
public DataTable EmployeeDataTable
{
get { return employeeDataTable; }
set
{
employeeDataTable = value;
}
}
It is better to name bool properties with prefix Is
public bool IsBusy { get; set; }
And name your class/properties/variables to understand what goal of them. For example:
public class Person//goal of this class is keep state/info about Person
{
public string Name { get; set; }//goal of this property is keep state/info about
//Name of Person
private void PopulateByPeople()// in my view it self-explainable name of method
{
employeeDataTable = new DataTable();
for (int i = 0; i < 5; i++)
{
employeeDataTable.Columns.Add(i.ToString() + " column");
}
for (int i = 0; i < 50; i++)//Adding 50 DataRows
{
var theRow = employeeDataTable.NewRow();
for (int j = 0; j < 5; j++)
{
theRow[j] = "o " + (i).ToString();
}
employeeDataTable.Rows.Add(theRow);
}
}
}

Autofixture, expected behavior?

Having a test similar to this:
public class myClass
{
public int speed100index = 0;
private List<int> values = new List<int> { 200 };
public int Speed100
{
get
{
return values[speed100index];
}
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var fixture = new Fixture();
var sut = fixture.Create<myClass>();
Assert.AreEqual(sut.Speed100, 200);
}
}
Would have expected this to work, but I can see why it's not. But how do I argue, that this is not a problem with AutoFixture, but a problem with the code?
AutoFixture is giving you feedback about the design of your class. The feedback is, you should follow a more object-oriented design for this class.
Protect your private state, to prevent your class from entering an inconsistent state.
You need to make the speed100index field, private, to ensure it remains consistent with the values List.
Here is what I see if I run debugger on your test:
Autofixture assigns a random number to speed100index field because it is public, and in your array there is nothing at point 53 (from my screenshot)
If you set speed100index to be private, Autofixture will not re-assign the number and your test will pass.

saving variables wp7

Whats the best way to save variables like userid that is stored and reachable from different pages in WP7.
There's the querystring method, but can be kind of a pain to implement.
When navigating, pass the parameter like a HTTP querystring.
Then, on the otherside, check if the key exists, and extract the value. The downside of this is if you need to do more than 1, you need to type it in yourself, and it only supports strings.
So to pass an integer, you'd need to convert it. (And to pass a complex object, you need to take all the pieces you need to recompile it on the other side)
NavigationService.Navigate(new Uri("/PanoramaPage1.xaml?selected=item2", UriKind.Relative));
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string selected = String.Empty;
//check to see if the selected parameter was passed.
if (NavigationContext.QueryString.ContainsKey("selected"))
{
//get the selected parameter off the query string from MainPage.
selected = NavigationContext.QueryString["selected"];
}
//did the querystring indicate we should go to item2 instead of item1?
if (selected == "item2")
{
//item2 is the second item, but 0 indexed.
myPanorama.DefaultItem = myPanorama.Items[1];
}
base.OnNavigatedTo(e);
}
Here's a sample app that uses a querystring.
http://dl.dropbox.com/u/129101/Panorama_querystring.zip
A easier (and better) idea is to define a variable globally, or use a static class. In App.xaml.cs, define
using System.Collections.Generic;
public static Dictionary<string,object> PageContext = new Dictionary<string,object>;
Then, on the first page, simply do
MyComplexObject obj;
int four = 4;
...
App.PageContext.Add("mycomplexobj",obj);
App.PageContext.Add("four",four);
Then, on the new page, simply do
MyComplexObj obj = App.PageContext["mycomplexobj"] as MyComplexObj;
int four = (int)App.PageContext["four"];
To be safe, you should probably check if the object exists:
if (App.PageContext.ContainsKey("four"))
int four = (int)App.PageContext["four"];
You may use an App level variable (defined in App.xaml.cs) and access it from anywhere within your app. If you want to persist, shove it into Isolated Storage and read it on App launch/activate. There are helpers available to JSon serialize/deserialize your reads/writes from the Isolated Storage.
Check out Jeff's post (here) on tips to use Isolated Storage.
Hope this helps!
Well "best" is always subjective, however, I think an application service is a good candidate for this sort of thing:-
public interface IPhoneApplicationService : IApplicationService
{
string Name {get; set;}
object Deactivating();
void Activating(object state);
}
public class AuthenticationService : IPhoneApplicationService
{
public static AuthenticationService Current {get; private set; }
public void StartService(ApplicationServiceContext context)
{
Current = this;
}
public void StopService()
{
Current = null;
}
public string Name {get; set;}
public object Deactivating()
{
// Return an serialisable object such as a Dictionary if necessary.
return UserID;
}
public void Activating(object state)
{
UserID = (int)state;
}
public int UserID { get; private set; }
public void Logon(string username, string password)
{
// Code here that eventually assigns to UserID.
}
}
You place an instance of this in your App.xaml:-
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
<local:AuthenticationService Name="AuthServ" />
</Application.ApplicationLifetimeObjects>
Now you do need to tweak the App.xaml.cs:-
private void Application_Activated(object sender, ActivatedEventArgs e)
{
var state = PhoneApplicationService.Current.State;
foreach (var service in ApplicationLifetimeObjects.OfType<IPhoneApplicationService>())
{
if (state.ContainsKey(service.Name))
{
service.Activating(state[service.Name]);
}
}
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
var state = PhoneApplicationService.Current.State;
foreach (var service in ApplicationLifetimeObjects.OfType<IPhoneApplicationService>())
{
if (state.ContainsKey(service.Name))
{
state[service.Name] = service.Deactivating();
}
else
{
state.Add(service.Name, service.Deactivating());
}
}
}
You can now access you UserID anywhere in your app with:-
AuthenticationService.Current.UserID
This general pattern can be used to maintain seperation of key application wide services (you don't load a whole bunch of incohesive properties into your App class). It also provides the hooks for maintaining state between activations which is essential.

Resources