2014-12-30 17 views
3

我的Windows Store應用程序中有一組ScrollViewer,它們在雙擊(即展開爲完整大小)時需要始終執行操作。他們是在不同的文件,或者我可能只是把這個代碼隱藏該文件:Windows運行時應用程序中的行爲

private void ScrollViewer_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e) 
{ 
    ScrollViewer sv = (ScrollViewer)sender; 
    if (sv.HorizontalScrollBarVisibility == ScrollBarVisibility.Disabled) 
    { 
     sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; 
    } 
    else 
    { 
     sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled; 
    } 
} 

有人告訴我尋找到的行爲,所以我研究的行爲,並與一個幫助夫婦在線教程,我能想出這樣的:

class ViewboxDoubleTap : DependencyObject, IBehavior 
{ 
    public interface IBehavior 
    { 
     DependencyObject AssociatedObject { get; } 
     void Attach(DependencyObject associatedObject); 
     void Detach(); 
    } 

    public void Attach(DependencyObject associatedObject) 
    { 
     if ((associatedObject != AssociatedObject) && !DesignMode.DesignModeEnabled) 
     { 
      if (AssociatedObject != null) 
       throw new InvalidOperationException("Cannot attach behavior multiple times."); 

      AssociatedObject = associatedObject; 
      var fe = AssociatedObject as FrameworkElement; 
      if (fe != null) 
      { 
       fe.AddHandler(UIElement.DoubleTappedEvent, new DoubleTappedEventHandler(ScrollViewer_DoubleTapped), true); 
      } 
     } 
    } 

    public void Detach() 
    { 
     var fe = AssociatedObject as FrameworkElement; 
     if (fe != null) 
     { 
      fe.RemoveHandler(UIElement.DoubleTappedEvent, new DoubleTappedEventHandler(ScrollViewer_DoubleTapped)); 
     } 

     AssociatedObject = null; 
    } 

    public DependencyObject AssociatedObject { get; private set; } 

    private void ScrollViewer_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e) 
    { 
     ScrollViewer sv = (ScrollViewer)sender; 
     if (sv.HorizontalScrollBarVisibility == ScrollBarVisibility.Disabled) 
     { 
      sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; 
     } 
     else 
     { 
      sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled; 
     } 
    } 
} 

而現在,一切是很好的編譯,但我不能讓這種附着在XAML:

XML命名空間:

xmlns:i="using:Microsoft.Xaml.Interactivity" 
xmlns:core="using:Microsoft.Xaml.Interactions.Core" 
xmlns:local="using:MyApp.Folder" 
xmlns:global="using:MyApp" 

相關代碼:

<ScrollViewer HorizontalAlignment="Left" VerticalScrollBarVisibility="Hidden" MaxZoomFactor="2" MinZoomFactor="1" MaxWidth="1067"> 

    <i:Interaction.Behaviors> 
     <core:EventTriggerBehavior EventName="DoubleTapped"> 
      <global:ViewboxDoubleTap /> 
     </core:EventTriggerBehavior> 
    </i:Interaction.Behaviors> 

    <Border BorderBrush="Black" BorderThickness="1"> 
     <Image Source="MyImage.png"/> 
    </Border> 
</ScrollViewer> 

當我嘗試去這個頁面,沒有任何反應,當我調試,它給了我此錯誤消息:

WinRT information: Cannot add instance of type 'LearnOneNote.ViewboxDoubleTap' to a collection of type 'Microsoft.Xaml.Interactivity.ActionCollection'. [Line: 25 Position: 30] 

另外,它告訴我,ViewboxDoubleTap不在namespace MyApp中,但是當我輸入<global:時,它自己拉起來;我不知道這是否對這個問題很重要。

任何幫助將不勝感激。

回答

2

被放倒退房IAction後(謝謝你,franssu),我想出了這個:

[DefaultEvent(typeof(ScrollViewer),"DoubleTapped")] 
public class ScrollViewerDoubleTap : DependencyObject, IAction 
{ 
    public object Execute(object sender, object parameter) 
    { 
     ScrollViewer sv = (ScrollViewer)sender; 
     if (sv.HorizontalScrollBarVisibility == ScrollBarVisibility.Disabled) 
     { 
      sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; 
     } 
     else 
     { 
      sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled; 
     } 
     return sender; 
    } 
} 

這完美的作品。

相關問題