首页 文章

Xamarin问题,如果将属性更改为之前的属性,则不会更改属性

提问于
浏览
0

所以我试图弄清楚为什么会这样,以及为什么我尝试的没有 . 我有一个使用回收缓存策略的ListView . 我们有一个设置,用户可以切换以使其主题变暗或变亮 . 因此,当在黑暗主题上,并且您单击其中一个单元格时,它具有此突出显示功能,可将单元格的背景颜色更改为灰色 . 这必须是一些默认行为,但Xamarin无法禁用此功能 . Anywho,为了解决这个问题,在我的ViewCell逻辑中,我设置了一个委托,将背景颜色更改为黑色(如果是黑暗主题)或白色(如果它是浅色主题) .

因此:

this.Tapped += (s, e) => {
    this.View.BackgroundColor = App.IsDarkThemeEnabled() ? Xamarin.Forms.Color.Black : Xamarin.Forms.Color.White;
};

这工作....第一次 . 第一次单击一个单元格时,它会正确地将背景颜色更改为黑色或白色,从而覆盖默认情况下xamarin提供的高亮显示功能(但无法将其关闭) . 但是,如果我返回相同的网格,并单击相同的单元格 . 它没有改变背景颜色!震惊了,我在代表中放了一个断点,而且很公平,它被击中但没有执行 .

完全混淆了这意味着什么,我幸运地决定做一个简单的测试:

int x = 1;
//make sure to not have a highlight affect when tapping on a cell.
this.Tapped += (s, e) => {
    //this.View.BackgroundColor = App.IsDarkThemeEnabled() ? Xamarin.Forms.Color.Transparent : Xamarin.Forms.Color.White;
    if (x == 1) {
        this.View.BackgroundColor = App.IsDarkThemeEnabled() ? Xamarin.Forms.Color.Black : Xamarin.Forms.Color.White;
        x++;
    } else
        this.View.BackgroundColor = App.IsDarkThemeEnabled() ? Xamarin.Forms.Color.Blue : Xamarin.Forms.Color.White;
};

这基本上只是第一次是黑/白,而在此之后的任何时候都是蓝/白 . 瞧!有效 . 第一次变黑,第二次变成蓝色 . 意识到这一点后,我只是将我的代码更改为:

this.Tapped += (s, e) => {
    this.View.BackgroundColor = App.IsDarkThemeEnabled() ? Xamarin.Forms.Color.Transparent : Xamarin.Forms.Color.White;
    this.View.BackgroundColor = App.IsDarkThemeEnabled() ? Xamarin.Forms.Color.Black : Xamarin.Forms.Color.White;
};

这很有效 . 所以它基本上没有实际改变属性,如果它与以前相同的 Value ?非常奇怪,但是想要澄清为什么会发生这种情况?这是一个Xamarin bug吗?

1 回答

  • 0

    误解的来源可以在你的评论中找到

    虽然我明白为什么会这样,但Xamarin将该 properties 变为灰色 .

    BackgroundColor 属性不是't changed or modified on Tap by the Xamarin.Forms platform at all. The visual representation is changed, but not the property. The question that remains is why the defined background color (black or white) isn' t用于点击 . 根据提供的信息,我不知道这是您的代码还是XF中的问题 . 你应该提交一个最小的repro案例的bug .

相关问题