【问题标题】:Xamarin Forms - Change Cursor to Wait CursorXamarin Forms - 将光标更改为等待光标
【发布时间】:2018-04-08 15:37:46
【问题描述】:

在大多数桌面应用程序中,如果您在等待某事,鼠标光标将变为等待光标。显然,手机有点不同。但是,在 UWP 领域,我们希望鼠标光标在异步操作发生时变为等待光标。

有没有办法在标准 Xamarin 表单中执行此操作? 或者,使用 3rd 方库?

希望在手机上,会有一些其他视觉指示器表明正在发生某些事情。 IE。键盘颜色变化或类似的东西。

如果没有,我猜我必须为每个平台实现一些依赖注入代码。有什么建议吗?

【问题讨论】:

    标签: c# android xamarin.forms uwp cursor


    【解决方案1】:

    无法回答您的UWP 问题,但这是您可以通过电话处理事情的方式:

    您可以在整个页面上添加Grid 并将其设置为灰色和半透明,以便页面的其余部分“变灰”。然后将ActivityIndicator 添加到Grid 的中间以指示正在加载某些内容。将Grid 中的IsVisible 设置为一个布尔值,表明某事正在加载/正在发生。

    这是我在我的一个页面上得到的:

    <Grid BackgroundColor="##60000000"
            IsVisible="{Binding IsLoading}">
        <Grid VerticalOptions="CenterAndExpand">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width=".5*" />
                <ColumnDefinition Width="9*" />
                <ColumnDefinition Width=".5*" />
            </Grid.ColumnDefinitions>
            <Frame Grid.Column="1"
                    HorizontalOptions="Center"
                    VerticalOptions="Center"
                    Padding="0"
                    HasShadow="True"
                    OutlineColor="#e6e6e6">
                <Grid BackgroundColor="White"
                        RowSpacing="0"
                        ColumnSpacing="0">
                    <StackLayout>
                        <ActivityIndicator IsRunning="true"
                                            Margin="10"
                                            HorizontalOptions="Center"
                                            VerticalOptions="Center" />
                        <Label Text="{i18n:Translate Text=loading_contacts}"
                                Margin="20,0,20,10"
                                HorizontalOptions="Center"
                                VerticalOptions="Center"/>
                    </StackLayout>     
                </Grid>
            </Frame>
        </Grid>
    </Grid>
    

    【讨论】:

    • 谢谢。我相信你的想法会奏效,但它没有抓住重点。我要做的就是在我所在的任何页面上更改光标。我根本不想玩视觉树。
    【解决方案2】:

    我的答案与 Dennis 类似,但重复使用更多。

    我在 App.xaml 中创建了一个控件模板

       <ControlTemplate x:Key="ActivityIndicatorTemplate">
            <Grid>
                <ContentPresenter />
                <StackLayout Style="{StaticResource BlockingPanel}"
                             IsVisible="{TemplateBinding BindingContext.IsBusy}">
                    <ActivityIndicator Style="{StaticResource ActivityIndicatorStyle}"
                                       IsVisible="{TemplateBinding BindingContext.IsBusy}"
                                       IsRunning="{TemplateBinding BindingContext.IsBusy}" />
                </StackLayout>
            </Grid>
        </ControlTemplate>
    

    这里是样式

        <Style x:Key="BlockingPanel" TargetType="StackLayout">
            <Setter Property="BackgroundColor" Value="{StaticResource BlockingColor}" />
            <Setter Property="HorizontalOptions" Value="FillAndExpand" />
            <Setter Property="VerticalOptions" Value="FillAndExpand" />
        </Style>
    
        <Style x:Key="ActivityIndicatorStyle" TargetType="ActivityIndicator">
            <Setter Property="Color" Value="White" />
            <Setter Property="HorizontalOptions" Value="CenterAndExpand" />
            <Setter Property="VerticalOptions" Value="CenterAndExpand" />
        </Style>
    
       <Color x:Key="BlockingColor">
            <x:Arguments>
                <x:Double>0</x:Double>
                <x:Double>0</x:Double>
                <x:Double>0</x:Double>
                <x:Double>0.75</x:Double>
            </x:Arguments>
        </Color>
    

    然后你可以在任何这样的页面上使用它

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Incident.Pages.LoginPage"
                 ControlTemplate="{StaticResource ActivityIndicatorTemplate}"
                 Title="Login">
    
    ...
    
    </ContentPage>
    

    【讨论】:

    • 谢谢。同样,这将起作用,但我正在编写一个对可视树一无所知的方法。我真的只是想要一些可以改变光标的东西。
    • 在这种情况下,在 UWP 平台项目中编写 UWP 特定代码,例如 Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 0) ;并使用依赖服务从共享项目中调用它
    • 我希望避免这样做。我希望 Xamarin 会编写一个库来执行此操作。
    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2012-03-29
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    相关资源
    最近更新 更多