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

WinUI3数据模板对象中Interaction.Behaviors失效问题求助

问题:Uno Platform中数据模板内的Interaction.Behaviors命令多次调用后失效

在Uno Platform(WinUI/.NET6)项目中,为TabViewItem、TreeViewItem和ListViewItem配置了多数据模板,通过ItemInvoked(TreeView)、DoubleTapped(ListView)、CloseRequested(TabView)等事件,借助Interaction.Behaviors绑定命令触发操作。但出现异常:这些命令在调用几次后就会失效,但相同的命令通过Button的Command绑定却能持续正常工作。

复现示例(TabView关闭操作)

通常前3-5个标签页可正常响应CloseRequested事件绑定的命令,之后失效:

<DataTemplate x:Key="HtmlFileTemplate" x:DataType="local:FileContentViewModel">
    <TabViewItem Header="{x:Bind Info.Name}">
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{x:Bind Content}" />
            <!-- 该按钮绑定的命令始终正常工作 -->
            <Button Command="{x:Bind CloseCommand}">Invoke FileContentViewModel.CloseCommand</Button>
        </StackPanel>
        <!-- 该行为绑定的命令在几次调用后失效 -->
        <i:Interaction.Behaviors>
            <ic:EventTriggerBehavior EventName="CloseRequested">
                <ic:InvokeCommandAction Command="{x:Bind CloseCommand}" />
            </ic:EventTriggerBehavior>
        </i:Interaction.Behaviors>
    </TabViewItem>
</DataTemplate>

<local:TabsTemplateSelector x:Key="TabsTemplateSelector" HtmlFileTemplate="{StaticResource HtmlFileTemplate}" ... />
...

<TabView TabItemsSource="{x:Bind ViewModel.Tabs}" TabItemTemplateSelector="{StaticResource TabsTemplateSelector}">
</TabView>

使用环境配置

  • 项目初始化:dotnet new unoapp -o UnoWinUI3AppName
  • 依赖包版本:
    • CommunityToolkit.Mvvm 7.1.2
    • Microsoft.WindowsAppSDK 1.1.3
    • Microsoft.Windows.SDK.BuildTools 10.0.22621.1
    • Uno.Microsoft.Xaml.Behaviors.Interactivity.WinUI 2.3.0
    • Uno.Microsoft.Xaml.Behaviors.WinUI.Managed 2.3.0

临时解决方案:自定义附加属性绑定行为

通过自定义附加属性的方式绑定行为,可规避命令失效问题:

C# 附加属性实现

public static class InteractionEx
{
    public static readonly DependencyProperty AttachBehaviorsProperty = DependencyProperty.RegisterAttached
    (
        "AttachBehaviors",
        typeof(object),
        typeof(FrameworkElement),
        new PropertyMetadata(false, AttachBehaviorsChanged)
    );

    public static object GetAttachBehaviors(DependencyObject o) => o.GetValue(AttachBehaviorsProperty);

    public static void SetAttachBehaviors(DependencyObject o, object value) => o.SetValue(AttachBehaviorsProperty, value);


    private static void AttachBehaviorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var behaviors = e.NewValue switch
        {
            Behavior single => new BehaviorCollection { single },
            BehaviorCollection multiple => multiple,
            _ => null,
        };

        Interaction.SetBehaviors(d, behaviors);
    }
}

XAML 使用示例

<ListView ItemsSource="{x:Bind Items}" ItemTemplateSelector="{StaticResource ListItemTemplateSelector}" SelectedItem="{x:Bind SelectedItem, Mode=TwoWay}">
    <local:InteractionEx.AttachBehaviors>
        <interactivity:BehaviorCollection>
            <core:EventTriggerBehavior EventName="DoubleTapped">
                <core:InvokeCommandAction Command="{x:Bind OpenCommand}" />
                <core:CallMethodAction MethodName="Open" TargetObject="{x:Bind}" />
            </core:EventTriggerBehavior>
        </interactivity:BehaviorCollection>
    </local:InteractionEx.AttachBehaviors>
</ListView>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 10:33:17