You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin UWP应用中检测Label元素文本变化并修改对应图标颜色

错误原因说明

你之前绑定的ContentPage.PropertyChanged事件只会监听页面自身属性(比如Title、BindingContext等)的变更,既不会触发列表子元素的属性变更通知,也不会捕获绑定数据源DriveItem内部的属性变更,所以无法匹配到Level的变更事件。

推荐实现方案(符合MVVM规范,无额外事件监听)

直接使用多值转换器在绑定层处理颜色逻辑,一次性解决你遇到的三个问题:

  1. 仅针对目标检出状态Label生效
  2. 可直接获取关联的文件名
  3. 无需手动操作控件属性,通过绑定自动修改颜色

步骤1:实现多值转换器

using System.Globalization;
using Xamarin.Forms;
using System.IO;

namespace GraphTutorial.Models
{
    public class CheckoutStatusColorConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            // 校验传入的两个参数:文件名、检出状态
            if (values.Length != 2 || values[0] is not string fileName || values[1] is not string level)
                return Color.Black; // 返回默认颜色
            
            // 非检出状态直接返回默认颜色
            if (level != "checkout") 
                return Color.Black;
            
            // 调用本地令牌搜索逻辑,判断是否为当前用户检出
            bool hasMatchingToken = CheckLocalTokenForFile(fileName);
            return hasMatchingToken ? Color.Red : Color.Black;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }

        // 替换为你实际的本地令牌查询逻辑
        private bool CheckLocalTokenForFile(string fileName)
        {
            string tokenPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), 
                $"{fileName}.token");
            return File.Exists(tokenPath);
        }
    }
}

步骤2:修改XAML代码

首先在页面资源中注册转换器,然后修改检出状态Label的TextColor绑定逻辑:

<?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:GraphTutorial.Models"
             Title="Shared Document Library"
             x:Class="GraphTutorial.SPDocumentLibraryContentsPage">
    <!-- 注册转换器 -->
    <ContentPage.Resources>
        <local:CheckoutStatusColorConverter x:Key="CheckoutColorConverter" />
    </ContentPage.Resources>

    <!-- 原有列表项模板部分 -->
    <Label Grid.Column="0" Text="{Binding Path=DriveItem.Name}" FontSize="Small" />
    <Label Grid.Column="1" 
           Text="{Binding Path=DriveItem.Publication.Level,Converter={StaticResource IconValueConverter}}" 
           FontFamily="Segoe MDL2 Assets" 
           FontSize="Small">
        <!-- 多值绑定颜色 -->
        <Label.TextColor>
            <MultiBinding Converter="{StaticResource CheckoutColorConverter}">
                <Binding Path="DriveItem.Name" />
                <Binding Path="DriveItem.Publication.Level" />
            </MultiBinding>
        </Label.TextColor>
    </Label>
</ContentPage>

替代方案(事件监听实现)

如果你坚持要用事件监听的方式实现,可以通过给Label附加Behavior的方式监听文本变更:

实现Behavior类

using Xamarin.Forms;

namespace GraphTutorial.Models
{
    public class CheckoutStatusBehavior : Behavior<Label>
    {
        protected override void OnAttachedTo(Label bindable)
        {
            base.OnAttachedTo(bindable);
            bindable.PropertyChanged += OnCheckoutLabelChanged;
        }

        protected override void OnDetachingFrom(Label bindable)
        {
            base.OnDetachingFrom(bindable);
            bindable.PropertyChanged -= OnCheckoutLabelChanged;
        }

        private void OnCheckoutLabelChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender is not Label label || e.PropertyName != nameof(Label.Text))
                return;
            
            // 从绑定上下文获取关联的文件信息
            if (label.BindingContext is not YourItemViewModel item) // 替换为你实际的列表项ViewModel类型
                return;

            string fileName = item.DriveItem.Name;
            string level = item.DriveItem.Publication.Level;

            if (level == "checkout" && CheckLocalTokenForFile(fileName))
            {
                label.TextColor = Color.Red;
            }
        }

        // 同上的本地令牌搜索逻辑
        private bool CheckLocalTokenForFile(string fileName)
        {
            // 实现你的逻辑
        }
    }
}

XAML中附加Behavior

<Label Grid.Column="1" 
       Text="{Binding Path=DriveItem.Publication.Level,Converter={StaticResource IconValueConverter}}" 
       FontFamily="Segoe MDL2 Assets" 
       FontSize="Small">
    <Label.Behaviors>
        <local:CheckoutStatusBehavior />
    </Label.Behaviors>
</Label>

内容的提问来源于stack exchange,提问作者dot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 14:45:04