C# System.Windows.Controls.UserControl在';卸载';
我发现了一个奇怪的现象,当父窗口的内容控件从当前控件更改为新控件时,UserControl加载的事件会触发 我已经在多个用户控件上测试了这种行为,并且在所有用户控件上都发生了这种情况 我所做的:C# System.Windows.Controls.UserControl在';卸载';,c#,wpf,mvvm,user-controls,interaction,C#,Wpf,Mvvm,User Controls,Interaction,我发现了一个奇怪的现象,当父窗口的内容控件从当前控件更改为新控件时,UserControl加载的事件会触发 我已经在多个用户控件上测试了这种行为,并且在所有用户控件上都发生了这种情况 我所做的: <xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"/> <Controls:TransitioningContentControl Transition="RightReplace
<xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"/>
<Controls:TransitioningContentControl Transition="RightReplace" Content="{Binding CurrentViewModel}"/>
<xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Load, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
窗口:
<xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"/>
<Controls:TransitioningContentControl Transition="RightReplace" Content="{Binding CurrentViewModel}"/>
<xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Load, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
当我更改CurrentViewModel属性时,相应的视图将加载到内容控件中
UserControl:
<xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"/>
<Controls:TransitioningContentControl Transition="RightReplace" Content="{Binding CurrentViewModel}"/>
<xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Load, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
加载UserControl
时(由于某种原因,当UserControl更改为另一个时),将运行Load方法
可能的解决方案:
<xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"/>
<Controls:TransitioningContentControl Transition="RightReplace" Content="{Binding CurrentViewModel}"/>
<xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Load, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
有一些方法可以解决这个问题,例如,我可以在Load方法中使用if
公式创建一个名为ShouldLoad
的布尔属性,但是这对于我试图实现的目标来说似乎相当复杂
我觉得这种行为不应该发生,但可能有一种解释…这对我帮助很大。。。事实证明,加载的事件在许多情况下都会被触发,例如在更改选项卡控件时
我目前在我的应用程序中也遇到了这个问题 到目前为止,我发现它和
有关,它是如何使用的,或者它放在哪里。我将UI更改为仅使用
,多个加载的事件不再发生在我身上。对于
,它似乎也能按预期工作