繁体   English   中英

Xamarin在点击时设置StackLayout的BackgroundColor

[英]Xamarin set BackgroundColor of StackLayout on tapped

StackLayout sl1 = this.FindByName<StackLayout>("myStackLayout");
sl1.GestureRecognizers.Add(
    new TapGestureRecognizer() {
        Command = new Command(() => { 

              Task.Run(() => { 

                 // When this line hits, background is set...  
                 sl1.BackgroundColor = Color.FromHex("CCCCCC");



                 //this.Refresh(); << force Refresh UI function or something????

                 Task.Delay(400);

                 // When this line hits, background is reset...  
                 sl1.BackgroundColor = Color.FromHex("EEEEEE");

              });


        }) 
   });

当我逐行调试此代码时,以上代码按预期工作。

但是,当我在不调试的情况下运行此代码时,UI不会更新BackgroundColor。

然后,当我尝试调试以查看发生了什么时,它似乎起作用了。

  • 为什么没有调试就无法正常工作?
  • 是否存在强制UI更新的功能?
  • 还有其他想法可以达到同样的效果吗?

编辑:

第一次也可以。

编辑2(解决方案)

使用两个答案的组合,我可以使用以下代码来工作:

        StackLayout sl1 = this.FindByName<StackLayout>("myStackLayout");
        sl1.GestureRecognizers.Add(
        new TapGestureRecognizer()
        {
            Command = new Command(async () =>
            {
                sl1.BackgroundColor = Color.FromHex("#CCCCCC");

                await Task.Run(async () => { 
                    await Task.Delay(100);
                    Device.BeginInvokeOnMainThread(() => { 
                        sl1.BackgroundColor = Color.FromHex("#EEEEEE");
                    });
                });

            })
        });

您正在尝试从后台线程更新UI,并且所有UI更改都应在主线程上完成。

Device.BeginInvokeOnMainThread(() =>
{
    sl1.BackgroundColor = Color.FromHex("CCCCCC");
}); 

刚刚使用一个快速的应用程序对此进行了测试,并且可以正常工作:

MainPage.xaml.cs中

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        StackLayout sl1 = this.FindByName<StackLayout>("myStackLayout");
        sl1.GestureRecognizers.Add(
            new TapGestureRecognizer()
            {
                Command = new Command(async () => {

                    // When this line hits, background is set...  
                    sl1.BackgroundColor = Color.FromHex("#e50000");



                    //this.Refresh(); << force Refresh UI function or something????

                    await Task.Delay(400);

                    // When this line hits, background is reset...  
                    sl1.BackgroundColor = Color.FromHex("#0be204");

                })
            });
    }
}

MainPage.xaml中

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:test"
             x:Class="test.MainPage">

    <StackLayout x:Name="myStackLayout" BackgroundColor="RoyalBlue">
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" BackgroundColor="Blue" />
    </StackLayout>

</ContentPage>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM