BusyIndicator is not visible?
本文关键字:visible not is BusyIndicator | 更新日期: 2023-09-27 18:20:36
我正在做一些操作,比如读取csv文件并转换为对象。除了BusyIndicator不可见之外,一切都很好,即使BusyIndicator也不可见。Isbusy=true;
所有的东西都在主线程上运行,所以我想当我读取文件时,UI或主线程可能会很忙,因为它不可见。
代码:
private void ImportData(Dictionary<string, ImportFieldMap> mappedItems)
{
var fileBytes = fileBrowseCtrl.FileBytes;
var getDelimiter = string.IsNullOrEmpty(txeSeperator.Text) ? UtilFunctions.GetDefaultDeLimiter() : txeSeperator.Text.ToCharArray()[0];
if (fileBytes == null)
{
MessageBox.Show(Uniconta.ClientTools.Localization.lookup("NoFilesSelected"), Uniconta.ClientTools.Localization.lookup("Error"), MessageBoxButton.OK);
return;
}
Encoding Coding = Encoding.Unicode;
if (ANSICodePage.IsUTF8(fileBytes))
Coding = Encoding.UTF8;
else if (ANSICodePage.IsANSI(fileBytes))
{
fileBytes = ANSICodePage.Convert2Unicode(fileBytes);
Coding = Encoding.Unicode;
}
try
{
busyIndicator.IsBusy = true;
CSVHelper csvData;
using (var reader = new StreamReader(new MemoryStream(fileBytes), Coding, true))
{
csvData = CSVHelper.Load(reader, getDelimiter);
}
//Converting to UnicontaObject Code...
}
catch
{
MessageBox.Show(Uniconta.ClientTools.Localization.lookup("InvalidFileFormat"), Uniconta.ClientTools.Localization.lookup("Error"), MessageBoxButton.OK);
fileBrowseCtrl.ResetControl();
}
finally
{
busyIndicator.IsBusy = false;
}
}
有没有一种方法可以在不同的线程上单独运行,这样当调用函数时,UI会显示busyIndicator,并且在后台进行读取csv和转换为对象的操作
我已经尝试过使用BackgroundWorkerThread,但它是一个非程序,所以有其他方法可以实现吗?
关于
1。多线程
1.1背景工人很好。
1.2.另一种选择是使用Dispatcher
每个WPF组件都有一个Dispatcher
,可以用来返回GUI线程:
// Get the dispatcher on the Gui thread
Dispatcher dispatcher = window.Dispatcher;
// use it on the worker thread to get back to GUI thread
dispatcher.Invoke(
() =>{
busyIndicator.IsBusy = true;
);
2.更新GUI
GUI中没有更改的原因可能是GUI属性没有引发任何事件。
public bool IsBusy{
get{ return isBusy; }
set{
isBusy = value;
FirePropertyChange();
}
}
并且INotifyPropertyChanged
应该在具有IsBusy属性的类上实现:
public class User : IDataErrorInfo, INotifyPropertyChanged
{
private void OnNotifyPropertyChange([CallerMemberName]string propName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged;
问候