绑定IsBusy时使我的ActivityIndi​​cator运行时出现问题

时间:2020-03-26 15:20:56

标签: xaml xamarin.forms visual-studio-2019

在我的主页上,当IsBusy为true时,我正在运行一个ActivityIndi​​cator,这可以正常工作,但是当我在另一页上使用相同的配置时,它不会运行。

IsBusy驻留在我的BaseViewModel中,然后在ViewModelBase中对其进行初始化,我的代码如下...

public class ViewModelBase : BaseViewModel
{
  //Lots of other Properties
}

ViewModelBase ...

 class BaseNetWork : ViewModelBase
{
    public BaseNetWork(Grid grid, Image img, Label lbl)
    {
        BaseImages Images = new BaseImages();
        NetworkShares NetWorkData = new NetworkShares();

        img.GestureRecognizers.Add((new TapGestureRecognizer((view) => OpenShares())));
        void OpenShares()
        {

            if (IsConnected)
            {
                grid.Children.Clear();
                img.Source = Images.GetImages(4);
                lbl.Text = "Connect";
                IsConnected = false;

            } else {

                IsBusy = true; // Set the IsBusy to true for the ActivityIndicator.
                NetWorkData.DeploySharesToGrid(grid, null, ConnectToShares);
                IsConnected = true;
                if (IsConnected)
                {
                    img.Source = Images.GetImages(3);
                    lbl.Text = "Disconnect";
                };
                IsBusy = false; // Set the IsBusy back to false.
            }
        }
    }
}

页面属性...

    public partial class FolderView : ContentPage
{

    BaseImages Images = new BaseImages();
    BaseNetWork viewModel;

    public FolderView()
    {           
        InitializeComponent();
        BindingContext = viewModel = new BaseNetWork(GridFolders, btnConnect, lblNet);
    }
    void CloseEntries(object sender, EventArgs e)
    {
        if (viewModel.IsConnected)
        {
            GridFolders.Children.Clear();
            btnConnect.Source = Images.GetImages(4);
            lblNet.Text = "Connect";
            viewModel.IsConnected = false;
        }
    }
}

View.xaml.cs ...

    <ScrollView>
    <StackLayout Padding="{StaticResource PagePadding}" Spacing="{StaticResource PageSpacing}">
        <Grid RowDefinitions="{StaticResource BrowseShareRows}" ColumnDefinitions="{StaticResource BrowseShareColumns}">
            <Label x:Name="lblNet" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalTextAlignment="Start" Text="Connect"/>
            <Image x:Name="btnConnect" Grid.Row="1" Grid.Column="0" HorizontalOptions="Start" Grid.ColumnSpan="2" Source="folder_darkblue.png"></Image>
            <StackLayout  Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2">
                <controls:Checkbox x:Name="cbHello" Text="{Binding ViewMessage}" Checked="{Binding ConnectToShares}" PropertyChanged="CloseEntries"/>
            </StackLayout>
            <ScrollView Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="5">
                <StackLayout>
                    <Grid x:Name="GridFolders" Style="{StaticResource FolderStyle}" IsVisible="{Binding AllowViewGrid}"></Grid>
                </StackLayout>
            </ScrollView>
            <ActivityIndicator Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="5" Color="red" IsRunning="{Binding IsBusy}"/>
        </Grid>
    </StackLayout>
</ScrollView>

View.xaml ...

    public MainPage()
    {
        InitializeComponent();
        //ConnectivityTest();
        BindingContext = new ViewModelBase();
    }

viewthatworks.xaml.cs

    <ScrollView>
    <StackLayout Padding="{StaticResource PagePadding}" VerticalOptions="CenterAndExpand" Spacing="{StaticResource PageSpacing}">
        <Grid x:Name="controlGrid" RowDefinitions="{StaticResource MainRowStyle}" ColumnDefinitions="{StaticResource MainColumnStyle}" Style="{StaticResource MainGridStyle}">
                <Grid RowDefinitions="{StaticResource GridRowLayout3}" ColumnDefinitions="{StaticResource GridColumnLayout3}" >
                    <Entry x:Name="txtLocation" Text="{Binding GetFolder}"
                        Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="1"
                        Style="{StaticResource MainEntryStyle}"
                        Placeholder="Name"/>
                    <Button x:Name="btnSave" Text="Save Folder"
                        Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1"
                        Style="{StaticResource MainButtonStyle}"
                        IsEnabled="{Binding IsNotBusy}"
                        Command="{Binding SaveContactCommand}"/>
                    <Button x:Name="btnCreate" Text="Create"
                        Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="1"
                        Style="{StaticResource MainButtonStyle}"
                        IsEnabled="{Binding IsNotBusy}"
                        Command="{Binding CreateDir}"/>
                    <Button x:Name="btnGrid" Text="App"
                        Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="1"
                        Style="{StaticResource MainButtonStyle}"
                        Clicked="Get_Grid"/>
                </Grid>
            </StackLayout>
            <ActivityIndicator IsRunning="{Binding IsBusy}" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="6" Color="red"/>
        </Grid>   
    </StackLayout>
</ScrollView>

viewthatworks.xaml

.xlsx

任何人都可以说为什么ActivityIndi​​cator没有运行吗?

1 个答案:

答案 0 :(得分:0)

很抱歉造成误解,我没有回答这个问题,我想也许我在更新一些代码后没有按Save :-),因为对象已更新并且页面正在获取更新的属性