我的FlipView
中有一个MainPage
。它是ItemTemplate
绑定到名为UserControl
的{{1}}。它是landscapeControl
绑定到名为ItemsSource
的类的列表。
landscapeControl:
MyLandscape
<Grid>
<ScrollViewer x:Name="LScrollViewer" MaxZoomFactor="2.0" MinZoomFactor="1.0" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" DoubleTapped="LScrollViewer_DoubleTapped" >
<Canvas x:Name="inkCanvas" Background="Transparent">
<StackPanel x:Name="LStackPanel" Orientation="Horizontal" Margin="0,0,0,0">
<Image x:Name="LImage0" HorizontalAlignment="Right" Source="{Binding firstImage}" Width="570"/>
<Image x:Name="LImage1" HorizontalAlignment="Left" Source="{Binding nextImage}" Width="570"/>
</StackPanel>
</Canvas>
</ScrollViewer>
</Grid>
上课:
MyLandscape
我的照片完美呈现。我想要的就是三件事:
1)我想从我的 public class MyLandscape
{
public ImageSource firstImage { get; set; }
public ImageSource nextImage { get; set; }
public Canvas inkCanvas { get; set; }
}
访问我的Canvas
。我想在MainPage
事件中执行此操作:
flipView_SelectionChanged
但是pc变量总是为空!我想绑定 landscapeControl pc = flipView1.SelectedItem as landscapeControl;
if (flipView1.Items.Count > 0)
{
var myCanvas = pc.getCanvas();
m_CanvasManager = new CanvasManager(myCanvas);
}
,所以每两张图片都有一个Canvas
?
那可能吗?
答案 0 :(得分:1)
一种方法是使用Command
将UIElement
传递回您的模型。
基本上,您的inkCanvas
元素将附加CanvasLoadedCommand
这样的附加内容。请注意,只有在inkCanvas
完全加载时才会调用此命令。
<Canvas x:Name="inkCanvas" Background="Transparent">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior>
<Core:InvokeCommandAction Command="{Binding CanvasLoadedCommand}" CommandParameter="{Binding ElementName=inkCanvas}" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
我在这里为这个小demo(工作源代码)创建了一个简单的DelegateCommand
。加载inkCanvas
时,
this.inkCanvas =(Canvas)canvas;
将被调用,然后inkCanvas
属性将被填充。
private DelegateCommand _canvasLoadedCommand;
public DelegateCommand CanvasLoadedCommand
{
get
{
if (_canvasLoadedCommand == null)
{
_canvasLoadedCommand = new DelegateCommand((canvas) =>
{
this.inkCanvas = (Canvas)canvas;
},
(canvas) => canvas != null);
}
return _canvasLoadedCommand;
}
}
希望这有帮助!
答案 1 :(得分:0)
更改您的代码:
if (flipView1.Items.Count > 0) {
var myCanvas = pc.getCanvas();
m_CanvasManager = new CanvasManager(myCanvas);
}
到
if (e.AddedItems.Count > 0) {
var myCanvas = (e.AddedItems[0] as LandscapeControl).getCanvas();
m_CanvasManager = new CanvasManager(myCanvas);
}
答案 2 :(得分:0)
嗯,经过长时间的搜索,在Windows Store Apps
:我们无法访问Canvas
内的FlipView
,如果它直接位于FlipView
内DataTemplate
或FlipView
的{{1}}。