RelativeSource 绑定找不到 Frame 祖先
RelativeSource binding can't find Frame ancestor
我正在尝试将我应用 Command
的实际 Frame
对象作为 CommandParameter
传递
XAML
<Frame NavigationUIVisibility="Hidden" Source="{Binding TargetContentPage}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContentRendered">
<command:EventToCommand Command="{Binding ContentRendered}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Frame}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Frame>
C#
public RelayCommand<Frame> ContentRendered
{
get
{
return new RelayCommand<Frame>(frame => frame.RemoveBackEntry());
}
}
当 运行 时,引发 NullReferenceException
说 frame
为空。
上面的代码可能有什么问题?
Frame 和 EventToCommand 之间没有 ancestor/descendent 关系。改为使用 ElementName 绑定:
<Frame x:Name="frame" ...>
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContentRendered">
<command:EventToCommand
Command="{Binding ContentRendered}"
CommandParameter="{Binding ElementName=frame}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Frame>
我正在尝试将我应用 Command
Frame
对象作为 CommandParameter
传递
XAML
<Frame NavigationUIVisibility="Hidden" Source="{Binding TargetContentPage}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContentRendered">
<command:EventToCommand Command="{Binding ContentRendered}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Frame}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Frame>
C#
public RelayCommand<Frame> ContentRendered
{
get
{
return new RelayCommand<Frame>(frame => frame.RemoveBackEntry());
}
}
当 运行 时,引发 NullReferenceException
说 frame
为空。
上面的代码可能有什么问题?
Frame 和 EventToCommand 之间没有 ancestor/descendent 关系。改为使用 ElementName 绑定:
<Frame x:Name="frame" ...>
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContentRendered">
<command:EventToCommand
Command="{Binding ContentRendered}"
CommandParameter="{Binding ElementName=frame}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Frame>