Xaml 按下Popupp内的按钮后未设置ActivityIndicator
我正在尝试在使用Xaml 按下Popupp内的按钮后未设置ActivityIndicator,xaml,xamarin.forms,activity-indicator,Xaml,Xamarin.forms,Activity Indicator,我正在尝试在使用Xamarin.Forms创建的pouppage中运行activityindicator。代码如下: C# XAML: <pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:pages="clr-namespace:
Xamarin.Forms创建的pouppage
中运行activityindicator
。代码如下:
C#
XAML:
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
x:Class="OPC_UA_Client.LoginPopupPage">
<ScrollView x:Name="Scroll"
HorizontalOptions="Center"
VerticalOptions="Center">
<AbsoluteLayout x:Name="Absolute">
<Frame x:Name="FrameContainer"
Margin="15"
HorizontalOptions="Center"
BackgroundColor="White">
<StackLayout Padding="25, 5">
<Label Text="Username: "
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
TextColor="#212121"/>
<Entry WidthRequest="25"
HorizontalOptions="FillAndExpand"
x:Name="UsernameEntry"
Placeholder="Username" />
<Label Text="Password: "
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
TextColor="#212121"/>
<Entry HorizontalOptions="FillAndExpand"
x:Name="PasswordEntry"
Placeholder="Password"
IsPassword="True"/>
<Button Margin="10, 5"
BackgroundColor="#FF9800"
HorizontalOptions="Fill"
Clicked="OnLoginButton"
x:Name="LoginButton"
TextColor="White"
Text="Login">
<Button.HeightRequest>
<OnPlatform x:TypeArguments="x:Double"
Android="50"
iOS="30"
WinPhone="30"/>
</Button.HeightRequest>
</Button>
<ActivityIndicator x:Name="LoadingIndicator"
IsRunning="False"
BackgroundColor="Aqua"
IsVisible="true"/>
</StackLayout>
</Frame>
</AbsoluteLayout>
</ScrollView>
</pages:PopupPage>
当我按下login按钮时
,我希望看到AcitivityIndicator
正在运行,但这不会发生。有什么问题吗?谢谢你的帮助,很抱歉我的英语不好。你现在的代码应该可以用了,我已经试过了。可能有多种原因导致它无法显示正在运行的ActivityIndicator
:
- 您的
async
代码完成得如此之快,以至于ActivityIndicator.IsRunning
在您看到它运行之前就被设置为false。注释除LoadingIndicator.IsRunning=true之外的所有内容代码>输出以查看是否是这种情况
- 调用更改主线程中用户界面的代码:
尽管如此,您还是应该在主线程上调用它,因为它更改了您的UI,所有UI更改都应该在主线程上调用。谢谢您的回答,我解决了我的问题。异步代码太快了,就像你在回答中说的那样。但是并行运行一个新任务,现在我得到了我想要的。
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
x:Class="OPC_UA_Client.LoginPopupPage">
<ScrollView x:Name="Scroll"
HorizontalOptions="Center"
VerticalOptions="Center">
<AbsoluteLayout x:Name="Absolute">
<Frame x:Name="FrameContainer"
Margin="15"
HorizontalOptions="Center"
BackgroundColor="White">
<StackLayout Padding="25, 5">
<Label Text="Username: "
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
TextColor="#212121"/>
<Entry WidthRequest="25"
HorizontalOptions="FillAndExpand"
x:Name="UsernameEntry"
Placeholder="Username" />
<Label Text="Password: "
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
TextColor="#212121"/>
<Entry HorizontalOptions="FillAndExpand"
x:Name="PasswordEntry"
Placeholder="Password"
IsPassword="True"/>
<Button Margin="10, 5"
BackgroundColor="#FF9800"
HorizontalOptions="Fill"
Clicked="OnLoginButton"
x:Name="LoginButton"
TextColor="White"
Text="Login">
<Button.HeightRequest>
<OnPlatform x:TypeArguments="x:Double"
Android="50"
iOS="30"
WinPhone="30"/>
</Button.HeightRequest>
</Button>
<ActivityIndicator x:Name="LoadingIndicator"
IsRunning="False"
BackgroundColor="Aqua"
IsVisible="true"/>
</StackLayout>
</Frame>
</AbsoluteLayout>
</ScrollView>
</pages:PopupPage>
private async void OnLoginButton(object sender, EventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
LoadingIndicator.IsRunning = true;
});
// code...
}