从MainPage访问UserControl中ItemsSource内的Canvas

时间:2015-05-09 06:40:05

标签: c# canvas windows-store-apps windows-8.1

我的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? 那可能吗?

3 个答案:

答案 0 :(得分:1)

一种方法是使用CommandUIElement传递回您的模型。

基本上,您的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,如果它直接位于FlipViewDataTemplateFlipView的{​​{1}}。