Caliburn.Micro在GridView中使用指针事件的最佳方法

时间:2016-12-19 19:26:48

标签: c# xaml mvvm uwp caliburn.micro

我有xaml代码的应用程序:

<GridView
AllowDrop="True" CanReorderItems="True"
CanDragItems="True" ItemsSource="{Binding myItems}"
ReorderMode="{Binding IsReorderEnabled}"
ItemContainerStyle="{StaticResource GridViewItemStyle1}"
cm:Message.Attach="[Event DragItemsStarting] = [Action DragItemsStarting($source, $eventArgs)]">
<GridView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="3"  HorizontalChildrenAlignment="Left" VerticalChildrenAlignment="Center" />
    </ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
    <DataTemplate>
        <Grid Height="100" 
            Width="100"
            cm:Message.Attach="[Event Tapped] = [Action ItemTapped()];
                              [Event Holding] = [Action ItemHolding()];        
                              [Event Drop] = [Action ItemDrop()];    
                              [Event DragOver] = [Action ItemDragOver($source, $eventArgs)]">
            <Grid cm:Message.Attach="[Event PointerReleased] = [Action ItemPointerReleased($source, $eventArgs)];  
                              [Event PointerCaptureLost] = [Action ItemPointerCaptureLost($source, $eventArgs)];  
                              [Event PointerCanceled] = [Action ItemPointerCanceled($source, $eventArgs)];  
                              [Event PointerEntered] = [Action ItemPointerEntered($source, $eventArgs)];    
                              [Event PointerExited] = [Action ItemPointerExited($source, $eventArgs)]">
                                <Image
                                    Source="{Binding ItemImage}" 
                                    MinHeight="30" MinWidth="30"
                                    Stretch="Fill"/>
            </Grid>
        </Grid>
    </DataTemplate>
</GridView.ItemTemplate>

问题是我用Caliburn.Micro签署的更多事件。 如果我只使用一个或两个事件,例如:

<DataTemplate>
        <Grid Height="100" 
            Width="100"
            cm:Message.Attach="[Event Tapped] = [Action ItemTapped()]">
            <Grid cm:Message.Attach="[Event PointerEntered] = [Action ItemPointerEntered($source, $eventArgs)];    
                              [Event PointerExited] = [Action ItemPointerExited($source, $eventArgs)]">
                                <Image
                                    Source="{Binding ItemImage}" 
                                    MinHeight="30" MinWidth="30"
                                    Stretch="Fill"/>
            </Grid>
        </Grid>
    </DataTemplate>

工作正常!如果我使用完整的代码,有时候我的应用程序崩溃了:

  

DependencyProperty目标无效:Windows.UI.Xaml.Controls.Grid

和随机错误:

    "System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.\r\n  
at Windows.UI.Xaml.Media.VisualTreeHelper.GetParent(DependencyObject reference)\r\n   
at Caliburn.Micro.ActionMessage.<>c.<.cctor>b__34_7(ActionExecutionContext context)\r\n   
at Caliburn.Micro.ActionMessage.<>c.<.cctor>b__34_8(ActionExecutionContext context)\r\n   
at Caliburn.Micro.ActionMessage.UpdateContext()\r\n 
at Caliburn.Micro.ActionMessage.ElementLoaded(Object sender, RoutedEventArgs e)\r"

解决问题的最佳方法是什么? Caliburn.Micro的最佳实践?

0 个答案:

没有答案