$Users = @(
"User1"
"User2"
)
$DomainControllers = @(
"DC1"
"DC2"
)
Foreach ($User in $Users){
Foreach ($Controller in $DomainControllers){
$Data = Get-ADUser $User -Properties "LastLogonDate" -Server $Controller | Select LastLogonData
}
$Data = $null
}
当我逐行调试此代码时,以上代码按预期工作。
但是,当我在不调试的情况下运行此代码时,UI不会更新BackgroundColor。
然后,当我尝试调试以查看发生了什么时,它似乎可以工作。
编辑:
第一次也可以。
编辑2(解决方案):
使用两个答案的组合,我可以使用以下代码来工作:
StackLayout sl1 = this.FindByName<StackLayout>("myStackLayout");
sl1.GestureRecognizers.Add(
new TapGestureRecognizer() {
Command = new Command(() => {
Task.Run(() => {
// When this line hits, background is set...
sl1.BackgroundColor = Color.FromHex("CCCCCC");
//this.Refresh(); << force Refresh UI function or something????
Task.Delay(400);
// When this line hits, background is reset...
sl1.BackgroundColor = Color.FromHex("EEEEEE");
});
})
});
答案 0 :(得分:0)
只需使用一个快速的应用程序对其进行测试,即可成功:
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
StackLayout sl1 = this.FindByName<StackLayout>("myStackLayout");
sl1.GestureRecognizers.Add(
new TapGestureRecognizer()
{
Command = new Command(async () => {
// When this line hits, background is set...
sl1.BackgroundColor = Color.FromHex("#e50000");
//this.Refresh(); << force Refresh UI function or something????
await Task.Delay(400);
// When this line hits, background is reset...
sl1.BackgroundColor = Color.FromHex("#0be204");
})
});
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:test"
x:Class="test.MainPage">
<StackLayout x:Name="myStackLayout" BackgroundColor="RoyalBlue">
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" BackgroundColor="Blue" />
</StackLayout>
</ContentPage>
答案 1 :(得分:0)
您正在尝试从后台线程更新UI,并且所有UI更改都应在主线程上完成。
Device.BeginInvokeOnMainThread(() =>
{
sl1.BackgroundColor = Color.FromHex("CCCCCC");
});