Silverlight indexer application example-general dictionary dataset Service

Source: Internet
Author: User
Tags silverlight

 

The current Silverlight client binding supports the indexing method. For example, the VM has attributes:
Public dictionary <string, string> keyvalues {Get; set ;}
In the backend CS, we want to access the value of a key by keyvalues ["XXXX"], where XXXX is the key. in XAML, we can access the following:
TEXT = "{bindings Path = keyvalues [xxxx]}"
As long as the indexer itself supports get, set can also implement bidirectional binding. However, there is a defect in this two-way approach, that is, if the value of keyvalues ["XXXX"] Changes midway through, the page cannot be notified to change as well. This is actually quite understandable. Although keyvalues itself can be an inotifypropertychanged object that can be notified of attributes, the index value itself cannot be notified of attribute changes. therefore, to bind general dictionary data, you must make some changes.
In addition, because Silverlight is asynchronous during server interaction, the value of the server cannot be directly returned to the caller, which also requires certain skills in data binding.
1) In general, the dictionary data of the system has considerable commonalities. The dictionary values include limited information such as keywords, codes, and descriptions, here is a simple dictionary value entity class:

Public class dicitemval
{
Public String key {Get; set ;}
Public String DESC {Get; set ;}
Public String code {Get; set ;}

}
2) dictionary entity:
Public class dictitem
{
Public String dictcode {Get; set ;}
Public String dictname {Get; set ;}
Public list <dicitemval> values {Get; set ;}
}
The dictionary above can be obtained from the server, accessed on the client, and adapted to asynchronous processing. It needs to be slightly wrapped:
3) dictionary packaging:
Public class dictbindingitem: inotifypropertychanged
{
Public String dictcode {Get; set ;}
Public String dictname {Get; set ;}
Private dictitem _ dict;
Public dictitem dict
{
Get {return _ dict ;}
Set {

If (_ dict! = Value)
{
_ Dict = value;
Raisepropertychanged ("dict ");
}
}
}

Public event propertychangedeventhandler propertychanged;
Protected void raisepropertychanged (string propertyname)
{
VaR handler = propertychanged;
If (handler! = NULL)
{
Handler (this, new propertychangedeventargs (propertyname ));
}
}
}

4) dictionary management is provided for VM usage. There are no static classes available here, and management is provided mainly for the convenience of pre-loading and other processing, and different VMS can not affect each other:
Public class dictmgmt
{
Private dictionary <string, dictbindingitem> _ dicts = new dictionary <string, dictbindingitem> ();
Public dictbindingitem this [String dictcode]
{
Get
{
Dictbindingitem thedict = NULL;
If (_ dicts. containskey (dictcode) = false)
{
Thedict = new dictbindingitem ();
Thedict. dictcode = dictcode;
_ Dicts. Add (thedict );
}
Else
{
Thedict = _ dicts [dictcode];
}
If (thedict. dict = NULL | thedict. dict. Values = NULL | thedict. dict. Values. Length <= 0)
{
Loaddict (thedict); // if no dictionary data exists, it is automatically loaded once.
}
}
}
Public void loaddict (dictbindingitem adict)
{
// Dictservices is the proxy class corresponding to the client that provides the dictionary loading service. You can implement it yourself. Getdict returns dictitem (including values). One parameter is dictcode.
Dictservices thesrv = new dictservices ();
Thesrv. getdict (adict. dictcode, OP => {
If (op. haserror = false)
{
Adict. dict = op. value;
}
}, Null );
}

}

5. It is easy to use. You can define a dictmgmt type attribute in the VM:
Private dictmgmt _ dictsource = new dictmgmt ();
Public dictmgmt dictsource {Get ;}
Bindings Path = dictsource [xxxx]. dict. Values}
In this case, the VM workload can be greatly simplified, and the page usage is also very simple. Of course, you can also expand and implement functions such as automatic pre-loading. This processing method is suitable for loading Dictionary data in large systems.
Note: The above Code has not been tested, and there will be no problem in the Mechanism. If there is an error, except that the dictionary service has not been implemented, all other errors should be caused by syntax errors. It should be okay to simply change it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.