2017-10-13 52 views
1

我有以下代碼,但它無法正常工作。帶Webview控件的活動指示器Xamarin表單不起作用

<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" > 

     <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" AbsoluteLayout.LayoutFlags="All" 
        AbsoluteLayout.LayoutBounds="0,0,1,1"> 
      <ScrollView VerticalOptions="FillAndExpand"> 
       <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" x:Name="webViewLayout"> 
        <WebView x:Name="webcontentcontrol" Source="{Binding WebViewSource}" VerticalOptions="FillAndExpand" HorizontalOptions="Fill"></WebView> 
       </StackLayout> 
      </ScrollView> 
     </StackLayout> 

     <StackLayout IsVisible="{Binding IsBusy}" 
      AbsoluteLayout.LayoutFlags="PositionProportional" 
      AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1"> 

      <ActivityIndicator IsRunning="{Binding IsBusy}" Style="{StaticResource ActivityIndicatorStyle}"/> 

      <Label Text="Loading..." IsVisible="{Binding IsBusy}" HorizontalOptions="Center" Style="{StaticResource ActivityIndicatorLabelStyle}"/> 

     </StackLayout> 
    </AbsoluteLayout> 

銫文件代碼是

public AnnouncementPage() 
    { 
     InitializeComponent(); 
     BindWebViewControl(); 
    } 

    protected override void OnAppearing() 
    { 
     webViewLayout.IsVisible = false; 
     IsBusy= true; 
    } 
    private async void BindWebViewControl() 
    {    
     var result = await _dataService.GetAnnouncement(); 
     webcontentcontrol.Source = result.Data.First().WebViewSource; 
     IsBusy = false; 
     webViewLayout.IsVisible = true; 
    } 

的問題是,標籤「正在加載中...」沒有隱瞞,和活動指示燈不顯示在屏幕上。

+0

是使用INotifyPropertyChanged接口? –

+0

是的,我已經實施 –

+0

,如果你使用綁定上下文viewmodel,那麼你需要在你的viewmodel中聲明你的isBusy變量並在xaml.cs中設置Viewmodel.isBusy,但你也可以做簡單的方法來查看我的答案。 –

回答

1

如下更改您的代碼,並嘗試希望它的幫助:

<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" > 

     <StackLayout x:Name="stkWebview" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" AbsoluteLayout.LayoutFlags="All" 
        AbsoluteLayout.LayoutBounds="0,0,1,1"> 
      <ScrollView VerticalOptions="FillAndExpand"> 
       <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" x:Name="webViewLayout"> 
        <WebView x:Name="webcontentcontrol" Source="{Binding WebViewSource}" VerticalOptions="FillAndExpand" HorizontalOptions="Fill"></WebView> 
       </StackLayout> 
      </ScrollView> 
     </StackLayout> 

     <StackLayout x:Name="stkLoading" 
      AbsoluteLayout.LayoutFlags="PositionProportional" 
      AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1"> 

      <ActivityIndicator IsRunning="true" Style="{StaticResource ActivityIndicatorStyle}"/> 

      <Label Text="Loading..." HorizontalOptions="Center" Style="{StaticResource ActivityIndicatorLabelStyle}"/> 

     </StackLayout> 
    </AbsoluteLayout> 

後面的代碼看起來象下面這樣:

public AnnouncementPage() 
    { 
     InitializeComponent(); 
     BindWebViewControl(); 
    } 

    protected override void OnAppearing() 
    { 
     stkWebview.IsVisible = false; 
     stkLoading.IsVisible = true; 
    } 
    private async void BindWebViewControl() 
    {    
     var result = await _dataService.GetAnnouncement(); 
     webcontentcontrol.Source = result.Data.First().WebViewSource; 
     stkWebview.IsVisible = true; 
     stkLoading.IsVisible = false; 
    } 
+0

謝謝你...它工作得很好。 –