我有一个Label,其Content属性绑定到ViewModel(Prism / MVVM)上的字符串。当我运行此方法时,将“ pending”分配给该字符串,该值会更改,但是视图永远不会更新。但是,它确实在第二个分配(“成功”)上更新。 我摆弄着试图用RaisePropertyChanged强制进行更新而没有成功,然后尝试运行部分代码异步。那行得通!?! -但我不知道为什么。关于此问题有任何想法吗?没有异步解决方法的任何补救措施?
XAML:
<Label
Content="{Binding DbaseCreateSuccess, UpdateSourceTrigger=PropertyChanged}">
</Label>
方法同步:
private void DbaseCreate()
{
DbaseCreateSuccess = "pending";
string[] files = Directory.GetFiles("Scripts", "*.*", SearchOption.AllDirectories);
String databaseName = "testbase";
String databaseScript = "testbase.sql";
int fileCount = 0;
foreach (String file in files)
{
if (file.Contains(databaseScript))
{
DataService.Runscript(file);
}
}
foreach (String file in files)
{
FileInfo info = new FileInfo(file);
Trace.WriteLine("FileExtension: " + info.Extension);
if (info.Extension == ".sql" && !file.Contains(databaseScript))
{
++fileCount;
DataService.Runscript(file, databaseName);
}
}
if (DataService.GetTableNames(databaseName).Count == fileCount)
{
DbaseCreateSuccess = "success";
}
方法异步:
private async void DbaseCreate()
{
DbaseCreateSuccess = "pending";
string[] files = Directory.GetFiles("Scripts", "*.*", SearchOption.AllDirectories);
String databaseName = "testbase";
String databaseScript = "testbase.sql";
int fileCount = 0;
await Task.Factory.StartNew(() =>
{
foreach (String file in files)
{
if (file.Contains(databaseScript))
{
DataService.Runscript(file);
}
}
foreach (String file in files)
{
FileInfo info = new FileInfo(file);
Trace.WriteLine("FileExtension: " + info.Extension);
if (info.Extension == ".sql" && !file.Contains(databaseScript))
{
++fileCount;
DataService.Runscript(file, databaseName);
}
}
});
if (DataService.GetTableNames(databaseName).Count == fileCount)
{
DbaseCreateSuccess = "success";
}
}
ViewModel:
public class DatabaseAdminViewModel : BindableBase
{
private readonly IUnityContainer _container;
private readonly IRegionManager _regionManager;
private String _DbaseCreateSuccess;
public String DbaseCreateSuccess { get { return _DbaseCreateSuccess; } set { SetProperty(ref _DbaseCreateSuccess, value, "DbaseCreateSuccess"); } }
public DelegateCommand DbaseCreateCommand { get; set; }
public DatabaseAdminViewModel(IUnityContainer container, IRegionManager regionManager)
{
_regionManager = regionManager;
_container = container;
DbaseCreateSuccess = "idle";
DbaseCreateCommand = new DelegateCommand(DbaseCreate);
}
private async void DbaseCreate()
{
DbaseCreateSuccess = "pending";
string[] files = Directory.GetFiles("Scripts", "*.*", SearchOption.AllDirectories);
String databaseName = "testbase";
String databaseScript = "testbase.sql";
int fileCount = 0;
await Task.Factory.StartNew(() =>
{
foreach (String file in files)
{
if (file.Contains(databaseScript))
{
DataService.Runscript(file);
}
}
foreach (String file in files)
{
FileInfo info = new FileInfo(file);
Trace.WriteLine("FileExtension: " + info.Extension);
if (info.Extension == ".sql" && !file.Contains(databaseScript))
{
++fileCount;
DataService.Runscript(file, databaseName);
}
}
});
if (DataService.GetTableNames(databaseName).Count == fileCount)
{
DbaseCreateSuccess = "success";
}
}
}