复选框选中n取消选中的问题 [英] Problem with checkbox check n uncheck

查看:77
本文介绍了复选框选中n取消选中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中,我有一个包含复选框的下拉列表,
当在后台检查项目时,将进行服务调用并将数据更新到UI.数据获取速度更快,但是直到将UI线程更新到UI为止,UI线程才是可用的,因此在此之前,该复选框保持禁用状态.
我尝试通过为复选框检查处理程序创建新线程来尝试,但是由于其中包含UI线程,因此出现了跨线程异常,并且两个调度程序均未起作用.

我希望该复选框立即响应用户操作,而不管后台发生了什么.

就像-当用户选中该复选框时,该复选框应立即反映出来,然后应更新用户界面.检查后,它可能保持禁用状态,直到更新完成.
现在,Rite会在用户进行检查时突出显示该复选框,但是需要花费一些时间才能显示刻度线.

In the code, i have a dropdown consisting of checkboxes,
When an item is checked- in the background a service call is made and data is updated to the UI. fetching of data is done faster but the UI thread wont be free until it is updated to the UI so till then the checkbox remains disabled.
I tried by creating a new thread for checkbox check handler but as there is a UI thread inside, i get a cross thread exception and neither dispatcher worked.

I want the checkbox to respond immediately to the user action irrespective of what is happening in the background.

Like-when the checkbox is checked by the user,it should reflect immediately and then the UI should be updated. After it is checked it may remain disabled until update is done.
Rite now the checkbox gets highlighted when the user checks but it takes time for the tick mark to appear.

推荐答案

阿莎,
这是进行此操作的简要说明:

Hi Asha,

Here is a briefly explained way of doing it:

private void OnSpaceStatusChecked ( object sender, EventArgsEndpointStatus e )
{
 Thread newThread = new Thread(DoYourBackgroundWork);
 newThread.IsBackground = true;
 // You can pass a parameter here, which can be retrieved from the obj parameter in the DoYourBackgroundWork  function.
 newThread.Start(param); 
}

private void DoYourBackgroundWork(object obj)
{
 //Do some backgroundwork. 
 //...
 
 //MethodInvoker should be used only if the method you want to call doesn't have any parameters.
 //If you require the use of parameters, create a delegate for it and call its invoke method.
 this.Invoke(new MethodInvoker(UpdateTheUIs));
}

private void UpdateTheUIs()
{
 //Update your UI controls here.
}



更新:
OP的评论:
不可能,因为我的是Web应用程序..

嗯,还有一个使用委托的解决方案.

在类声明之前,在之后声明一个委托:



UPDATE:
Comment from OP:
Not possible because mine is web application..

Well there is another solution by using Delegates.

Declare a delegate after before the class declaration:

// Please declare this delegate before your class definition, ie. it sits on top of class declaration.
public delegate void UpdateUI(object params);
// Create a reference as a class field. To be declared after the class defenition
UpdateUI update;

private void OnSpaceStatusChecked ( object sender, EventArgsEndpointStatus e )
{
 //Point your delegate to the function
 update = UpdateTheUIs;
 Thread newThread = new Thread(DoYourBackgroundWork);
 newThread.IsBackground = true;
 // You can pass a parameter here, which can be retrieved from the obj parameter in the DoYourBackgroundWork  function.
 newThread.Start(param);
}

private void DoYourBackgroundWork(object obj)
{
 //Do some backgroundwork. 
 //...
 //Pass the delegate object together with the parameter so that it can call the UpdateTheUIs function without encountering any cross-thread exception. Use the obj parameter with values that will be required to update your UI.
 this.Dispatcher.Invoke(update , obj);
}

private void UpdateTheUIs(object param)
{
 // From param you will receive "obj" called from the above invoke.
 // Use it accordingly
 //Update your UI controls here.
}



更新2:

糟糕,我忘了它的光芒. :doh:

只需将this.Invoke替换为this.Dispatcher.Invoke
我已经相应地更新了答案.
希望现在清除.



UPDATE 2:

Oops, I forgot that its silverlight. :doh:

Just replace this.Invoke with this.Dispatcher.Invoke
I have updated the answer accordingly.
Hope its clear now.


您需要处理事件CheckedChanged.假设它是C#:

You need to handle the event CheckedChanged. Assuming it''s C#:

my.CheckBox.CheckedChanged += (sender, eventArgs) => {
    CheckState checkState = (CheckBox)sender.CheckState;
    //or, simpler:
    bool state = (CheckBox)sender.Checked;
    RespondImmediatey(checkState); //or whatever
};



—SA



—SA


感谢您的回复,

Mycode,

私有void OnSpaceStatusChecked(对象发送者,EventArgsEndpointStatus e){


_selectedSpaceHeader = null;
ShowExpanderControl(false);
_lstfilterOptions = e.Data;
GetOriginalSpaceList();

如果(_spaceHeaderList == null){
_spaceHeaderList = new List< SpaceHeader> ();
}
其他{
_spaceHeaderList.Clear();
}

GetStatusSearchList(_searchList);
//如果按文本搜索";则对文本进行过滤被执行.
SearchOnText();

if(!_spaceHeaderList.Contains(_selectedSpaceHeader)){
_selectedSpaceHeader = null;
}
UpdateSpaceListDataOnUI();
}

此处理程序已挂接到xaml中的复选框...
Thanks for responding,

Mycode,

private void OnSpaceStatusChecked ( object sender, EventArgsEndpointStatus e ) {


_selectedSpaceHeader = null;
ShowExpanderControl ( false );
_lstfilterOptions = e.Data;
GetOriginalSpaceList ();

if ( _spaceHeaderList == null ) {
_spaceHeaderList = new List<SpaceHeader> ();
}
else {
_spaceHeaderList.Clear ();
}

GetStatusSearchList ( _searchList );
//Filter on text if "search by text" is performed.
SearchOnText ();

if ( !_spaceHeaderList.Contains ( _selectedSpaceHeader ) ) {
_selectedSpaceHeader = null;
}
UpdateSpaceListDataOnUI ();
}

This handler is hooked to the checkbox in xaml...


这篇关于复选框选中n取消选中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆