【问题标题】:Xamarin Forms ActivityIndicator UWP Always RunningXamarin Forms ActivityIndi​​cator UWP 始终运行
【发布时间】:2017-11-20 18:53:54
【问题描述】:

我对 Xamarin UWP 项目中的 ActivityIndicator 有疑问。指标一直在运行。我必须设置属性IsVisible 来隐藏指示器。我想在ActivityIndicator 上执行特定于平台的条件,并仅在平台为 Windows 时设置属性IsVisible

这是我尝试过的:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MyApp.Views.LoginPage">
<StackLayout Padding="10" Spacing="10">
    <Label Text="User" />
    <Entry Text="{Binding Email}" Placeholder="User" />
    <Label Text="Pass" />
    <Entry Text="{Binding Password}" Placeholder="Pass" />
    <Button Text="Autentificare" />
    <ActivityIndicator IsRunning="{Binding IsBusy}">
        <OnPlatform x:TypeArguments="x:Boolean">
            <On Platform="Windows" Value="IsVisible">{Binding IsBusy}</On>
        </OnPlatform>
    </ActivityIndicator>
</StackLayout>
</ContentPage>

我尝试使用OnPlatform 属性,但我不知道如何正确使用。有什么想法吗?

【问题讨论】:

    标签: xamarin uwp xamarin.forms uwp-xaml


    【解决方案1】:

    我已经测试了您的代码并重现了您的问题。您可以从source code 找到原因。

    void UpdateIsRunning()
    {
    Control.ElementOpacity = Element.IsRunning ? Element.Opacity : 0;
    }
    

    IsRunning 属性只是设置透明度的条件 本机 Control 而不是更改本机控制的 Active 属性。但它没有按预期工作。我会将此问题报告给相关团队。目前有一种解决方法。您可以将IsBusy 绑定到IsVisibleIsRunning,如下所示。

    <ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}"/>
    

    更新

    您可以创建继承ActivityIndicatorCustomActivityIndicator 类。然后在本机客户端项目中为其实现自定义渲染器。更多内容请参考以下代码。

    CustomActivityIndi​​cator.cs

    public class CustomActivityIndicator : ActivityIndicator
    {
         public CustomActivityIndicator()
         {
    
         }
    
    }
    

    CustomActivityIndi​​catorRenderer.cs

    [assembly: ExportRenderer(typeof(CustomActivityIndicator), typeof(CustomActivityIndicatorRenderer))]
    
    namespace XamarinActivatorTest.UWP
    {
        public class CustomActivityIndicatorRenderer : ActivityIndicatorRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<ActivityIndicator> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    UpdateStatus();
                }
            }
            protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                base.OnElementPropertyChanged(sender, e);
                if (e.PropertyName == nameof(Element.IsRunning))
                {
                    UpdateStatus();
                }
            }
            private void UpdateStatus()
            {
                Control.ShowPaused = !Element.IsRunning;
                Control.Opacity = Element.IsRunning ? 1 : 0;
            }
        }
    }
    

    您可以直接绑定IsRunning 属性。因为IsRunningproperty 的功能在您的自定义渲染器中发生了更改。

    <StackLayout Padding="10" Spacing="10">
        <Button Text="Autentificare"  Clicked="Button_Clicked"/>
        <local:CustomActivityIndicator IsRunning="{Binding IsBusy}" >
        </local:CustomActivityIndicator>
    </StackLayout>
    

    我已将code sample 上传到 git hub。

    【讨论】:

    • 是的,这就是我所做的。但我不想对所有设备都使用 hack。我想把这个属性 IsVisible 只用于 UWP 设备。这可能吗?
    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2018-02-14
    • 2017-11-18
    相关资源
    最近更新 更多