Silverlight Memory store - silverlight

I'm making a silverlight website.
Is it possible to make some kind of a memory where i can store some values and can recall them from different pages?
thx

I normally create a class called Session, with a property called Default that looks like this:
public class SessionBase<T> : INotifyPropertyChanged where T : class
{
public static T Default
{
get
{
var instance = Application.Current.Resources["SessionName"] as T;
if (instance == null)
{
instance = Activator.CreateInstance<T>();
Application.Current.Resources.Add("SessionName", instance);
}
return instance;
}
}
}
Then in my app.xaml I have:
<Classes:Session x:Name="SessionName"/>
This way you can access the same instance with binding and with c# (code behind). T inherits SessionBase and in that class you can store any information you want and it will remain available throughout the application session. eg:
public class Session : SessionBase<Session>
{
public int X { get; set; }
}
You will then be able to bind to X (need to raise the PropertyChanged for two way binding) and access it from c#

Related

Using a generic ViewModel

I have a WPF MVVM App, but I want my ViewModel to be generic. What the app is suppose to do is take some Data and do CRUD operations on it without knowing the Type of the data it's getting at the compile-time. So I declared my ViewModel like this:
public class GenericViewModel<T> where T : class
{
private void ConstructorBase()
{
Type theType = typeof(T);
Properties = theType.GetProperties().ToList();
}
public GenericViewModel(DbContext _dbContextInsert) //pravi novi repository na osnovu DbContexta
{
ConstructorBase();
_R = new RepositoryGlobal<T>(_dbContextInsert);
}
public T newT { get; set; }
public T selectedT { get; set; }
public List<PropertyInfo> Properties { get; set; }
private RepositoryGlobal<T> _R;
}
Now, disregard almost everything you see inside it, the only important thing is that the Constructor is never reached. I set this ViewModel as the DataContext for the main window like this:
InitializeComponent();
this.DataContext = new GenericViewModel<Person>(new PersonDbContext());
But when I put a breakpoint inside the ViewModel's constructor, the program never gets stopped.
Any ideas?
Dependencies should be abstractions, not implementations.
Your generic view model should not create it's own repository, instead you should pass in an instance of this dependency via the constructor.
public class GenericViewModel<T> where T : class
{
protected readonly IRepository<T> _Repository;
public GenericViewModel(IRepository<T> repository)
{
_Repository = repository;
}
...
}
You would then create an instance of your repository like so:
DbContext context = new PersonDbContext();
IRepository<Person> personRepo = new PersonRepository(context);
GenericViewModel<Person> personViewModel = new GenericViewModel<Person>(personRepo);
There, your View Model's dependencies are no longer tied to a specific implementation, your code is now far more adaptable to changes. Not to mention massively easier to test.

Fill a combobox in WPF with a list of web services

I have a control like this:
<ComboBox x:Name="ComboTipo"
Height="23"
SelectionChanged="ComboTipo_SelectionChanged"
Width="450"
Canvas.Left="609"
Canvas.Top="26" />
And my code is:
ComboTipo.DisplayMemberPath = "Descripcion";
ComboTipo.SelectedValuePath = "IdTipoPersona";
ComboTipo.ItemsSource = myWebServices.dameTipos();
My web services returns a list for this object, this class is created in automatic when i add the reference to the web services:
public partial class TipoPersona {
private short idTipoPersonaField;
private string descripcionField;
/// <comentarios/>
public short IdTipoPersona {
get {
return this.idTipoPersonaField;
}
set {
this.idTipoPersonaField = value;
}
}
/// <comentarios/>
public string Descripcion {
get {
return this.descripcionField;
}
set {
this.descripcionField = value;
}
}
}
But the problem is:
The combobox displays the data types for each element of the list, and i want display the Descripcion.
Can you help me plis! Thanks
What does IdTipoPersona look like? Is it a class you created? If so, you may need to reference the property that you want displayed. It would look something like this:
ComboTipoPersona.SelectedValuePath = "IdTipoPersona.Text";
Where Text would be replaced by the property. It is really hard to judge otherwise what is going on with knowing a little more about the object structure that myWebServices.dameTipos() returns.
EDIT
Ok I was able to simulate your problem and simulate a solution as well.
Your issue is in the Tipos class. There are a couple of things necessary when binding to a combobox with a custom class.
First off, you will want to add accessors and mutators (getters and setters) to IdTippoPersona and Descripcion.
You should add a constructor that assigns to those properties with parameters.
It is usually a good idea to add a default constructor.
The finished code will look like this:
public class Tipos
{
public int IdTipoPersona { get; set; }
public string Descripcion { get; set; }
public Tipos(int id, string descripcion)
{
IdTipoPersona = id;
Descripcion = descripcion;
}
}
I found the asnwer if someone needs it.
We need create a class intermediate class but we were working with entity framework, for this way, we need add the intermediate class like complex type in my model (entity framework).
And also we need override this class.
And it works so well.
Thanks for all #Goody

Update a wpf label periodically

I am new to WPF and C# im trying to understand how can I update a UI element from a BL class (to keep a seperation between the logic and the UI) the bl gets periodic updates from a c++ network component and should update the form once a new argument comes in (I read on the msdn website but I want to see some concrete examples to make sure I got it right)
Because of your gets periodic updates from a c++ network component comment, I am assuming that you already have a system to update your property. I would expose that property from your business class in a view model class, a class with public properties and ICommand functions designed specifically to supply all of the required data to a view, or UserControl.
To be honest, I wouldn't have that (or any) functionality in a business class (depending what you mean by business class)... I'd personally put it straight into the view model, or have a manager/service class that exposed it.
If you insist on keeping it where it is, you'll have to implement either an event or a delegate in your business class so that users of that class can be alerted as to when the value changes. Then you could simply attach a handler to your event/delegate from the view model class and easily update the exposed property whenever the actual property changes.
So it would go a little something like this... in your business class (I am assuming that your value is an int, but you can change this if it is incorrect... the principal is the same):
public delegate void FieldUpdate(int value);
public FieldUpdate OnFieldUpdate { get; set; }
...
private int field;
public int Field
{
get { return field; }
set
{
if (value != field)
{
field = value;
if (OnFieldUpdate != null) OnFieldUpdate(field);
}
}
}
Then in your view model:
private YourBusinessClass instance = new YourBusinessClass();
public YourBusinessClass Instance
{
get { return instance; }
set { instance = value; NotifyPropertyChanged("Instance"); }
}
Attach a handler:
instance.OnFieldUpdate += OnBusinessClassFieldUpdate;
...
public void OnBusinessClassFieldUpdate(int value)
{
Instance = value;
}
Now whenever the field is updated in the business class, the view model (and data bound UI controls) will automatically update via the delegate.

binary serialization of types defined in a silverlight assembly

I have a silverlight assembly which is referenced by a silverlight application and a ASP.Net application.
In the ASP.Net app i need to serialize(Binary) some of the instances of types defined in the silverlight assembly. Can anyone help me with this?
Edit
My problem is that i have a silverlight assembly which is used by a silverilght application, a asp.net app and a winforms app, in my asp.net app and winforms app i need to serialize types defined in the shared silverlight assmebly.
Also on a slightly different note can some one explain why this attribute is needed?
Also on a slightly different note can
some one explain why this attribute is
needed?
Lets see:
During serialization BinaryFormatter calls methods of FormatterServices class. One of they is
private static MemberInfo[] InternalGetSerializableMembers(RuntimeType type)
This method contains next code:
if (!CheckSerializable(type))
{
throw new SerializationException(Environment.GetResourceString("Serialization_NonSerType", new object[] { type.FullName, type.Module.Assembly.FullName }));
}
lets check CheckSerializable method of FormatterServices class:
private static bool CheckSerializable(RuntimeType type)
{
return type.IsSerializable;
}
This code similar to:
Type t = typeof (SomeClass);
bool isTypeSerializable = t.IsSerializable;
In this example isTypeSerializable will be true if class SomeClass has SerializableAttribute. Otherwise false.
Soo... Simple answer: this attribute is flag which indicates that instances of class may be serialized. So it just needed and thats all.
Regarding your main question:
Unfortunately you can't use binary formatter for instance of class defined in silverlight assembly directly.
One way to do that to use a proxy class.
Example:
Class inside your silverlight assembly:
public class SomeClass
{
public int IntValue { get; set; }
public string StringValue { get; set; }
public bool BoolValue { get; set; }
}
Proxy class which supports serialization inside common .NET assemly with reference to silverlight assembly:
[Serializable]
public class SomeClassProxy
{
public int IntValue { get; set; }
public string StringValue { get; set; }
public bool BoolValue { get; set; }
public static SomeClassProxy GetSerializableObject(SomeClass silverlightClass)
{
return new SomeClassProxy
{
IntValue = silverlightClass.IntValue,
StringValue = silverlightClass.StringValue,
BoolValue = silverlightClass.BoolValue
};
}
public static SomeClass DeserializeSilverlightCompatible(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
SomeClassProxy proxy = formatter.Deserialize(stream) as SomeClassProxy;
return new SomeClass
{
IntValue = proxy.IntValue,
StringValue = proxy.StringValue,
BoolValue = proxy.BoolValue
};
}
}
So in your asp .net or winforms applications you should operate with proxy class:
using(MemoryStream memoryStream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
//create instance of silverlight class
SomeClass mySilverlightClass = new SomeClass();
mySilverlightClass.IntValue = 555;
mySilverlightClass.StringValue = "Hello World!";
mySilverlightClass.BoolValue = true;
//<===serialize and take care of silverlight instance
formatter.Serialize(memoryStream, SomeClassProxy.GetSerializableObject(mySilverlightClass));
memoryStream.Seek(0, SeekOrigin.Begin);
//===>deserialize to silverlight instance
SomeClass mySilverlightClassRestored = SomeClassProxy.DeserializeSilverlightCompatible(memoryStream);
}
So this proxy takes care on both serialization and deserialization (after deserialization you'll receive instance of class SomeClass which defined in silverlight assembly).
If binary formatter is soft rescriction then I may recommend you to use xml serializer instead:
XmlSerializer s = new XmlSerializer(typeof(SomeClass));
s.Serialize(memoryStream, mySilverlightClass);
memoryStream.Seek(0, SeekOrigin.Begin);
SomeClass restored = s.Deserialize(memoryStream) as SomeClass;
It that case SerializableAttribute isn't needed.
You could try out my serializer which is capable of moving binary data between .NET and Silverlight... http://whydoidoit.com
I used something called the sharp serializer which is able to binary serialize types without a serializable attribute.
You can use #if directives to include serialization attributes (and code) for non-Silverlight builds:
#if !SILVERLIGHT
[Serializable]
#endif
public class SomeClass
{
}

Winforms: access class properties throughout application

I know this must be an age-old, tired question, but I cant seem to find anything thru my trusty friend (aka Google).
I have a .net 3.5 c# winforms app, that presents a user with a login form on application startup. After a successful login, I want to run off to the DB, pull in some user-specific data and hold them (in properties) in a class called AppCurrentUser.cs, that can thereafer be accessed across all classes in the assembly - the purpose here being that I can fill some properties with a once-off data read, instead of making a call to the DB everytime I need to. In a web app, I would usually use Session variables, and I know that the concept of that does not exist in WinForms.
The class structure resembles the following:
public class AppCurrentUser {
public AppCurrentUser() { }
public Guid UserName { get; set; }
public List<string> Roles { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
Now, I have some options that I need some expert advice on:
Being a "dumb" class, I should make the properties non-static, instantiate the class and then set the properties...but then I will only be able to access that instance from within the class that it was created in, right?
Logically, I believe that these properties should be static as I will only be using the class once throughout the application (and not creating new instances of it), and it's property values will be "reset" on application close. (If I create an instance of it, I can dispose of it on application close)
How should I structure my class and how do I access its properties across all classes in my assembly? I really would appreciate your honest and valued advice on this!!
Thanks!
Use the singleton pattern here:
public class AppUser
{
private static _current = null;
public static AppUser Current
{
get { return = _current; }
}
public static void Init()
{
if (_current == null)
{
_current = new AppUser();
// Load everything from the DB.
// Name = Dd.GetName();
}
}
public string Name { get; private set; }
}
// App startup.
AppUser.Init();
// Now any form / class / whatever can simply do:
var name = AppUser.Current.Name;
Now the "static" things are thread-unsafe. I'll leave it as an exercise of the reader to figure out how to properly use the lock() syntax to make it thread-safe. You should also handle the case if the Current property is accessed before the call to Init.
It depends on how you setup your architecture. If you're doing all your business logic code inside the actual form (e.g. coupling it to the UI), then you probably want to pass user information in as a parameter when you make a form, then keep a reference to it from within that form. In other words, you'd be implementing a Singleton pattern.
You could also use Dependency Injection, so that every time you request the user object, the dependency injection framework (like StructureMap) will provide you with the right object. -- you could probably use it like a session variable since you'll be working in a stateful environment.
The correct place to store this type of information is in a custom implementation of IIdentity. Any information that you need to identify a user or his access rights can be stored in that object, which is then associated with the current thread and can be queried from the current thread whenever needed.
This principal is illustrated in Rocky Lhotka's CLSA books, or google winforms custom identity.
I'm not convinced this is the right way but you could do something like this (seems to be what you're asking for anyway):
public class Sessions
{
// Variables
private static string _Username;
// properties
public static string Username
{
get
{
return _Username;
}
set
{
_Username = value;
}
}
}
in case the c# is wrong...i'm a vb.net developer...
then you'd just use Sessions.USername etc etc

Resources